View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package com.foxinmy.weixin4j.http.apache.mime;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import com.foxinmy.weixin4j.http.ContentType;
37  import com.foxinmy.weixin4j.http.entity.HttpEntity;
38  
39  class MultipartFormEntity implements HttpEntity {
40  
41  	private final AbstractMultipartForm multipart;
42  	private final ContentType contentType;
43  	private final long contentLength;
44  
45  	MultipartFormEntity(final AbstractMultipartForm multipart,
46  			final ContentType contentType, final long contentLength) {
47  		super();
48  		this.multipart = multipart;
49  		this.contentType = contentType;
50  		this.contentLength = contentLength;
51  	}
52  
53  	AbstractMultipartForm getMultipart() {
54  		return this.multipart;
55  	}
56  
57  	public boolean isRepeatable() {
58  		return this.contentLength != -1;
59  	}
60  
61  	public boolean isChunked() {
62  		return !isRepeatable();
63  	}
64  
65  	public boolean isStreaming() {
66  		return !isRepeatable();
67  	}
68  
69  	@Override
70  	public long getContentLength() {
71  		return this.contentLength;
72  	}
73  
74  	public ContentType getContentType() {
75  		return this.contentType;
76  	}
77  
78  	@Override
79  	public InputStream getContent() throws IOException {
80  		if (this.contentLength < 0) {
81  			throw new IllegalArgumentException("Content length is unknown");
82  		} else if (this.contentLength > 5 * 1024 * 1024) {
83  			throw new IllegalArgumentException("Content length is too long: "
84  					+ this.contentLength);
85  		}
86  		final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
87  		writeTo(outstream);
88  		outstream.flush();
89  		return new ByteArrayInputStream(outstream.toByteArray());
90  	}
91  
92  	@Override
93  	public void writeTo(final OutputStream outstream) throws IOException {
94  		this.multipart.writeTo(outstream);
95  	}
96  }