/** * This method is a shorthand for {@link #copyStream(java.io.InputStream, java.io.OutputStream, int, long)} called * with a {@link Long#MAX_VALUE}. * * @param in the InputStream to read from * @param out the OutputStream to write to * @param bufferSize size of the buffer to use, in bytes * @return the number of bytes that were copied * @throws FileTransferException if something went wrong while reading from or writing to one of the provided streams */ public static long copyStream(InputStream in, OutputStream out, int bufferSize) throws FileTransferException { return copyStream(in, out, bufferSize, Long.MAX_VALUE); }
/** * This method is a shorthand for {@link #readUntilEOF(java.io.InputStream, int)} called with a * {@link BufferPool#getDefaultBufferSize default buffer size}. * * @param in the InputStream to read * @throws IOException if an I/O error occurs */ public static void readUntilEOF(InputStream in) throws IOException { readUntilEOF(in, BufferPool.getDefaultBufferSize()); }
/** * This method is a shorthand for {@link #copyChunk(RandomAccessInputStream, RandomAccessOutputStream, long, long, long, int)} * called with a {@link BufferPool#getDefaultBufferSize default buffer size}. * * @param rais the source stream * @param raos the destination stream * @param srcOffset offset to the beginning of the chunk in the source stream * @param destOffset offset to the beginning of the chunk in the destination stream * @param length number of bytes to copy * @throws java.io.IOException if an error occurred while copying data */ public static void copyChunk(RandomAccessInputStream rais, RandomAccessOutputStream raos, long srcOffset, long destOffset, long length) throws IOException { copyChunk(rais, raos, srcOffset, destOffset, length, BufferPool.getDefaultBufferSize()); }
/** * This method is a shorthand for {@link #readFully(java.io.InputStream, byte[], int, int)}. * * @param in the InputStream to read from * @param b the buffer into which the stream data is copied * @return the same byte array that was passed, returned only for convience * @throws java.io.EOFException if EOF is reached before all bytes have been read * @throws IOException if an I/O error occurs */ public static byte[] readFully(InputStream in, byte b[]) throws EOFException, IOException { return readFully(in, b, 0, b.length); }
StreamUtils.copyChunk(rais, raos, cdStartOffset, newCdStartOffset, cdLength); StreamUtils.fillWithConstant(raos, (byte)0, entryInfo.dataOffset-entryInfo.headerOffset + ze.getCompressedSize(), WRITE_BUFFER_SIZE); cdEndOffset = lastEntryInfo.centralHeaderOffset + lastEntryInfo.centralHeaderLen; StreamUtils.copyChunk(rais, raos, startOffset, entryInfo.centralHeaderOffset, cdEndOffset-startOffset);
/** * This method is a shorthand for {@link #fillWithConstant(java.io.OutputStream, byte, long, int)} called with a * {@link BufferPool#getDefaultBufferSize default buffer size}. * * @param out the OutputStream to write to * @param value the byte constant to write len times * @param len number of bytes to write * @throws java.io.IOException if an error occurred while writing */ public static void fillWithConstant(OutputStream out, byte value, long len) throws IOException { fillWithConstant(out, value, len, BufferPool.getDefaultBufferSize()); }
/** * Creates a new <code>ArArchiveEntryIterator</code> that parses the given AR <code>InputStream</code>. * The <code>InputStream</code> will be closed by {@link #close()}. * * @param in an AR archive <code>InputStream</code> * @throws IOException if an I/O error occurred while initializing this iterator */ ArArchiveEntryIterator(InputStream in) throws IOException { this.in = in; // Skip the global header: "!<arch>" string followed by LF char (8 characters in total). StreamUtils.skipFully(in, 8); }
/** * This method is a shorthand for {@link #readUpTo(java.io.InputStream, byte[], int, int) readUpTo(in, b, 0, b.length)}. * * @param in the InputStream to read from * @param b the buffer into which the stream data is copied * @return the number of bytes that have been read, can be less than len if EOF has been reached prematurely * @throws IOException if an I/O error occurs */ public static int readUpTo(InputStream in, byte b[]) throws IOException { return readUpTo(in, b, 0, b.length); }
/** * Reads <code>b.length</code> bytes from this file into the byte array, starting at the current file pointer. * This method reads repeatedly from the file until the requested number of bytes are read. This method blocks until * the requested number of bytes are read, the end of the stream is detected, or an exception is thrown. * * @param b the buffer into which the data is read. * @throws java.io.EOFException if this file reaches the end before reading all the bytes. * @throws IOException if an I/O error occurs. */ public void readFully(byte b[]) throws IOException { StreamUtils.readFully(this, b, 0, b.length); }
public ArchiveEntry nextEntry() throws IOException { if(currentEntry!=null) { // Skip the current entry's data, plus 1 padding byte if size is odd long size = currentEntry.getSize(); StreamUtils.skipFully(in, size + (size%2)); } // Get the next entry, if any currentEntry = getNextEntry(); return currentEntry; }
/** * Tries and detect whether the given stream contains binary or text data.</br> * This method returns <code>true</code> if it thinks that the bytes correspond to binary data. * * <p>A maximum of {@link #RECOMMENDED_BYTE_SIZE} will be read from the <code>InputStream</code>. The * stream will not be closed and will not be repositionned after the bytes have been read. It is up to the calling * method to use the <code>InputStream#mark()</code> and <code>InputStream#reset()</code> methods (if supported) * or reopen the stream if needed. * </p> * * @param in the stream to analyze * @return true if BinaryDetector thinks that the specified data is binary * @throws IOException if an error occurred while reading the InputStream. */ public static boolean guessBinary(InputStream in) throws IOException { byte[] bytes = new byte[RECOMMENDED_BYTE_SIZE]; return guessBinary(bytes, 0, StreamUtils.readUpTo(in, bytes)); } }
private void sendFile(InputStream in, URLConnection conn) throws IOException { conn.connect(); OutputStream out = conn.getOutputStream(); try { StreamUtils.copyStream(in, out, IO_BUFFER_SIZE); } finally { out.close(); } }
private void assertReadable(InputStream in) throws IOException { assert in != null; StreamUtils.readUntilEOF(in); in.close(); }
/** * Reads exactly <code>len</code> bytes from this file into the byte array, starting at the current file pointer. * This method reads repeatedly from the file until the requested number of bytes are read. This method blocks until * the requested number of bytes are read, the end of the stream is detected, or an exception is thrown. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the number of bytes to read. * @throws java.io.EOFException if this file reaches the end before reading all the bytes. * @throws IOException if an I/O error occurs. */ public void readFully(byte b[], int off, int len) throws IOException { StreamUtils.readFully(this, b, off, len); }
StreamUtils.copyChunk(rais, raos, currentEntryInfo.headerOffset, 0, (currentEntryInfo.dataOffset- currentEntryInfo.headerOffset)+currentEntry.getCompressedSize()); shift = currentEntryInfo.headerOffset; StreamUtils.copyChunk(rais, raos, currentEntryInfo.headerOffset, previousEntryInfo.dataOffset+previousCompressedSize, (currentEntryInfo.dataOffset- currentEntryInfo.headerOffset)+currentEntry.getCompressedSize()); shift = currentEntryInfo.headerOffset - (previousEntryInfo.dataOffset+previousCompressedSize);
StreamUtils.skipFully(in, 2);
/** * Try and detect the character encoding in which the bytes contained by the given <code>InputStream</code> are * encoded, and returns the best guess or <code>null</code> if there is none (not enough data or confidence). * Note that the returned character encoding may or may not be available on the Java runtime -- use * <code>java.nio.Charset#isSupported(String)</code> to determine if it is available. * * <p>A maximum of {@link #MAX_RECOMMENDED_BYTE_SIZE} will be read from the <code>InputStream</code>. The * stream will not be closed and will not be repositionned after the bytes have been read. It is up to the calling * method to use the <code>InputStream#mark()</code> and <code>InputStream#reset()</code> methods (if supported) * or reopen the stream if needed. * </p> * * @param in the InputStream that supplies the bytes * @return the best guess at the character encoding, null if there is none (not enough data or confidence) * @throws IOException if an error occurred while reading the stream */ public static String detectEncoding(InputStream in) throws IOException { byte buf[] = BufferPool.getByteArray(MAX_RECOMMENDED_BYTE_SIZE); try { return detectEncoding(buf, 0, StreamUtils.readUpTo(in, buf)); } finally { BufferPool.releaseByteArray(buf); } }
/** * This method is a shorthand for {@link #copyStream(java.io.InputStream, java.io.OutputStream, int)} called with a * {@link BufferPool#getDefaultBufferSize() default buffer size}. * * @param in the InputStream to read from * @param out the OutputStream to write to * @return the number of bytes that were copied * @throws FileTransferException if something went wrong while reading from or writing to one of the provided streams */ public static long copyStream(InputStream in, OutputStream out) throws FileTransferException { return copyStream(in, out, BufferPool.getDefaultBufferSize()); }
private void assertReadable(URL url) throws IOException { assert url != null; InputStream in = url.openStream(); StreamUtils.readUntilEOF(in); in.close(); }