com.google.api.client.http
Class InputStreamContent

java.lang.Object
  extended by com.google.api.client.http.InputStreamContent
All Implemented Interfaces:
HttpContent

public final class InputStreamContent
extends Object
implements HttpContent

Serializes HTTP request content from an input stream into an output stream.

The type and inputStream fields are required. The input stream is guaranteed to be closed at the end of writeTo(OutputStream).

For a file input, use setFileInput(File), and for a byte array or string input use setByteArrayInput(byte[]).

Sample use with a URL:

 
 private static void setRequestJpegContent(HttpRequest request, URL jpegUrl) {
   InputStreamContent content = new InputStreamContent();
   content.inputStream = jpegUrl.openStream();
   content.type = "image/jpeg";
   request.content = content;
 }
 
 

Since:
1.0
Author:
Yaniv Inbar

Field Summary
 String encoding
          Content encoding (for example "gzip") or null for none.
 InputStream inputStream
          Required input stream to read from.
 long length
          Content length or less than zero if not known.
 String type
          Required content type.
 
Constructor Summary
InputStreamContent()
           
 
Method Summary
static void copy(InputStream inputStream, OutputStream outputStream)
          Writes the content provided by the given source input stream into the given destination output stream.
 String getEncoding()
          Returns the content encoding (for example "gzip") or null for none.
 long getLength()
          Returns the content length or less than zero if not known.
 String getType()
          Returns the content type.
 void setByteArrayInput(byte[] content)
          Sets the inputStream and length from the given byte array.
 void setFileInput(File file)
          Sets the inputStream from a file input stream based on the given file, and the length based on the file's length.
 void writeTo(OutputStream out)
          Writes the content to the given output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

type

public String type
Required content type.


length

public long length
Content length or less than zero if not known. Defaults to -1.


inputStream

public InputStream inputStream
Required input stream to read from.


encoding

public String encoding
Content encoding (for example "gzip") or null for none.

Constructor Detail

InputStreamContent

public InputStreamContent()
Method Detail

setFileInput

public void setFileInput(File file)
                  throws FileNotFoundException
Sets the inputStream from a file input stream based on the given file, and the length based on the file's length.

Sample use:

 
 private static void setRequestJpegContent(HttpRequest request, File jpegFile) {
   InputStreamContent content = new InputStreamContent();
   content.setFileInput(jpegFile);
   content.type = "image/jpeg";
   request.content = content;
 }
 
 

Throws:
FileNotFoundException

setByteArrayInput

public void setByteArrayInput(byte[] content)
Sets the inputStream and length from the given byte array.

For string input, call the appropriate String.getBytes(int, int, byte[], int) method.

Sample use:

 
 private static void setRequestJsonContent(HttpRequest request, String json) {
   InputStreamContent content = new InputStreamContent();
   content.setByteArrayInput(json.getBytes());
   content.type = "application/json";
   request.content = content;
 }
 
 


writeTo

public void writeTo(OutputStream out)
             throws IOException
Description copied from interface: HttpContent
Writes the content to the given output stream.

Specified by:
writeTo in interface HttpContent
Throws:
IOException

getEncoding

public String getEncoding()
Description copied from interface: HttpContent
Returns the content encoding (for example "gzip") or null for none.

Specified by:
getEncoding in interface HttpContent

getLength

public long getLength()
Description copied from interface: HttpContent
Returns the content length or less than zero if not known.

Specified by:
getLength in interface HttpContent

getType

public String getType()
Description copied from interface: HttpContent
Returns the content type.

Specified by:
getType in interface HttpContent

copy

public static void copy(InputStream inputStream,
                        OutputStream outputStream)
                 throws IOException
Writes the content provided by the given source input stream into the given destination output stream.

The input stream is guaranteed to be closed at the end of the method.

Parameters:
inputStream - source input stream
outputStream - destination output stream
Throws:
IOException - I/O exception


Copyright © 2010 Google. All Rights Reserved.