Package org.restlet.engine.io
Class IoUtils
- java.lang.Object
-
- org.restlet.engine.io.IoUtils
-
public class IoUtils extends java.lang.ObjectIO manipulation utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static intBUFFER_SIZEThe size to use when instantiating buffered items such as instances of theBufferedReaderclass.private static char[]HEXDIGITSSupport for byte to hexa conversions.static intTIMEOUT_MSThe number of milliseconds after which IO operation will time out.
-
Constructor Summary
Constructors Modifier Constructor Description privateIoUtils()Private constructor to ensure that the class acts as a true utility class i.e.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopy(java.io.InputStream inputStream, java.io.OutputStream outputStream)Copies an input stream to an output stream.static voidcopy(java.io.InputStream inputStream, java.io.RandomAccessFile randomAccessFile)Copies an input stream to a random access file.static voidcopy(java.io.Reader reader, java.io.Writer writer)Copies characters from a reader to a writer.static voidcopy(java.nio.channels.ReadableByteChannel readableChannel, java.nio.channels.WritableByteChannel writableChannel)Writes a readable channel to a writable channel.static booleandelete(java.io.File file)Deletes an individual file or an empty directory.static booleandelete(java.io.File file, boolean recursive)Deletes an individual file or a directory.static booleandelete(java.io.File file, boolean recursive, boolean garbageCollect)Deletes an individual file or a directory.static longexhaust(java.io.InputStream input)Exhaust the content of the representation by reading it and silently discarding anything read.static longgetAvailableSize(Representation representation)Returns the size effectively available.static java.nio.channels.ReadableByteChannelgetChannel(java.io.InputStream inputStream)Returns a readable byte channel based on a given input stream.static java.nio.channels.WritableByteChannelgetChannel(java.io.OutputStream outputStream)Returns a writable byte channel based on a given output stream.static java.nio.channels.ReadableByteChannelgetChannel(Representation representation)Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method.private static intgetProperty(java.lang.String name, int defaultValue)static java.io.ReadergetReader(java.io.InputStream stream, CharacterSet characterSet)Returns a reader from an input stream and a character set.static java.io.ReadergetReader(WriterRepresentation representation)Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.static java.io.InputStreamgetStream(java.io.Reader reader, CharacterSet characterSet)Returns an input stream based on a given character reader.static java.io.OutputStreamgetStream(java.io.Writer writer, CharacterSet characterSet)Returns an output stream based on a given writer.static java.io.InputStreamgetStream(java.nio.channels.ReadableByteChannel readableChannel)Returns an input stream based on a given readable byte channel.static java.io.OutputStreamgetStream(java.nio.channels.WritableByteChannel writableChannel)Returns an output stream based on a given writable byte channel.static java.io.InputStreamgetStream(Representation representation)Returns an input stream based on the given representation's content and its write(OutputStream) method.static java.lang.StringgetText(Representation representation)Converts the representation to a string value.static java.io.WritergetWriter(java.io.OutputStream outputStream, CharacterSet characterSet)Returns a writer to the given output stream, using the given character set for encoding to bytes.static booleanisBlocking(java.nio.channels.Channel channel)Indicates if the channel is in blocking mode.static voidrelease(java.nio.channels.Selector selector, java.nio.channels.SelectionKey selectionKey)Release the selection key, working around for bug #6403933.static byte[]toByteArray(char[] chars)Converts a char array into a byte array using the default character set.static byte[]toByteArray(char[] chars, java.lang.String charsetName)Converts a char array into a byte array using the provided character set.static char[]toCharArray(byte[] bytes)Converts a byte array into a character array using the default character set.static char[]toCharArray(byte[] bytes, java.lang.String charsetName)Converts a byte array into a character array using the default character set.static java.lang.StringtoHexString(byte[] byteArray)Converts a byte array into an hexadecimal string.static java.lang.StringtoString(java.io.InputStream inputStream)Converts an input stream to a string.
As this method uses the InputstreamReader class, the default character set is used for decoding the input stream.static java.lang.StringtoString(java.io.InputStream inputStream, CharacterSet characterSet)Converts an input stream to a string using the specified character set for decoding the input stream.static java.lang.StringtoString(java.io.Reader reader)Converts a reader to a string.
-
-
-
Field Detail
-
BUFFER_SIZE
public static final int BUFFER_SIZE
The size to use when instantiating buffered items such as instances of theBufferedReaderclass. It looks for the System property "org.restlet.engine.io.bufferSize" and if not defined, uses the "8192" default value.
-
HEXDIGITS
private static final char[] HEXDIGITS
Support for byte to hexa conversions.
-
TIMEOUT_MS
public static final int TIMEOUT_MS
The number of milliseconds after which IO operation will time out. It looks for the System property "org.restlet.engine.io.timeoutMs" and if not defined, uses the "60000" default value.
-
-
Method Detail
-
copy
public static void copy(java.io.InputStream inputStream, java.io.OutputStream outputStream) throws java.io.IOExceptionCopies an input stream to an output stream. When the reading is done, the input stream is closed.- Parameters:
inputStream- The input stream.outputStream- The output stream.- Throws:
java.io.IOException
-
copy
public static void copy(java.io.InputStream inputStream, java.io.RandomAccessFile randomAccessFile) throws java.io.IOExceptionCopies an input stream to a random access file. When the reading is done, the input stream is closed.- Parameters:
inputStream- The input stream.randomAccessFile- The random access file.- Throws:
java.io.IOException
-
copy
public static void copy(java.nio.channels.ReadableByteChannel readableChannel, java.nio.channels.WritableByteChannel writableChannel) throws java.io.IOExceptionWrites a readable channel to a writable channel.- Parameters:
readableChannel- The readable channel.writableChannel- The writable channel.- Throws:
java.io.IOException
-
copy
public static void copy(java.io.Reader reader, java.io.Writer writer) throws java.io.IOExceptionCopies characters from a reader to a writer. When the reading is done, the reader is closed.- Parameters:
reader- The reader.writer- The writer.- Throws:
java.io.IOException
-
delete
public static boolean delete(java.io.File file)
Deletes an individual file or an empty directory.- Parameters:
file- The individual file or directory to delete.- Returns:
- True if the deletion was successful.
-
delete
public static boolean delete(java.io.File file, boolean recursive)Deletes an individual file or a directory. A recursive deletion can be forced as well. Under Windows operating systems, the garbage collector will be invoked once before attempting to delete in order to prevent locking issues.- Parameters:
file- The individual file or directory to delete.recursive- Indicates if directory with content should be deleted recursively as well.- Returns:
- True if the deletion was successful or if the file or directory didn't exist.
-
delete
public static boolean delete(java.io.File file, boolean recursive, boolean garbageCollect)Deletes an individual file or a directory. A recursive deletion can be forced as well. The garbage collector can be run once before attempting to delete, to workaround lock issues under Windows operating systems.- Parameters:
file- The individual file or directory to delete.recursive- Indicates if directory with content should be deleted recursively as well.garbageCollect- Indicates if the garbage collector should be run.- Returns:
- True if the deletion was successful or if the file or directory didn't exist.
-
exhaust
public static long exhaust(java.io.InputStream input) throws java.io.IOExceptionExhaust the content of the representation by reading it and silently discarding anything read.- Parameters:
input- The input stream to exhaust.- Returns:
- The number of bytes consumed or -1 if unknown.
- Throws:
java.io.IOException
-
getAvailableSize
public static long getAvailableSize(Representation representation)
Returns the size effectively available. This returns the same value asRepresentation.getSize()if no range is defined, otherwise it returns the size of the range usingRange.getSize().- Parameters:
representation- The representation to evaluate.- Returns:
- The available size.
-
getChannel
public static java.nio.channels.ReadableByteChannel getChannel(java.io.InputStream inputStream) throws java.io.IOExceptionReturns a readable byte channel based on a given input stream. If it is supported by a file a read-only instance of FileChannel is returned.- Parameters:
inputStream- The input stream to convert.- Returns:
- A readable byte channel.
- Throws:
java.io.IOException
-
getChannel
public static java.nio.channels.WritableByteChannel getChannel(java.io.OutputStream outputStream)
Returns a writable byte channel based on a given output stream.- Parameters:
outputStream- The output stream.- Returns:
- A writable byte channel.
-
getChannel
public static java.nio.channels.ReadableByteChannel getChannel(Representation representation) throws java.io.IOException
Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method. Internally, it uses a writer thread and a pipe channel.- Parameters:
representation- the representation to get theOutputStreamfrom.- Returns:
- A readable byte channel.
- Throws:
java.io.IOException
-
getProperty
private static int getProperty(java.lang.String name, int defaultValue)
-
getReader
public static java.io.Reader getReader(java.io.InputStream stream, CharacterSet characterSet) throws java.io.UnsupportedEncodingExceptionReturns a reader from an input stream and a character set.- Parameters:
stream- The input stream.characterSet- The character set. May be null.- Returns:
- The equivalent reader.
- Throws:
java.io.UnsupportedEncodingException- if a character set is given, but not supported
-
getReader
public static java.io.Reader getReader(WriterRepresentation representation) throws java.io.IOException
Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.- Parameters:
representation- The representation to read from.- Returns:
- The character reader.
- Throws:
java.io.IOException
-
getStream
public static java.io.OutputStream getStream(java.io.Writer writer, CharacterSet characterSet)Returns an output stream based on a given writer.- Parameters:
writer- The writer.characterSet- The character set used to write on the output stream.- Returns:
- the output stream of the writer
-
getStream
public static java.io.InputStream getStream(java.nio.channels.ReadableByteChannel readableChannel)
Returns an input stream based on a given readable byte channel.- Parameters:
readableChannel- The readable byte channel.- Returns:
- An input stream based on a given readable byte channel.
-
getStream
public static java.io.InputStream getStream(java.io.Reader reader, CharacterSet characterSet)Returns an input stream based on a given character reader.- Parameters:
reader- The character reader.characterSet- The stream character set.- Returns:
- An input stream based on a given character reader.
-
getStream
public static java.io.InputStream getStream(Representation representation)
Returns an input stream based on the given representation's content and its write(OutputStream) method. Internally, it uses a writer thread and a pipe stream.- Parameters:
representation- the representation to get theOutputStreamfrom.- Returns:
- A stream with the representation's content.
-
getStream
public static java.io.OutputStream getStream(java.nio.channels.WritableByteChannel writableChannel)
Returns an output stream based on a given writable byte channel.- Parameters:
writableChannel- The writable byte channel.- Returns:
- An output stream based on a given writable byte channel.
-
getText
public static java.lang.String getText(Representation representation) throws java.io.IOException
Converts the representation to a string value. Be careful when using this method as the conversion of large content to a string fully stored in memory can result in OutOfMemoryErrors being thrown.- Parameters:
representation- The representation to convert.- Returns:
- The representation as a string value.
- Throws:
java.io.IOException
-
getWriter
public static java.io.Writer getWriter(java.io.OutputStream outputStream, CharacterSet characterSet)Returns a writer to the given output stream, using the given character set for encoding to bytes.- Parameters:
outputStream- The target output stream.characterSet- The character set for encoding.- Returns:
- The wrapping writer.
-
isBlocking
public static boolean isBlocking(java.nio.channels.Channel channel)
Indicates if the channel is in blocking mode. It returns false when the channel is selectable and configured to be non blocking.- Parameters:
channel- The channel to test.- Returns:
- True if the channel is in blocking mode.
-
release
public static void release(java.nio.channels.Selector selector, java.nio.channels.SelectionKey selectionKey) throws java.io.IOExceptionRelease the selection key, working around for bug #6403933.- Parameters:
selector- The associated selector.selectionKey- The used selection key.- Throws:
java.io.IOException
-
toByteArray
public static byte[] toByteArray(char[] chars)
Converts a char array into a byte array using the default character set.- Parameters:
chars- The source characters.- Returns:
- The result bytes.
-
toByteArray
public static byte[] toByteArray(char[] chars, java.lang.String charsetName)Converts a char array into a byte array using the provided character set.- Parameters:
chars- The source characters.charsetName- The character set to use.- Returns:
- The result bytes.
-
toCharArray
public static char[] toCharArray(byte[] bytes)
Converts a byte array into a character array using the default character set.- Parameters:
bytes- The source bytes.- Returns:
- The result characters.
-
toCharArray
public static char[] toCharArray(byte[] bytes, java.lang.String charsetName)Converts a byte array into a character array using the default character set.- Parameters:
bytes- The source bytes.charsetName- The character set to use.- Returns:
- The result characters.
-
toHexString
public static java.lang.String toHexString(byte[] byteArray)
Converts a byte array into an hexadecimal string.- Parameters:
byteArray- The byte array to convert.- Returns:
- The hexadecimal string.
-
toString
public static java.lang.String toString(java.io.InputStream inputStream)
Converts an input stream to a string.
As this method uses the InputstreamReader class, the default character set is used for decoding the input stream.- Parameters:
inputStream- The input stream.- Returns:
- The converted string.
- See Also:
InputStreamReader,toString(InputStream, CharacterSet)
-
toString
public static java.lang.String toString(java.io.InputStream inputStream, CharacterSet characterSet)Converts an input stream to a string using the specified character set for decoding the input stream. Once read, the input stream is closed.- Parameters:
inputStream- The input stream.characterSet- The character set- Returns:
- The converted string.
- See Also:
InputStreamReader
-
toString
public static java.lang.String toString(java.io.Reader reader)
Converts a reader to a string.- Parameters:
reader- The characters reader.- Returns:
- The converted string.
- See Also:
InputStreamReader
-
-