org.glassfish.jersey.message.internal
Class AbstractMessageReaderWriterProvider<T>

java.lang.Object
  extended by org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider<T>
Type Parameters:
T - Java type supported by the provider
All Implemented Interfaces:
MessageBodyReader<T>, MessageBodyWriter<T>
Direct Known Subclasses:
AbstractFormProvider, AbstractJaxbProvider, ByteArrayProvider, DataSourceProvider, DocumentProvider, FileProvider, InputStreamProvider, ReaderProvider, RenderedImageProvider

public abstract class AbstractMessageReaderWriterProvider<T>
extends Object
implements MessageBodyReader<T>, MessageBodyWriter<T>

Author:
Paul Sandoz

Field Summary
static Charset UTF8
          The UTF-8 Charset.
 
Constructor Summary
AbstractMessageReaderWriterProvider()
           
 
Method Summary
static Charset getCharset(MediaType m)
          Get the character set from a media type.
 long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
          Originally, the method has been called before writeTo to ascertain the length in bytes of the serialized form of t.
static String readFromAsString(InputStream in, MediaType type)
          Read the bytes of an input stream and convert to a string.
static void writeTo(InputStream in, OutputStream out)
          Reader bytes from an input stream and write then to an output stream.
static void writeTo(Reader in, Writer out)
          Reader characters from an input stream and write then 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.ws.rs.ext.MessageBodyReader
isReadable, readFrom
 
Methods inherited from interface javax.ws.rs.ext.MessageBodyWriter
isWriteable, writeTo
 

Field Detail

UTF8

public static final Charset UTF8
The UTF-8 Charset.

Constructor Detail

AbstractMessageReaderWriterProvider

public AbstractMessageReaderWriterProvider()
Method Detail

writeTo

public static void writeTo(InputStream in,
                           OutputStream out)
                    throws IOException
Reader bytes from an input stream and write then 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

public static void writeTo(Reader in,
                           Writer out)
                    throws IOException
Reader characters from an input stream and write then 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

public static Charset getCharset(MediaType m)
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

public static String readFromAsString(InputStream in,
                                      MediaType type)
                               throws IOException
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 charaters.
Returns:
the string.
Throws:
IOException - if there is an error reading from the input stream.

writeToAsString

public static void writeToAsString(String s,
                                   OutputStream out,
                                   MediaType type)
                            throws IOException
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

getSize

public long getSize(T t,
                    Class<?> type,
                    Type genericType,
                    Annotation[] annotations,
                    MediaType mediaType)
Description copied from interface: MessageBodyWriter
Originally, the method has been called before writeTo to ascertain the length in bytes of the serialized form of t. A non-negative return value has been used in a HTTP Content-Length header.

As of JAX-RS 2.0, the method has been deprecated and the value returned by the method is ignored by a JAX-RS runtime. All MessageBodyWriter implementations are advised to return -1 from the method. Responsibility to compute the actual Content-Length header value has been delegated to JAX-RS runtime.

Specified by:
getSize in interface MessageBodyWriter<T>
Parameters:
t - the instance to write
type - the class of instance that is to be written.
genericType - the type of instance to be written. GenericEntity provides a way to specify this information at runtime.
annotations - an array of the annotations attached to the message entity instance.
mediaType - the media type of the HTTP entity.
Returns:
length in bytes or -1 if the length cannot be determined in advance.


Copyright © 2007-2014, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.