public static byte[] obtainByteData(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] bytes = new byte[512]; // Read bytes from the input stream in bytes.length-sized chunks and write // them into the output stream int readBytes; while ((readBytes = is.read(bytes)) > 0) { os.write(bytes, 0, readBytes); } // Convert the contents of the output stream into a byte array byte[] byteData = os.toByteArray(); // Close the streams is.close(); os.close(); return byteData; }
/** * @see java.io.ByteArrayOutputStream#reset() */ public synchronized void reset() { count = 0; filledBufferSum = 0; currentBufferIndex = 0; currentBuffer = getBuffer(currentBufferIndex); }
/** * Creates a new byte array output stream, with a buffer capacity of the specified size, in * bytes. * * @param size * the initial size. * @exception IllegalArgumentException * if size is negative. */ public ByteArrayOutputStream(final int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } needNewBuffer(size); }
/** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input * the <code>InputStream</code> to read from * @return the requested byte array * @throws NullPointerException * if the input is null * @throws IOException * if an I/O error occurs */ public static byte[] toByteArray(final InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
@Override protected byte[] readAllResources(List<IResourceStream> resources) throws IOException, ResourceStreamNotFoundException { ByteArrayOutputStream output = new ByteArrayOutputStream(); for (IResourceStream curStream : resources) { IOUtils.copy(curStream.getInputStream(), output); output.write(";".getBytes()); } byte[] bytes = output.toByteArray(); if (getCompressor() != null) { String nonCompressed = new String(bytes, "UTF-8"); bytes = getCompressor().compress(nonCompressed).getBytes("UTF-8"); } return bytes; }
/** * Returns the data for this output stream as an array of bytes, assuming that the data has been * retained in memory. If the data was written to disk, this method returns <code>null</code>. * * @return The data for this output stream, or <code>null</code> if no such data is available. */ public byte[] getData() { if (memoryOutputStream != null) { return memoryOutputStream.toByteArray(); } return null; }
/** * Constructs an instance of this class which will trigger an event at the specified threshold, * and save data to a file beyond that point. * * @param threshold * The number of bytes at which to trigger an event. * @param outputFile * The file to which data is saved beyond the threshold. */ public DeferredFileOutputStream(int threshold, File outputFile) { super(threshold); this.outputFile = outputFile; memoryOutputStream = new ByteArrayOutputStream(); currentOutputStream = memoryOutputStream; }
/** * Calls the write(byte[]) method. * * @see java.io.OutputStream#write(int) */ public synchronized void write(int b) { write(new byte[] { (byte)b }, 0, 1); }
/** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input * the <code>InputStream</code> to read from * @return the requested byte array * @throws NullPointerException * if the input is null * @throws IOException * if an I/O error occurs */ public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
/** * Returns the data for this output stream as an array of bytes, assuming that the data has been * retained in memory. If the data was written to disk, this method returns <code>null</code>. * * @return The data for this output stream, or <code>null</code> if no such data is available. */ public byte[] getData() { if (memoryOutputStream != null) { return memoryOutputStream.toByteArray(); } return null; }
/** * Constructs an instance of this class which will trigger an event at the specified threshold, * and save data to a file beyond that point. * * @param threshold * The number of bytes at which to trigger an event. * @param outputFile * The file to which data is saved beyond the threshold. */ public DeferredFileOutputStream(int threshold, File outputFile) { super(threshold); if (outputFile == null) throw new IllegalArgumentException("output file must be specified"); this.outputFile = outputFile; fileFactory = null; memoryOutputStream = new ByteArrayOutputStream(); currentOutputStream = memoryOutputStream; }
/** * Calls the write(byte[]) method. * * @see java.io.OutputStream#write(int) */ @Override public synchronized void write(final int b) { write(new byte[] { (byte)b }, 0, 1); }
/** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input * the <code>InputStream</code> to read from * @return the requested byte array * @throws NullPointerException * if the input is null * @throws IOException * if an I/O error occurs */ public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
/** * Returns the data for this output stream as an array of bytes, assuming that the data has been * retained in memory. If the data was written to disk, this method returns <code>null</code>. * * @return The data for this output stream, or <code>null</code> if no such data is available. */ public byte[] getData() { if (memoryOutputStream != null) { return memoryOutputStream.toByteArray(); } return null; }
/** * Constructs an instance of this class which will trigger an event at the specified threshold, * and save data to a file beyond that point. * * @param threshold * The number of bytes at which to trigger an event. * @param fileFactory * The FileFactory to create the file. */ public DeferredFileOutputStream(int threshold, FileFactory fileFactory) { super(threshold); if (fileFactory == null) throw new IllegalArgumentException("FileFactory must be specified"); this.fileFactory = fileFactory; memoryOutputStream = new ByteArrayOutputStream(); currentOutputStream = memoryOutputStream; }
/** * @see java.io.ByteArrayOutputStream#reset() */ public synchronized void reset() { count = 0; filledBufferSum = 0; currentBufferIndex = 0; currentBuffer = getBuffer(currentBufferIndex); }
/** * Calls the write(byte[]) method. * * @see java.io.OutputStream#write(int) */ @Override public synchronized void write(int b) { write(new byte[] { (byte)b }, 0, 1); }
/** * Creates a new byte array output stream, with a buffer capacity of the specified size, in * bytes. * * @param size * the initial size. * @exception IllegalArgumentException * if size is negative. */ public ByteArrayOutputStream(int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } needNewBuffer(size); }
/** * Get the contents of a <code>Reader</code> as a <code>byte[]</code> using the default * character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * * @param input * the <code>Reader</code> to read from * @return the requested byte array * @throws NullPointerException * if the input is null * @throws IOException * if an I/O error occurs */ public static byte[] toByteArray(Reader input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
/** * This as a string using the provided encoding. * * @param enc * the encoding to use * @return This as a string using the provided encoding * @throws UnsupportedEncodingException */ public String toString(final String enc) throws UnsupportedEncodingException { return new String(toByteArray(), enc); }