List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(byte b[], int off, int len)
From source file:Main.java
public static byte[] readInputStream(InputStream in) { try {/*from w ww . j av a2s . c o m*/ ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] b = new byte[in.available()]; int length = 0; while ((length = in.read(b)) != -1) { os.write(b, 0, length); } b = os.toByteArray(); in.close(); in = null; os.close(); os = null; return b; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Converts an input stream into a byte array * //from ww w.j a v a 2 s.c om * @param is * the input stream * @return byte array */ protected static byte[] convertStreamToByteArray(InputStream inputStream) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len; try { while ((len = inputStream.read(buffer)) >= 0) { byteArrayOutputStream.write(buffer, 0, len); } } catch (IOException e) { } return byteArrayOutputStream.toByteArray(); }
From source file:Main.java
public static String uncompress(InputStream inputStream) throws IOException { if (inputStream == null) { return null; }/*from w ww .j a v a2 s . c om*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPInputStream gunzip = new GZIPInputStream(inputStream); byte[] buffer = new byte[256]; int n; while ((n = gunzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } return out.toString(); }
From source file:Main.java
private static byte[] getSizeData(InputStream is) { ByteArrayOutputStream byteBuffer; try {/*from www.ja va2 s. c o m*/ byteBuffer = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } return byteBuffer.toByteArray(); } catch (Exception e) { return new byte[] {}; } }
From source file:Main.java
public static ByteArrayInputStream toByteStream(InputStream istream) { ByteArrayInputStream byteistream = new ByteArrayInputStream(new byte[0]); try {/*from ww w . j a v a 2 s . co m*/ ByteArrayOutputStream byteostream = new ByteArrayOutputStream(8192); byte[] buffer = new byte[8192]; int lenght; while ((lenght = istream.read(buffer)) != -1) { byteostream.write(buffer, 0, lenght); } byteistream = new ByteArrayInputStream(byteostream.toByteArray()); byteostream.close(); } catch (Exception e) { } finally { try { istream.close(); } catch (Exception e) { } } return byteistream; }
From source file:Main.java
/** * get byte[] from input stream//from w w w.j av a 2s. com * * @param inputStream * @return byte[] * @throws IOException */ public static byte[] getBytes(InputStream inputStream) throws IOException { ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); int bufferSize = 2048 * 10; // 2m byte[] buffer = new byte[bufferSize]; int len; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } return byteBuffer.toByteArray(); }
From source file:Main.java
public static String readInStream(InputStream inStream) { try {/*from ww w . j a v a2 s . com*/ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int length = -1; while ((length = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, length); } outStream.close(); inStream.close(); return outStream.toString(); } catch (IOException e) { Log.i("FileTest", e.getMessage()); } return null; }
From source file:Main.java
public static String decodeUTF8String(InputStream inputStream) { try {/*from w ww . j a v a 2 s . c o m*/ if (inputStream == null) { return ""; } if (inputStream.available() <= 0) { return ""; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while ((len = inputStream.read(buff, 0, buff.length)) > 0) { baos.write(buff, 0, len); } String str = new String(baos.toByteArray(), "utf-8"); return str; } catch (Exception e) { // TODO: handle exception } return null; }
From source file:Main.java
public static byte[] getByteFromInputStream(final InputStream in) throws IOException { ByteArrayOutputStream buffer = null; buffer = new ByteArrayOutputStream(); int nRead;//from ww w. j a va 2s . c o m final byte[] data = new byte[16384]; while ((nRead = in.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return buffer.toByteArray(); }
From source file:Main.java
/** * Convert InputStream to String/*from w w w .j a va 2s . c o m*/ * * @param is * @return string * @throws Exception */ public static String toString(final InputStream is) throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { final byte[] buffer = new byte[4096]; for (int b = is.read(buffer); b >= 0; b = is.read(buffer)) { out.write(buffer, 0, b); } return new String(out.toByteArray()); } catch (final Exception e) { throw e; } }