Package org.apache.commons.net.io
Class Util
- java.lang.Object
-
- org.apache.commons.net.io.Util
-
public final class Util extends java.lang.ObjectThe Util class cannot be instantiated and stores short static convenience methods that are often quite useful.- See Also:
CopyStreamException,CopyStreamListener,CopyStreamAdapter
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_COPY_BUFFER_SIZEThe default buffer size (8192) used bycopyStreamandcopyReaderand by the copyReader/copyStream methods if a zero or negative buffer size is supplied.
-
Constructor Summary
Constructors Modifier Constructor Description privateUtil()Cannot be instantiated.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcloseQuietly(java.io.Closeable closeable)Deprecated.UseIOUtils.closeQuietly(Closeable).static voidcloseQuietly(java.net.Socket socket)Deprecated.UseIOUtils.closeQuietly(Socket).static longcopyReader(java.io.Reader source, java.io.Writer dest)Deprecated.UseIOUtils.copy(Reader, Writer).static longcopyReader(java.io.Reader source, java.io.Writer dest, int bufferSize)Copies the contents of a Reader to a Writer using a copy buffer of a given size.static longcopyReader(java.io.Reader source, java.io.Writer dest, int bufferSize, long streamSize, CopyStreamListener listener)Copies the contents of a Reader to a Writer using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination.static longcopyStream(java.io.InputStream source, java.io.OutputStream dest)Same ascopyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE);static longcopyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize)Copies the contents of an InputStream to an OutputStream using a copy buffer of a given size.static longcopyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener)Copies the contents of an InputStream to an OutputStream using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination.static longcopyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener, boolean flush)Copies the contents of an InputStream to an OutputStream using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination.static java.io.PrintWriternewPrintWriter(java.io.PrintStream printStream)Creates a new PrintWriter using the default encoding.
-
-
-
Field Detail
-
DEFAULT_COPY_BUFFER_SIZE
public static final int DEFAULT_COPY_BUFFER_SIZE
The default buffer size (8192) used bycopyStreamandcopyReaderand by the copyReader/copyStream methods if a zero or negative buffer size is supplied.- See Also:
- Constant Field Values
-
-
Method Detail
-
closeQuietly
@Deprecated public static void closeQuietly(java.io.Closeable closeable)
Deprecated.UseIOUtils.closeQuietly(Closeable).Closes the object quietly, catching rather than throwing IOException. Intended for use from finally blocks.- Parameters:
closeable- the object to close, may benull- Since:
- 3.0
-
closeQuietly
@Deprecated public static void closeQuietly(java.net.Socket socket)
Deprecated.UseIOUtils.closeQuietly(Socket).Closes the socket quietly, catching rather than throwing IOException. Intended for use from finally blocks.- Parameters:
socket- the socket to close, may benull- Since:
- 3.0
-
copyReader
@Deprecated public static long copyReader(java.io.Reader source, java.io.Writer dest) throws CopyStreamExceptionDeprecated.UseIOUtils.copy(Reader, Writer).Same ascopyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE);- Parameters:
source- where to copy fromdest- where to copy to- Returns:
- number of bytes copied
- Throws:
CopyStreamException- on error
-
copyReader
public static long copyReader(java.io.Reader source, java.io.Writer dest, int bufferSize) throws CopyStreamExceptionCopies 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 the method call. The number of characters read/written is returned.- Parameters:
source- The source Reader.dest- The destination writer.bufferSize- The number of characters to buffer during the copy. A zero or negative value means to useDEFAULT_COPY_BUFFER_SIZE.- Returns:
- 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.
-
copyReader
public static long copyReader(java.io.Reader source, java.io.Writer dest, int bufferSize, long streamSize, CopyStreamListener listener) throws CopyStreamExceptionCopies the contents of a Reader to a Writer using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination. If you wish to notify more than one listener you should use a CopyStreamAdapter as the listener and register the additional listeners with the CopyStreamAdapter.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 the method call. The number of characters read/written is returned.
- Parameters:
source- The source Reader.dest- The destination writer.bufferSize- The number of characters to buffer during the copy. A zero or negative value means to useDEFAULT_COPY_BUFFER_SIZE.streamSize- The number of characters in the stream being copied. Should be set to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown. Not currently used (though it is passed toCopyStreamListener.bytesTransferred(long, int, long)listener- The CopyStreamListener to notify of progress. If this parameter is null, notification is not attempted.- Returns:
- 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.
-
copyStream
public static long copyStream(java.io.InputStream source, java.io.OutputStream dest) throws CopyStreamExceptionSame ascopyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE);- Parameters:
source- where to copy fromdest- where to copy to- Returns:
- number of bytes copied
- Throws:
CopyStreamException- on error
-
copyStream
public static long copyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize) throws CopyStreamExceptionCopies the contents of an InputStream to an OutputStream using a copy buffer of a given size. The contents of the InputStream are read until the end of the stream is reached, but neither the source nor the destination are closed. You must do this yourself outside the method call. The number of bytes read/written is returned.- Parameters:
source- The source InputStream.dest- The destination OutputStream.bufferSize- The number of bytes to buffer during the copy. A zero or negative value means to useDEFAULT_COPY_BUFFER_SIZE.- Returns:
- The number of bytes 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.
-
copyStream
public static long copyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener) throws CopyStreamExceptionCopies the contents of an InputStream to an OutputStream using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination. If you wish to notify more than one listener you should use a CopyStreamAdapter as the listener and register the additional listeners with the CopyStreamAdapter.The contents of the InputStream are read until the end of the stream is reached, but neither the source nor the destination are closed. You must do this yourself outside the method call. The number of bytes read/written is returned.
- Parameters:
source- The source InputStream.dest- The destination OutputStream.bufferSize- The number of bytes to buffer during the copy. A zero or negative value means to useDEFAULT_COPY_BUFFER_SIZE.streamSize- The number of bytes in the stream being copied. Should be set to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown. Not currently used (though it is passed toCopyStreamListener.bytesTransferred(long, int, long)listener- The CopyStreamListener to notify of progress. If this parameter is null, notification is not attempted.- Returns:
- number of bytes read/written
- 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.
-
copyStream
public static long copyStream(java.io.InputStream source, java.io.OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener, boolean flush) throws CopyStreamExceptionCopies the contents of an InputStream to an OutputStream using a copy buffer of a given size and notifies the provided CopyStreamListener of the progress of the copy operation by calling its bytesTransferred(long, int) method after each write to the destination. If you wish to notify more than one listener you should use a CopyStreamAdapter as the listener and register the additional listeners with the CopyStreamAdapter.The contents of the InputStream are read until the end of the stream is reached, but neither the source nor the destination are closed. You must do this yourself outside the method call. The number of bytes read/written is returned.
- Parameters:
source- The source InputStream.dest- The destination OutputStream.bufferSize- The number of bytes to buffer during the copy. A zero or negative value means to useDEFAULT_COPY_BUFFER_SIZE.streamSize- The number of bytes in the stream being copied. Should be set to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown. Not currently used (though it is passed toCopyStreamListener.bytesTransferred(long, int, long)listener- The CopyStreamListener to notify of progress. If this parameter is null, notification is not attempted.flush- Whether to flush the output stream after every write. This is necessary for interactive sessions that rely on buffered streams. If you don't flush, the data will stay in the stream buffer.- Returns:
- number of bytes read/written
- 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.
-
newPrintWriter
public static java.io.PrintWriter newPrintWriter(java.io.PrintStream printStream)
Creates a new PrintWriter using the default encoding.- Parameters:
printStream- the target PrintStream.- Returns:
- a new PrintWriter.
- Since:
- 3.11.0
-
-