com.google.api.client.http
Class AbstractInputStreamContent

java.lang.Object
  extended by com.google.api.client.http.AbstractInputStreamContent
All Implemented Interfaces:
HttpContent
Direct Known Subclasses:
ByteArrayContent, FileContent, InputStreamContent

public abstract class AbstractInputStreamContent
extends Object
implements HttpContent

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

The type field is required. Subclasses should implement the HttpContent.getLength(), getInputStream(), and HttpContent.retrySupported() for their specific type of input stream. If you need to limit the amount of content read from the input stream, you may use LimitInputStream.

Implementations don't need to be thread-safe.

Warning: in prior version 1.9 the maximum amount of content read from the input stream was limited by the HttpContent.getLength(), but now instead all content is read. You may use LimitInputStream if that functionality is needed.

Since:
1.4
Author:
[email protected] (Jacob Moshenko)

Constructor Summary
AbstractInputStreamContent(String type)
           
 
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.
static void copy(InputStream inputStream, OutputStream outputStream, boolean closeInputStream)
          Writes the content provided by the given source input stream into the given destination output stream.
 boolean getCloseInputStream()
          Returns whether the input stream should be closed at the end of writeTo(java.io.OutputStream).
 String getEncoding()
          Returns the content encoding (for example "gzip") or null for none.
abstract  InputStream getInputStream()
          Return an input stream for the specific implementation type of AbstractInputStreamContent.
 String getType()
          Returns the content type or null for none.
 AbstractInputStreamContent setCloseInputStream(boolean closeInputStream)
          Sets whether the input stream should be closed at the end of writeTo(java.io.OutputStream).
 AbstractInputStreamContent setEncoding(String encoding)
          Sets the content encoding (for example "gzip") or null for none.
 AbstractInputStreamContent setType(String type)
          Sets the content type or null for none.
 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
 
Methods inherited from interface com.google.api.client.http.HttpContent
getLength, retrySupported
 

Constructor Detail

AbstractInputStreamContent

public AbstractInputStreamContent(String type)
Parameters:
type - Content type or null for none
Since:
1.5
Method Detail

getInputStream

public abstract InputStream getInputStream()
                                    throws IOException
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.

Upgrade warning: in prior version 1.6 getInputStream() was protected, it is now public.

Throws:
IOException
Since:
1.7

writeTo

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

The recommendation for implementations is that they should not close the output stream. Callers should not assume whether or not the output stream has been closed. Implementations that do not close the output stream should flush it at the end of the method.

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

getType

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

Specified by:
getType in interface HttpContent

getCloseInputStream

public final boolean getCloseInputStream()
Returns whether the input stream should be closed at the end of writeTo(java.io.OutputStream). Default is true.

Since:
1.7

setEncoding

public AbstractInputStreamContent setEncoding(String encoding)
Sets the content encoding (for example "gzip") or null for none. Subclasses should override by calling super.

Since:
1.5

setType

public AbstractInputStreamContent setType(String type)
Sets the content type or null for none. Subclasses should override by calling super.

Since:
1.5

setCloseInputStream

public AbstractInputStreamContent setCloseInputStream(boolean closeInputStream)
Sets whether the input stream should be closed at the end of writeTo(java.io.OutputStream). Default is true. Subclasses should override by calling super.

Since:
1.7

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 this method.

Sample use:

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

Parameters:
inputStream - source input stream
outputStream - destination output stream
Throws:
IOException

copy

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

Sample use:

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

Parameters:
inputStream - source input stream
outputStream - destination output stream
closeInputStream - whether the input stream should be closed at the end of this method
Throws:
IOException
Since:
1.7


Copyright © 2011-2012 Google. All Rights Reserved.