/*** * Same as <code> copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE); </code> * @param source where to copy from * @param dest where to copy to * @return number of bytes copied * @throws CopyStreamException on error ***/ public static final long copyStream(InputStream source, OutputStream dest) throws CopyStreamException { return copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE); }
/** * Returns the IOException responsible for the failure of a copy operation. * @return The IOException responsible for the failure of a copy operation. */ public IOException getIOException() { return (IOException) getCause(); // cast is OK because it was initialised with an IOException } }
/*** * Writes a character array to the output. * * @param buffer The character array to write. * @throws IOException If an error occurs while writing to the underlying * output. ***/ @Override public void write(char[] buffer) throws IOException { write(buffer, 0, buffer.length); }
@Override public void bytesTransferred(CopyStreamEvent event) { bytesTransferred(event.getTotalBytesTransferred(), event.getBytesTransferred(), event.getStreamSize()); }
/** * * @param _reader the reader to wrap * @param addDotReader whether to additionally wrap the reader in a DotTerminatedMessageReader * @throws IOException */ ReplyIterator(BufferedReader _reader, boolean addDotReader) throws IOException { reader = addDotReader ? new DotTerminatedMessageReader(_reader) : _reader; line = reader.readLine(); // prime the iterator if (line == null) { Util.closeQuietly(reader); } }
/*** * Same as <code> copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE); </code> * @param source where to copy from * @param dest where to copy to * @return number of bytes copied * @throws CopyStreamException on error ***/ public static final long copyReader(Reader source, Writer dest) throws CopyStreamException { return copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE); }
/*** * Writes a byte array to the stream. * * @param buffer The byte array to write. * @throws IOException If an error occurs while writing to the underlying * stream. ***/ @Override public synchronized void write(byte buffer[]) throws IOException { write(buffer, 0, buffer.length); }
/** * This method is invoked by a CopyStreamEvent source after copying * a block of bytes from a stream. The CopyStreamEvent will contain * the total number of bytes transferred so far and the number of bytes * transferred in the last write. The CopyStreamAdapater will relay * the event to all of its registered listeners, listing itself as the * source of the event. * @param event The CopyStreamEvent fired by the copying of a block of * bytes. */ @Override public void bytesTransferred(CopyStreamEvent event) { for (EventListener listener : internalListeners) { ((CopyStreamListener) (listener)).bytesTransferred(event); } }
/*** * Writes a byte array to the stream. * * @param buffer The byte array to write. * @throws IOException If an error occurs while writing to the underlying * stream. ***/ @Override public synchronized void write(byte buffer[]) throws IOException { write(buffer, 0, buffer.length); }
/** * Reads the next characters from the message into an array and * returns the number of characters read. Returns -1 if the end of the * message has been reached. * @param buffer The character array in which to store the characters. * @return The number of characters read. Returns -1 if the * end of the message has been reached. * @throws IOException If an error occurs in reading the underlying * stream. */ @Override public int read(char[] buffer) throws IOException { return read(buffer, 0, buffer.length); }
/*** * Reads the next number of bytes from the stream into an array and * returns the number of bytes read. Returns -1 if the end of the * stream has been reached. * * @param buffer The byte array in which to store the data. * @return The number of bytes read. Returns -1 if the * end of the message has been reached. * @throws IOException If an error occurs in reading the underlying * stream. ***/ @Override public int read(byte buffer[]) throws IOException { return read(buffer, 0, buffer.length); }
/*** * Reads the next number of bytes from the stream into an array and * returns the number of bytes read. Returns -1 if the end of the * stream has been reached. * * @param buffer The byte array in which to store the data. * @return The number of bytes read. Returns -1 if the * end of the message has been reached. * @throws IOException If an error occurs in reading the underlying * stream. ***/ @Override public int read(byte buffer[]) throws IOException { return read(buffer, 0, buffer.length); }
/** * Merge two copystream listeners, either or both of which may be null. * * @param local the listener used by this class, may be null * @return a merged listener or a single listener or null * @since 3.0 */ private CopyStreamListener __mergeListeners(CopyStreamListener local) { if (local == null) { return __copyStreamListener; } if (__copyStreamListener == null) { return local; } // Both are non-null CopyStreamAdapter merged = new CopyStreamAdapter(); merged.addCopyStreamListener(local); merged.addCopyStreamListener(__copyStreamListener); return merged; }
/** * Creates a new CopyStreamException instance. * @param message A message describing the error. * @param bytesTransferred The total number of bytes transferred before * an exception was thrown in a copy operation. * @param exception The IOException thrown during a copy operation. */ public CopyStreamException(String message, long bytesTransferred, IOException exception) { super(message); initCause(exception); // merge this into super() call once we need 1.6+ totalBytesTransferred = bytesTransferred; }
/*** * Writes a number of characters from a character array to the output * starting from a given offset. * * @param buffer The character array to write. * @param offset The offset into the array at which to start copying data. * @param length The number of characters to write. * @throws IOException If an error occurs while writing to the underlying * output. ***/ @Override public void write(char[] buffer, int offset, int length) throws IOException { synchronized (lock) { while (length-- > 0) { write(buffer[offset++]); } } }
/*** * Copies the contents of a Reader to a Writer using a * copy buffer of a given size. The contents of the Reader are * read until its end is reached, but neither the source nor the * destination are closed. You must do this yourself outside of the * method call. The number of characters read/written is returned. * * @param source The source Reader. * @param dest The destination writer. * @param bufferSize The number of characters to buffer during the copy. * A zero or negative value means to use {@link #DEFAULT_COPY_BUFFER_SIZE}. * @return The number of characters read/written in the copy operation. * @throws CopyStreamException If an error occurs while reading from the * source or writing to the destination. The CopyStreamException * will contain the number of bytes confirmed to have been * transferred before an * IOException occurred, and it will also contain the IOException * that caused the error. These values can be retrieved with * the CopyStreamException getTotalBytesTransferred() and * getIOException() methods. ***/ public static final long copyReader(Reader source, Writer dest, int bufferSize) throws CopyStreamException { return copyReader(source, dest, bufferSize, CopyStreamEvent.UNKNOWN_STREAM_SIZE, null); }
/*** * Writes a number of bytes from a byte array to the stream starting from * a given offset. * * @param buffer The byte array to write. * @param offset The offset into the array at which to start copying data. * @param length The number of bytes to write. * @throws IOException If an error occurs while writing to the underlying * stream. ***/ @Override public synchronized void write(byte buffer[], int offset, int length) throws IOException { while (length-- > 0) { write(buffer[offset++]); } }
/** * This method is not part of the JavaBeans model and is used by the * static methods in the org.apache.commons.io.Util class for efficiency. * It is invoked after a block of bytes to inform the listener of the * transfer. The CopyStreamAdapater will create a CopyStreamEvent * from the arguments and relay the event to all of its registered * listeners, listing itself as the source of the event. * @param totalBytesTransferred The total number of bytes transferred * so far by the copy operation. * @param bytesTransferred The number of bytes copied by the most recent * write. * @param streamSize The number of bytes in the stream being copied. * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if * the size is unknown. */ @Override public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { for (EventListener listener : internalListeners) { ((CopyStreamListener) (listener)).bytesTransferred( totalBytesTransferred, bytesTransferred, streamSize); } }
/*** * Writes a String to the output. * * @param string The String to write. * @throws IOException If an error occurs while writing to the underlying * output. ***/ @Override public void write(String string) throws IOException { write(string.toCharArray()); }
/*** * Writes part of a String to the output starting from a given offset. * * @param string The String to write. * @param offset The offset into the String at which to start copying data. * @param length The number of characters to write. * @throws IOException If an error occurs while writing to the underlying * output. ***/ @Override public void write(String string, int offset, int length) throws IOException { write(string.toCharArray(), offset, length); }