001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/BufferedHttpEntity.java $
003: * $Revision: 496070 $
004: * $Date: 2007-01-14 13:18:34 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.ByteArrayInputStream;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038:
039: import org.apache.http.HttpEntity;
040: import org.apache.http.util.EntityUtils;
041:
042: /**
043: * A wrapping entity that buffers it content if necessary.
044: * The buffered entity is always repeatable.
045: * If the wrapped entity is repeatable itself, calls are passed through.
046: * If the wrapped entity is not repeatable, the content is read into a
047: * buffer once and provided from there as often as required.
048: *
049: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
050: *
051: * @version $Revision: 496070 $
052: *
053: * @since 4.0
054: */
055: public class BufferedHttpEntity extends HttpEntityWrapper {
056:
057: private final byte[] buffer;
058:
059: public BufferedHttpEntity(final HttpEntity entity)
060: throws IOException {
061: super (entity);
062: if (!entity.isRepeatable() || entity.getContentLength() < 0) {
063: this .buffer = EntityUtils.toByteArray(entity);
064: } else {
065: this .buffer = null;
066: }
067: }
068:
069: public long getContentLength() {
070: if (this .buffer != null) {
071: return this .buffer.length;
072: } else {
073: return wrappedEntity.getContentLength();
074: }
075: }
076:
077: public InputStream getContent() throws IOException {
078: if (this .buffer != null) {
079: return new ByteArrayInputStream(this .buffer);
080: } else {
081: return wrappedEntity.getContent();
082: }
083: }
084:
085: /**
086: * Tells that this entity does not have to be chunked.
087: *
088: * @return <code>false</code>
089: */
090: public boolean isChunked() {
091: return (buffer == null) && wrappedEntity.isChunked();
092: }
093:
094: /**
095: * Tells that this entity is repeatable.
096: *
097: * @return <code>true</code>
098: */
099: public boolean isRepeatable() {
100: return true;
101: }
102:
103: public void writeTo(final OutputStream outstream)
104: throws IOException {
105: if (outstream == null) {
106: throw new IllegalArgumentException(
107: "Output stream may not be null");
108: }
109: if (this .buffer != null) {
110: outstream.write(this .buffer);
111: } else {
112: wrappedEntity.writeTo(outstream);
113: }
114: }
115:
116: // non-javadoc, see interface HttpEntity
117: public boolean isStreaming() {
118: return (buffer == null) && wrappedEntity.isStreaming();
119: }
120:
121: } // class BufferedHttpEntity
|