1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/entity/EntitySerializer.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.entity; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.OutputStream; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.HttpEntity; 1.37 +import ch.boye.httpclientandroidlib.HttpException; 1.38 +import ch.boye.httpclientandroidlib.HttpMessage; 1.39 +import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; 1.40 +import ch.boye.httpclientandroidlib.impl.io.ChunkedOutputStream; 1.41 +import ch.boye.httpclientandroidlib.impl.io.ContentLengthOutputStream; 1.42 +import ch.boye.httpclientandroidlib.impl.io.IdentityOutputStream; 1.43 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; 1.44 + 1.45 +/** 1.46 + * HTTP entity serializer. 1.47 + * <p> 1.48 + * This entity serializer currently supports "chunked" and "identitiy" 1.49 + * transfer-coding and content length delimited content. 1.50 + * <p> 1.51 + * This class relies on a specific implementation of 1.52 + * {@link ContentLengthStrategy} to determine the content length or transfer 1.53 + * encoding of the entity. 1.54 + * <p> 1.55 + * This class writes out the content of {@link HttpEntity} to the data stream 1.56 + * using a transfer coding based on properties on the HTTP message. 1.57 + * 1.58 + * @since 4.0 1.59 + */ 1.60 +public class EntitySerializer { 1.61 + 1.62 + private final ContentLengthStrategy lenStrategy; 1.63 + 1.64 + public EntitySerializer(final ContentLengthStrategy lenStrategy) { 1.65 + super(); 1.66 + if (lenStrategy == null) { 1.67 + throw new IllegalArgumentException("Content length strategy may not be null"); 1.68 + } 1.69 + this.lenStrategy = lenStrategy; 1.70 + } 1.71 + 1.72 + /** 1.73 + * Creates a transfer codec based on properties of the given HTTP message 1.74 + * and returns {@link OutputStream} instance that transparently encodes 1.75 + * output data as it is being written out to the output stream. 1.76 + * <p> 1.77 + * This method is called by the public 1.78 + * {@link #serialize(SessionOutputBuffer, HttpMessage, HttpEntity)}. 1.79 + * 1.80 + * @param outbuffer the session output buffer. 1.81 + * @param message the HTTP message. 1.82 + * @return output stream. 1.83 + * @throws HttpException in case of HTTP protocol violation. 1.84 + * @throws IOException in case of an I/O error. 1.85 + */ 1.86 + protected OutputStream doSerialize( 1.87 + final SessionOutputBuffer outbuffer, 1.88 + final HttpMessage message) throws HttpException, IOException { 1.89 + long len = this.lenStrategy.determineLength(message); 1.90 + if (len == ContentLengthStrategy.CHUNKED) { 1.91 + return new ChunkedOutputStream(outbuffer); 1.92 + } else if (len == ContentLengthStrategy.IDENTITY) { 1.93 + return new IdentityOutputStream(outbuffer); 1.94 + } else { 1.95 + return new ContentLengthOutputStream(outbuffer, len); 1.96 + } 1.97 + } 1.98 + 1.99 + /** 1.100 + * Writes out the content of the given HTTP entity to the session output 1.101 + * buffer based on properties of the given HTTP message. 1.102 + * 1.103 + * @param outbuffer the output session buffer. 1.104 + * @param message the HTTP message. 1.105 + * @param entity the HTTP entity to be written out. 1.106 + * @throws HttpException in case of HTTP protocol violation. 1.107 + * @throws IOException in case of an I/O error. 1.108 + */ 1.109 + public void serialize( 1.110 + final SessionOutputBuffer outbuffer, 1.111 + final HttpMessage message, 1.112 + final HttpEntity entity) throws HttpException, IOException { 1.113 + if (outbuffer == null) { 1.114 + throw new IllegalArgumentException("Session output buffer may not be null"); 1.115 + } 1.116 + if (message == null) { 1.117 + throw new IllegalArgumentException("HTTP message may not be null"); 1.118 + } 1.119 + if (entity == null) { 1.120 + throw new IllegalArgumentException("HTTP entity may not be null"); 1.121 + } 1.122 + OutputStream outstream = doSerialize(outbuffer, message); 1.123 + entity.writeTo(outstream); 1.124 + outstream.close(); 1.125 + } 1.126 + 1.127 +}