List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:com.aurel.track.lucene.util.FileUtil.java
public static byte[] getBytes(File file) throws IOException { if (file == null || !file.exists()) { return null; }//w w w. ja v a2 s . c om ByteArrayOutputStream out = new ByteArrayOutputStream(); FileInputStream in = new FileInputStream(file); int c = in.read(); while (c != -1) { out.write(c); c = in.read(); } in.close(); out.close(); return out.toByteArray(); }
From source file:cn.sharesdk.net.NetworkHelper.java
public static String Base64Gzip(String str) { ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String result = null;//from www . j a v a 2 s. c o m // gzip GZIPOutputStream gos; try { gos = new GZIPOutputStream(baos); int count; byte data[] = new byte[1024]; while ((count = bais.read(data, 0, 1024)) != -1) { gos.write(data, 0, count); } gos.finish(); gos.close(); byte[] output = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); result = Base64.encodeToString(output, Base64.NO_WRAP); } catch (IOException e) { e.printStackTrace(); Ln.i("NetworkHelper", "Base64Gzip == >>", e); } return result; }
From source file:com.chenshu.compress.CompressSizeTest.java
public static int compress(StreamFactory factory, byte[] src) { byte[] dest = null; ByteArrayOutputStream bout = null; OutputStream out = null;//w w w. j a v a 2 s .c om try { bout = new ByteArrayOutputStream(src.length); out = factory.getOutputStream(bout); out.write(src); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (bout != null) { try { bout.close(); } catch (IOException e) { e.printStackTrace(); } } } byte[] bs = bout.toByteArray(); ByteArrayInputStream bin = null; InputStream in = null; ByteArrayOutputStream os = null; try { bin = new ByteArrayInputStream(bs); in = factory.getInputStream(bin); dest = new byte[src.length]; os = new ByteArrayOutputStream(src.length); int count = 0; while ((count = in.read(dest)) != -1) { os.write(dest, 0, count); } dest = os.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (bin != null) { try { bin.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(src.length == dest.length); System.out.println(Arrays.equals(src, dest)); System.out.println( new String(src, Charset.forName("UTF-8")).equals(new String(dest, Charset.forName("UTF-8")))); return bs.length; }
From source file:com.jrummyapps.busybox.signing.ZipSigner.java
/** * Write a .SF file with a digest the specified manifest. *///from w w w .java 2 s . c o m private static byte[] writeSignatureFile(Manifest manifest, OutputStream out) throws IOException, GeneralSecurityException { final Manifest sf = new Manifest(); final Attributes main = sf.getMainAttributes(); main.putValue("Manifest-Version", MANIFEST_VERSION); main.putValue("Created-By", CREATED_BY); final MessageDigest md = MessageDigest.getInstance("SHA1"); final PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, "UTF-8"); // Digest of the entire manifest manifest.write(print); print.flush(); main.putValue("SHA1-Digest-Manifest", base64encode(md.digest())); final Map<String, Attributes> entries = manifest.getEntries(); for (final Map.Entry<String, Attributes> entry : entries.entrySet()) { // Digest of the manifest stanza for this entry. print.print("Name: " + entry.getKey() + "\r\n"); for (final Map.Entry<Object, Object> att : entry.getValue().entrySet()) { print.print(att.getKey() + ": " + att.getValue() + "\r\n"); } print.print("\r\n"); print.flush(); final Attributes sfAttr = new Attributes(); sfAttr.putValue("SHA1-Digest", base64encode(md.digest())); sf.getEntries().put(entry.getKey(), sfAttr); } final ByteArrayOutputStream sos = new ByteArrayOutputStream(); sf.write(sos); String value = sos.toString(); String done = value.replace("Manifest-Version", "Signature-Version"); out.write(done.getBytes()); print.close(); sos.close(); return done.getBytes(); }
From source file:io.confluent.kafkarest.converters.AvroConverter.java
/** * Converts Avro data (including primitive types) to their equivalent JsonNode representation. * * @param value the value to convert//from w ww .j av a 2 s . c o m * @return an object containing the root JsonNode representing the converted object and the size * in bytes of the data when serialized */ public static JsonNodeAndSize toJson(Object value) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Schema schema = getSchema(value); JsonEncoder encoder = encoderFactory.jsonEncoder(schema, out); DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); // Some types require wrapping/conversion Object wrappedValue = value; if (value instanceof byte[]) { wrappedValue = ByteBuffer.wrap((byte[]) value); } writer.write(wrappedValue, encoder); encoder.flush(); byte[] bytes = out.toByteArray(); out.close(); return new JsonNodeAndSize(jsonMapper.readTree(bytes), bytes.length); } catch (IOException e) { // These can be generated by Avro's JSON encoder, the output stream operations, and the // Jackson ObjectMapper.readTree() call. log.error("Jackson failed to deserialize JSON generated by Avro's JSON encoder: ", e); throw new ConversionException("Failed to convert Avro to JSON: " + e.getMessage()); } catch (RuntimeException e) { // Catch-all since it's possible for, e.g., Avro to throw many different RuntimeExceptions log.error("Unexpected exception convertion Avro to JSON: ", e); throw new ConversionException("Failed to convert Avro to JSON: " + e.getMessage()); } }
From source file:com.bigdata.dastor.utils.FBUtilities.java
public static byte[] decompress(byte[] compressedData, int off, int len) throws IOException, DataFormatException { // Create the decompressor and give it the data to compress Inflater decompressor = new Inflater(); decompressor.setInput(compressedData, off, len); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length); // Decompress the data byte[] buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count);/*from ww w . ja v a 2 s.c o m*/ } bos.close(); // Get the decompressed data return bos.toByteArray(); }
From source file:com.liferay.util.Http.java
public static byte[] URLtoByteArray(String location, Cookie[] cookies, boolean post) throws IOException { byte[] byteArray = null; HttpMethod method = null;/*from www . j a v a 2 s . c o m*/ try { HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); if (location == null) { return byteArray; } else if (!location.startsWith(HTTP_WITH_SLASH) && !location.startsWith(HTTPS_WITH_SLASH)) { location = HTTP_WITH_SLASH + location; } HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(new URI(location)); if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) { hostConfig.setProxy(PROXY_HOST, PROXY_PORT); } client.setHostConfiguration(hostConfig); client.setConnectionTimeout(5000); client.setTimeout(5000); if (cookies != null && cookies.length > 0) { HttpState state = new HttpState(); state.addCookies(cookies); state.setCookiePolicy(CookiePolicy.COMPATIBILITY); client.setState(state); } if (post) { method = new PostMethod(location); } else { method = new GetMethod(location); } method.setFollowRedirects(true); client.executeMethod(method); Header locationHeader = method.getResponseHeader("location"); if (locationHeader != null) { return URLtoByteArray(locationHeader.getValue(), cookies, post); } InputStream is = method.getResponseBodyAsStream(); if (is != null) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] bytes = new byte[512]; for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) { buffer.write(bytes, 0, i); } byteArray = buffer.toByteArray(); is.close(); buffer.close(); } return byteArray; } finally { try { if (method != null) { method.releaseConnection(); } } catch (Exception e) { Logger.error(Http.class, e.getMessage(), e); } } }
From source file:aarddict.Volume.java
static String decompressBz2(byte[] bytes) throws IOException { BZip2CompressorInputStream in = new BZip2CompressorInputStream(new ByteArrayInputStream(bytes)); int n = 0;//ww w . j av a 2 s. com ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 5); byte[] buf = new byte[1024]; try { while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } } finally { in.close(); out.close(); } return utf8(out.toByteArray()); }
From source file:org.opendatakit.common.utils.WebUtils.java
/** * Decode a safeEncode() string.// w ww . ja v a 2s . c o m * * @param encodedWebsafeString * @return rawString */ public static String safeDecode(String encodedWebsafeString) { if (encodedWebsafeString == null || encodedWebsafeString.length() == 0) { return encodedWebsafeString; } try { ByteArrayInputStream in = new ByteArrayInputStream( Base64.decodeBase64(encodedWebsafeString.getBytes(CharEncoding.UTF_8))); GZIPInputStream gzip = new GZIPInputStream(in); ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch = gzip.read(); while (ch >= 0) { out.write(ch); ch = gzip.read(); } gzip.close(); out.flush(); out.close(); return new String(out.toByteArray(), CharEncoding.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new IllegalArgumentException("Unexpected failure: " + e.toString()); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException("Unexpected failure: " + e.toString()); } }
From source file:com.clustercontrol.plugin.impl.AsyncTask.java
/** * SerializableBinary???/* ww w . j av a 2 s .c om*/ * @param obj Serializable * @return ??Binary * @throws IOException */ public static byte[] encodeBinary(Serializable obj) throws IOException { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; byte[] bytes = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(obj); bytes = baos.toByteArray(); } finally { if (oos != null) { oos.close(); } if (baos != null) { baos.close(); } } return bytes; }