Class ReaderWriter
java.lang.Object
org.glassfish.jersey.message.internal.ReaderWriter
A utility class for reading and writing using byte and character streams.
If a byte or character array is utilized then the size of the array is by default the value of 8192. This value can be set using the system property "jersey.config.io.bufferSize".
- Author:
- Paul Sandoz
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The buffer size for arrays of byte and character.static final Charset
The UTF-8 Charset. -
Method Summary
Modifier and TypeMethodDescriptionstatic Charset
Get the character set from a media type.static String
readFromAsString
(InputStream in, MediaType type) Read the bytes of an input stream and convert to a string.static String
readFromAsString
(Reader reader) Read the characters of a reader and convert to a string.static void
safelyClose
(Closeable closeable) Safely close a closeable, without throwing an exception.static void
writeTo
(InputStream in, OutputStream out) Read bytes from an input stream and write them to an output stream.static void
Read characters from an input stream and write them to an output stream.static void
writeToAsString
(String s, OutputStream out, MediaType type) Convert a string to bytes and write those bytes to an output stream.
-
Field Details
-
UTF8
The UTF-8 Charset. -
BUFFER_SIZE
public static final int BUFFER_SIZEThe buffer size for arrays of byte and character.
-
-
Method Details
-
writeTo
Read bytes from an input stream and write them to an output stream.- Parameters:
in
- the input stream to read from.out
- the output stream to write to.- Throws:
IOException
- if there is an error reading or writing bytes.
-
writeTo
Read characters from an input stream and write them to an output stream.- Parameters:
in
- the reader to read from.out
- the writer to write to.- Throws:
IOException
- if there is an error reading or writing characters.
-
getCharset
Get the character set from a media type.The character set is obtained from the media type parameter "charset". If the parameter is not present the
UTF8
charset is utilized.- Parameters:
m
- the media type.- Returns:
- the character set.
-
readFromAsString
Read the bytes of an input stream and convert to a string.- Parameters:
in
- the input stream to read from.type
- the media type that determines the character set defining how to decode bytes to characters.- Returns:
- the string.
- Throws:
IOException
- if there is an error reading from the input stream.
-
readFromAsString
Read the characters of a reader and convert to a string.- Parameters:
reader
- the reader- Returns:
- the string
- Throws:
IOException
- if there is an error reading from the reader.
-
writeToAsString
Convert a string to bytes and write those bytes to an output stream.- Parameters:
s
- the string to convert to bytes.out
- the output stream to write to.type
- the media type that determines the character set defining how to decode bytes to characters.- Throws:
IOException
- in case of a write failure.
-
safelyClose
Safely close a closeable, without throwing an exception.- Parameters:
closeable
- object to be closed.
-