/** * Reads the given input stream and returns its content as a byte array. * * @param inputStream an input stream. * @param close true to close the input stream after reading. * @return the content of the given input stream. * @throws IOException if a problem occurs during reading. */ private static byte[] readStream(final InputStream inputStream, final boolean close) throws IOException { if (inputStream == null) { throw new IOException("Class not found"); } try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] data = new byte[INPUT_STREAM_DATA_CHUNK_SIZE]; int bytesRead; while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) { outputStream.write(data, 0, bytesRead); } outputStream.flush(); return outputStream.toByteArray(); } finally { if (close) { inputStream.close(); } } }
public byte[] getFlushedOutput() { try { output.flush(); return output.toByteArray(); } catch (IOException e) { throw Throwables.propagate(e); } } }
public static byte[] convertToByteArray(Properties props) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); props.store(out, null); out.flush(); return out.toByteArray(); }
/** * Method used to read a protobuf input stream into a byte array * @param in the protobuf input stream * @param out the byte output stream * @param buffer the array out output bytes */ public static void readFully(InputStream in, ByteArrayOutputStream out, byte[] buffer) { try { int numRead = 0; while ((numRead = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, numRead); } out.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
public static String getText(Document document, String charset) throws IOException, UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputFormat outputFormat = new OutputFormat(" ", true, charset); try { XMLWriter writer = new XMLWriter(baos, outputFormat); writer.write(document); baos.flush(); } catch (UnsupportedEncodingException e) { throw e; } catch (IOException e) { throw e; } finally { if (baos != null) { baos.close(); } } return baos.toString(charset); } }
ByteArrayOutputStream baos = new ByteArrayOutputStream(); AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder(); FileOutputStream fos = new FileOutputStream(file.getPath()); baos.writeTo(fos); baos.flush(); fos.flush(); baos.close(); fos.close(); } catch (IOException e) {
/** * 保存文件 * * @param baos * @param fileName * @throws Exception */ public static String saveFile(ByteArrayOutputStream baos, String fileName) throws Exception { String Path = App.getAppContext().getFilesDir().getPath(); File dirFile = new File(Path); if (!dirFile.exists()) { dirFile.mkdir(); } File myCaptureFile = new File(Path + "/" + fileName); FileOutputStream fi = new FileOutputStream(myCaptureFile); baos.writeTo(fi); baos.flush(); baos.close(); return myCaptureFile.getAbsolutePath(); }
private static byte[] inputStreamToBytes(InputStream is) { final int bufferSize = 16384; ByteArrayOutputStream buffer = new ByteArrayOutputStream(bufferSize); try { int nRead; byte[] data = new byte[bufferSize]; while ((nRead = is.read(data)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Error reading data from stream", e); } return null; } return buffer.toByteArray(); } }
public static byte[] write(Object o) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); baos.flush(); return baos.toByteArray(); } catch (Exception e) { return null; } }
throws XMLStreamException, IOException { String lineSeparator = System.getProperty("line.separator"); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(outStream); writer.flush(); writer.close(); outStream.flush(); String formattedLogMessage = outStream.toString(); formattedLogMessage = formattedLogMessage.replaceAll("'", "'"); formattedLogMessage = formattedLogMessage.replaceAll("<", "<"); formattedLogMessage = formattedLogMessage.replaceAll("&", "&"); outStream.close(); return formattedLogMessage;
public byte[] getFlushedOutput() { try { output.flush(); return output.toByteArray(); } catch (IOException e) { throw Throwables.propagate(e); } } }
public void handle(HttpExchange t) throws IOException { String query = t.getRequestURI().getRawQuery(); ByteArrayOutputStream response = this.response.get(); response.reset(); OutputStreamWriter osw = new OutputStreamWriter(response); TextFormat.write004(osw, registry.filteredMetricFamilySamples(parseQuery(query))); osw.flush(); osw.close(); response.flush(); response.close(); t.getResponseHeaders().set("Content-Type", TextFormat.CONTENT_TYPE_004); if (shouldUseCompression(t)) { t.getResponseHeaders().set("Content-Encoding", "gzip"); t.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); final GZIPOutputStream os = new GZIPOutputStream(t.getResponseBody()); response.writeTo(os); os.close(); } else { t.getResponseHeaders().set("Content-Length", String.valueOf(response.size())); t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.size()); response.writeTo(t.getResponseBody()); } t.close(); }
/** * 将非FileInputStream的某InputStream中的全部数据读入到字节数组中 * * @param is * @return * @throws IOException */ public static byte[] readBytesFromOtherInputStream(InputStream is) throws IOException { ByteArrayOutputStream data = new ByteArrayOutputStream(); int readBytes; byte[] buffer = new byte[Math.max(is.available(), 4096)]; // 最低4KB的缓冲区 while ((readBytes = is.read(buffer, 0, buffer.length)) != -1) { data.write(buffer, 0, readBytes); } data.flush(); return data.toByteArray(); }
public static byte[] write(Object o) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); baos.flush(); return baos.toByteArray(); } catch (Exception e) { return null; } }
private byte[] getBytes(InputStream is) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } is.close(); bos.flush(); byte[] result = bos.toByteArray(); System.out.println(new String(result)); return result; }
public static byte[] write(Object o) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); baos.flush(); return baos.toByteArray(); } catch (Exception e) { return null; } }
public static byte [] readFully(InputStream is) { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int num; byte[] data = new byte[1 << 12]; while ((num = is.read( data, 0, data.length )) != -1) buffer.write(data, 0, num); buffer.flush(); return buffer.toByteArray(); } catch (IOException ex) { return null; } }
byte[] getProtoBufAnnotation(List<CoreLabel> tokens) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); for(CoreLabel token: tokens){ CoreNLPProtos.Token ptoken = p.toProto(token); ptoken.writeDelimitedTo(os); } os.flush(); return os.toByteArray(); }
private void copy() throws IOException { _copy = new ByteArrayOutputStream(); int chunk; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { _copy.write(data, 0, chunk); } _copy.flush(); }