com.google.api.client.util.StreamingContent Java Examples

The following examples show how to use com.google.api.client.util.StreamingContent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GZipEncoding.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void encode(StreamingContent content, OutputStream out) throws IOException {
  // must not close the underlying output stream
  OutputStream out2 =
      new BufferedOutputStream(out) {
        @Override
        public void close() throws IOException {
          // copy implementation of super.close(), except do not close the underlying output
          // stream
          try {
            flush();
          } catch (IOException ignored) {
          }
        }
      };
  GZIPOutputStream zipper = new GZIPOutputStream(out2);
  content.writeTo(zipper);
  // cannot call just zipper.finish() because that would cause a severe memory leak
  zipper.close();
}
 
Example #2
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public void write(OutputStream outputStream, StreamingContent content) throws IOException {
  try {
    Thread.sleep(sleepTimeInMs);
  } catch (InterruptedException e) {
    throw new IOException("sleep interrupted", e);
  }
}
 
Example #3
Source File: GoogleApacheHttpRequest.java    From PYX-Reloaded with Apache License 2.0 4 votes vote down vote up
ContentEntity(long contentLength, StreamingContent streamingContent) {
    this.contentLength = contentLength;
    this.streamingContent = Preconditions.checkNotNull(streamingContent);
}
 
Example #4
Source File: ContentEntity.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param contentLength content length or less than zero if not known
 * @param streamingContent streaming content
 */
ContentEntity(long contentLength, StreamingContent streamingContent) {
  this.contentLength = contentLength;
  this.streamingContent = Preconditions.checkNotNull(streamingContent);
}
 
Example #5
Source File: HttpEncodingStreamingContent.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param content streaming content
 * @param encoding HTTP encoding
 */
public HttpEncodingStreamingContent(StreamingContent content, HttpEncoding encoding) {
  this.content = Preconditions.checkNotNull(content);
  this.encoding = Preconditions.checkNotNull(encoding);
}
 
Example #6
Source File: HttpEncodingStreamingContent.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns the streaming content. */
public StreamingContent getContent() {
  return content;
}
 
Example #7
Source File: NetHttpRequest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public void write(OutputStream outputStream, final StreamingContent content)
    throws IOException {
  content.writeTo(outputStream);
}
 
Example #8
Source File: MultipartContent.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void writeTo(OutputStream out) throws IOException {
  Writer writer = new OutputStreamWriter(out, getCharset());
  String boundary = getBoundary();
  for (Part part : parts) {
    HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
    if (part.headers != null) {
      headers.fromHttpHeaders(part.headers);
    }
    headers
        .setContentEncoding(null)
        .setUserAgent(null)
        .setContentType(null)
        .setContentLength(null)
        .set("Content-Transfer-Encoding", null);
    // analyze the content
    HttpContent content = part.content;
    StreamingContent streamingContent = null;
    if (content != null) {
      headers.set("Content-Transfer-Encoding", Arrays.asList("binary"));
      headers.setContentType(content.getType());
      HttpEncoding encoding = part.encoding;
      long contentLength;
      if (encoding == null) {
        contentLength = content.getLength();
        streamingContent = content;
      } else {
        headers.setContentEncoding(encoding.getName());
        streamingContent = new HttpEncodingStreamingContent(content, encoding);
        contentLength = AbstractHttpContent.computeLength(content);
      }
      if (contentLength != -1) {
        headers.setContentLength(contentLength);
      }
    }
    // write multipart-body from RFC 1521 §7.2.1
    // write encapsulation
    // write delimiter
    writer.write(TWO_DASHES);
    writer.write(boundary);
    writer.write(NEWLINE);
    // write body-part; message from RFC 822 §4.1
    // write message fields
    HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
    if (streamingContent != null) {
      writer.write(NEWLINE);
      writer.flush();
      // write message text/body
      streamingContent.writeTo(out);
    }
    // terminate encapsulation
    writer.write(NEWLINE);
  }
  // write close-delimiter
  writer.write(TWO_DASHES);
  writer.write(boundary);
  writer.write(TWO_DASHES);
  writer.write(NEWLINE);
  writer.flush();
}
 
Example #9
Source File: ContentEntity.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param contentLength content length or less than zero if not known
 * @param streamingContent streaming content
 */
ContentEntity(long contentLength, StreamingContent streamingContent) {
  this.contentLength = contentLength;
  this.streamingContent = Preconditions.checkNotNull(streamingContent);
}
 
Example #10
Source File: MultipartFormDataContent.java    From Broadsheet.ie-Android with MIT License 4 votes vote down vote up
@Override
public void writeTo(OutputStream out) throws IOException {

    Writer writer = new OutputStreamWriter(out, getCharset());
    String boundary = getBoundary();

    for (Part part : parts) {
        HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
        if (part.headers != null) {
            headers.fromHttpHeaders(part.headers);
        }
        headers.setContentEncoding(null).setUserAgent(null).setContentType(null).setContentLength(null);
        // analyze the content
        HttpContent content = part.content;
        StreamingContent streamingContent = null;
        String contentDisposition = String.format("form-data; name=\"%s\"", part.name);
        if (part.filename != null) {
            headers.setContentType(content.getType());
            contentDisposition += String.format("; filename=\"%s\"", part.filename);
        }
        headers.set("Content-Disposition", contentDisposition);
        HttpEncoding encoding = part.encoding;
        if (encoding == null) {
            streamingContent = content;
        } else {
            headers.setContentEncoding(encoding.getName());
            streamingContent = new HttpEncodingStreamingContent(content, encoding);
        }
        // write separator
        writer.write(TWO_DASHES);
        writer.write(boundary);
        writer.write(NEWLINE);
        // write headers
        HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
        // write content
        if (streamingContent != null) {
            writer.write(NEWLINE);
            writer.flush();
            streamingContent.writeTo(out);
            writer.write(NEWLINE);
        }
    }
    // write end separator
    writer.write(TWO_DASHES);
    writer.write(boundary);
    writer.write(TWO_DASHES);
    writer.write(NEWLINE);
    writer.flush();
}
 
Example #11
Source File: LowLevelHttpRequest.java    From google-http-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the streaming content or {@code null} for no content.
 *
 * @throws IOException I/O exception
 * @since 1.14
 */
public final void setStreamingContent(StreamingContent streamingContent) throws IOException {
  this.streamingContent = streamingContent;
}
 
Example #12
Source File: LowLevelHttpRequest.java    From google-http-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the streaming content or {@code null} for no content.
 *
 * @since 1.14
 */
public final StreamingContent getStreamingContent() {
  return streamingContent;
}
 
Example #13
Source File: HttpEncoding.java    From google-http-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Encodes the streaming content into the output stream.
 *
 * <p>Implementations must not close the output stream, and instead should flush the output
 * stream. Some callers may assume that the output stream has not been closed, and will fail to
 * work if it has been closed.
 *
 * @param content streaming content
 * @param out output stream
 */
void encode(StreamingContent content, OutputStream out) throws IOException;
 
Example #14
Source File: NetHttpRequest.java    From google-http-java-client with Apache License 2.0 votes vote down vote up
void write(OutputStream outputStream, StreamingContent content) throws IOException;