| /******************************************************************************* |
| * Copyright (c) 2009 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Matthew Hatem, IBM Corporation - initial API and implementation |
| * Boris Bokowski, IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.e4.ui.internal.web; |
| |
| import java.io.ByteArrayOutputStream; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| |
| public class E4BrowserUtil { |
| |
| public static byte[] getBytesFromFile(File file) throws IOException { |
| InputStream is = new FileInputStream(file); |
| long length = file.length(); |
| byte[] bytes = new byte[(int)length]; |
| int offset = 0; |
| int numRead = 0; |
| while (offset < bytes.length |
| && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
| offset += numRead; |
| } |
| if (offset < bytes.length) { |
| throw new IOException("File read incomplete"+file.getName()); |
| } |
| is.close(); |
| return bytes; |
| } |
| |
| public static byte[] getBytesFromStream(InputStream is) throws IOException { |
| int BUFFER_SIZE = 8192; |
| byte[] buffer = new byte[BUFFER_SIZE]; |
| ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| int numRead = 0; |
| while ((numRead = is.read(buffer)) > 0) { |
| os.write(buffer, 0, numRead); |
| } |
| return os.toByteArray(); |
| } |
| |
| } |