com.google.api.client.http
Class InputStreamContent

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

public final class InputStreamContent
extends AbstractInputStreamContent

Concrete implementation of AbstractInputStreamContent that simply handles the transfer of data from an input stream to an output stream. This should only be used for streams that can not be re-opened and retried. If you have a stream that it is possible to recreate please create a new subclass of AbstractInputStreamContent.

The AbstractInputStreamContent.type field is required. The input stream is guaranteed to be closed at the end of AbstractInputStreamContent.writeTo(OutputStream).

Sample use with a URL:

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

Since:
1.0
Author:
Yaniv Inbar

Field Summary
 InputStream inputStream
          Required input stream to read from.
 long length
          Content length or less than zero if not known.
 
Fields inherited from class com.google.api.client.http.AbstractInputStreamContent
encoding, type
 
Constructor Summary
InputStreamContent()
           
 
Method Summary
static void copy(InputStream inputStream, OutputStream outputStream)
          Deprecated. Scheduled for removal in version 1.5. Please use AbstractInputStreamContent.copy(java.io.InputStream, java.io.OutputStream) as an alternative.
protected  InputStream getInputStream()
          Return an input stream for the specific implementation type of AbstractInputStreamContent.
 long getLength()
          Returns the content length or less than zero if not known.
 boolean retrySupported()
          Returns whether or not retry is supported on this content type.
 void setByteArrayInput(byte[] content)
          Deprecated. Scheduled for removal in 1.5. Please use ByteArrayContent instead.
 void setFileInput(File file)
          Deprecated. Scheduled for removal in 1.5. Please use FileContent instead.
 
Methods inherited from class com.google.api.client.http.AbstractInputStreamContent
getEncoding, getType, writeTo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.

Constructor Detail

InputStreamContent

public InputStreamContent()
Method Detail

setFileInput

@Deprecated
public void setFileInput(File file)
                  throws FileNotFoundException
Deprecated. Scheduled for removal in 1.5. Please use FileContent instead.

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)
      throws FileNotFoundException {
    InputStreamContent content = new InputStreamContent();
    content.setFileInput(jpegFile);
    content.type = "image/jpeg";
    request.content = content;
  }
 
 

Throws:
FileNotFoundException

setByteArrayInput

@Deprecated
public void setByteArrayInput(byte[] content)
Deprecated. Scheduled for removal in 1.5. Please use ByteArrayContent instead.

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:

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


getLength

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


retrySupported

public boolean retrySupported()
Description copied from interface: HttpContent
Returns whether or not retry is supported on this content type. This is a backward incompatible change that affects versions prior to 1.4.


getInputStream

protected InputStream getInputStream()
Description copied from class: AbstractInputStreamContent
Return an input stream for the specific implementation type of AbstractInputStreamContent. If the specific implementation will return true for HttpContent.retrySupported() this should be a factory function which will create a new InputStream from the source data whenever invoked.

Specified by:
getInputStream in class AbstractInputStreamContent

copy

@Deprecated
public static void copy(InputStream inputStream,
                                   OutputStream outputStream)
                 throws IOException
Deprecated. Scheduled for removal in version 1.5. Please use AbstractInputStreamContent.copy(java.io.InputStream, java.io.OutputStream) as an alternative.

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.

Sample use:


    static void downloadMedia(HttpResponse response, File file)
        throws IOException {
      FileOutputStream out = new FileOutputStream(file);
      try {
        InputStreamContent.copy(response.getContent(), out);
      } finally {
        out.close();
      }
     }
 

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


Copyright © 2010-2011 Google. All Rights Reserved.