Package org.restlet.engine.io
Class IoUtils
java.lang.Object
org.restlet.engine.io.IoUtils
IO manipulation utilities.
- Author:
- Thierry Boileau
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe size to use when instantiating buffered items such as instances of theBufferedReaderclass.static final intThe number of milliseconds after which IO operation will time out. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopy(InputStream inputStream, OutputStream outputStream) Copies an input stream to an output stream.static voidcopy(InputStream inputStream, RandomAccessFile randomAccessFile) Copies an input stream to a random access file.static voidCopies characters from a reader to a writer.static booleanDeletes an individual file or an empty directory.static booleanDeletes an individual file or a directory.static booleanDeletes an individual file or a directory.static longexhaust(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 ReadergetReader(InputStream stream, CharacterSet characterSet) Returns a reader from an input stream and a character set.static ReadergetReader(WriterRepresentation representation) Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.static InputStreamgetStream(Reader reader, CharacterSet characterSet) Returns an input stream based on a given character reader.static OutputStreamgetStream(Writer writer, CharacterSet characterSet) Returns an output stream based on a given writer.static InputStreamgetStream(Representation representation) Returns an input stream based on the given representation's content and its write(OutputStream) method.static StringgetText(Representation representation) Converts the representation to a string value.static WritergetWriter(OutputStream outputStream, CharacterSet characterSet) Returns a writer to the given output stream, using the given character set for encoding to bytes.static byte[]toByteArray(char[] chars) Converts a char array into a byte array using the default character set.static byte[]toByteArray(char[] chars, 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, String charsetName) Converts a byte array into a character array using the default character set.static StringtoHexString(byte[] byteArray) Converts a byte array into an hexadecimal string.static StringtoString(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 StringtoString(InputStream inputStream, CharacterSet characterSet) Converts an input stream to a string using the specified character set for decoding the input stream.static StringConverts a reader to a string.
-
Field Details
-
BUFFER_SIZE
public static final int BUFFER_SIZEThe 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. -
TIMEOUT_MS
public static final int TIMEOUT_MSThe 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 Details
-
copy
Copies 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:
IOException
-
copy
public static void copy(InputStream inputStream, RandomAccessFile randomAccessFile) throws IOException Copies 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:
IOException
-
copy
Copies characters from a reader to a writer. When the reading is done, the reader is closed.- Parameters:
reader- The reader.writer- The writer.- Throws:
IOException
-
delete
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
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
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
Exhaust 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:
IOException
-
getAvailableSize
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.
-
getReader
public static Reader getReader(InputStream stream, CharacterSet characterSet) throws UnsupportedEncodingException Returns 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:
UnsupportedEncodingException- if a character set is given, but not supported
-
getReader
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:
IOException
-
getStream
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
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
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.
-
getText
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:
IOException
-
getWriter
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.
-
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
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
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
Converts a byte array into an hexadecimal string.- Parameters:
byteArray- The byte array to convert.- Returns:
- The hexadecimal string.
-
toString
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:
-
toString
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:
-
toString
Converts a reader to a string.- Parameters:
reader- The characters reader.- Returns:
- The converted string.
- See Also:
-