List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(byte b[], int off, int len)
From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java
private static String inflate(String saml) throws Exception { byte[] compressedData = Base64.decodeBase64(saml); ByteArrayInputStream bin = new ByteArrayInputStream(compressedData); InflaterInputStream decompressor = new InflaterInputStream(bin, new Inflater(true)); //decompressor.setInput(compressedData); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length); // Decompress the data byte[] buf = new byte[1024]; int len;//from w w w . ja v a2 s . c o m while ((len = decompressor.read(buf)) > 0) { bos.write(buf, 0, len); } try { bos.close(); } catch (IOException e) { } // Get the decompressed data byte[] decompressedData = bos.toByteArray(); String decoded = new String(decompressedData); return decoded; }
From source file:Main.java
private static byte[] uncompressBytesInflateDeflate(byte[] inBytes) throws IOException { Inflater inflater = new Inflater(); inflater.setInput(inBytes);// ww w . j ava2 s. c om ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length); byte[] buffer = new byte[1024 * 8]; while (!inflater.finished()) { int count; try { count = inflater.inflate(buffer); } catch (DataFormatException e) { throw new IOException(e); } bos.write(buffer, 0, count); } byte[] output = bos.toByteArray(); return output; }
From source file:Main.java
@SuppressWarnings("unchecked") public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) { Parcelable parcelable = null;/*from www.java2 s . c o m*/ FileInputStream fis = null; ByteArrayOutputStream bos = null; try { fis = context.openFileInput(fileName); if (fis != null) { bos = new ByteArrayOutputStream(); byte[] b = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(b)) != -1) { bos.write(b, 0, bytesRead); } byte[] data = bos.toByteArray(); Parcel parcel = Parcel.obtain(); parcel.unmarshall(data, 0, data.length); parcel.setDataPosition(0); parcelable = parcel.readParcelable(classLoader); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); parcelable = null; } finally { if (fis != null) try { fis.close(); } catch (IOException e) { e.printStackTrace(); } if (bos != null) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } return parcelable; }
From source file:Main.java
@SuppressWarnings("unchecked") public static List<Parcelable> readParcelableList(Context context, String fileName, ClassLoader classLoader) { List<Parcelable> results = null; FileInputStream fis = null;// ww w. ja va 2 s. co m ByteArrayOutputStream bos = null; try { fis = context.openFileInput(fileName); if (fis != null) { bos = new ByteArrayOutputStream(); byte[] b = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(b)) != -1) { bos.write(b, 0, bytesRead); } byte[] data = bos.toByteArray(); Parcel parcel = Parcel.obtain(); parcel.unmarshall(data, 0, data.length); parcel.setDataPosition(0); results = parcel.readArrayList(classLoader); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); results = null; } finally { if (fis != null) try { fis.close(); } catch (IOException e) { e.printStackTrace(); } if (bos != null) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } return results; }
From source file:Main.java
/** * Read a file from assets/*from w w w. ja v a 2 s.c om*/ * * @return the string from assets */ public static String getfromAssets(Context ctx, String file_name) { AssetManager assetManager = ctx.getAssets(); ByteArrayOutputStream outputStream = null; InputStream inputStream = null; try { inputStream = assetManager.open(file_name); outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); } catch (IOException e) { } } catch (IOException e) { } return outputStream.toString(); }
From source file:ai.susi.server.api.susi.ConsoleService.java
public static byte[] loadData(String url) throws IOException { ClientConnection cc = new ClientConnection(url); // fully read the input stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); int n;// w ww . j a v a 2 s.co m byte[] buffer = new byte[16384]; try { while ((n = cc.inputStream.read(buffer, 0, buffer.length)) != -1) baos.write(buffer, 0, n); } catch (IOException e) { } baos.flush(); byte[] b = baos.toByteArray(); // finished, close cc.close(); return b; }
From source file:com.cats.version.utils.Utils.java
public static String getResultFromStream(InputStream input) { if (null != input) { int iLen = -1; byte[] data = new byte[512]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); try {// www .jav a 2 s . co m do { iLen = input.read(data); if (iLen != -1) { arrayOutputStream.write(data, 0, iLen); } } while (iLen != -1); } catch (Exception e) { e.printStackTrace(); } data = arrayOutputStream.toByteArray(); return new String(data, Charset.defaultCharset()); } return "Process starup failed. inputstream empty"; }
From source file:com.fallenangelsguild.eltime.WebHelper.java
/** * Pull the raw text content of the given URL. This call blocks until the * operation has completed, and is synchronized because it uses a shared * buffer {@link #sBuffer}./* w w w . j a va 2s . c o m*/ * * @param url The exact URL to request. * @return The raw content returned by the server. * @throws ApiException If any connection or server error occurs. */ protected static synchronized String getUrlContent(String url) throws ApiException { if (c == null) { throw new ApiException("Must initialize() first"); } // Create client and set our specific user-agent string HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); request.setHeader("User-Agent", sUserAgent); try { HttpResponse response = client.execute(request); // Check if server response is valid StatusLine status = response.getStatusLine(); if (status.getStatusCode() != HTTP_STATUS_OK) { throw new ApiException("Invalid response from server: " + status.toString()); } // Pull content stream from response HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ByteArrayOutputStream content = new ByteArrayOutputStream(); // Read response into a buffered stream int readBytes = 0; while ((readBytes = inputStream.read(sBuffer)) != -1) { content.write(sBuffer, 0, readBytes); } // Return result from buffered stream return new String(content.toByteArray()); } catch (IOException e) { throw new ApiException("Problem communicating with API", e); } }
From source file:com.ksc.http.apache.client.impl.ApacheDefaultHttpRequestFactoryTest.java
public static byte[] drainInputStream(InputStream inputStream) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try {//from w w w .j a v a 2s . com byte[] buffer = new byte[1024]; long bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) > -1) { byteArrayOutputStream.write(buffer, 0, (int) bytesRead); } return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(byteArrayOutputStream, null); } }
From source file:br.com.manish.ahy.kernel.util.FileUtil.java
public static byte[] readResourceAsBytes(String path) { byte[] byteData = null; try {// w ww . ja va2 s . c om InputStream is = FileUtil.class.getResourceAsStream(path); if (is.available() > Integer.MAX_VALUE) { throw new OopsException("Oversized file :-( can't read it, sorry: " + path); } ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] bytes = new byte[512]; int readBytes; while ((readBytes = is.read(bytes)) > 0) { os.write(bytes, 0, readBytes); } byteData = os.toByteArray(); is.close(); os.close(); } catch (Exception e) { throw new OopsException(e, "Problems when reading: [" + path + "]."); } return byteData; }