001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.resource;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: import org.restlet.data.MediaType;
026: import org.restlet.util.ByteUtils;
027:
028: /**
029: * Transient representation based on a BIO input stream.
030: *
031: * @author Jerome Louvel (contact@noelios.com)
032: */
033: public class InputRepresentation extends StreamRepresentation {
034: /** The representation's stream. */
035: private InputStream inputStream;
036:
037: /**
038: * Constructor.
039: *
040: * @param inputStream
041: * The representation's stream.
042: * @param mediaType
043: * The representation's media type.
044: */
045: public InputRepresentation(InputStream inputStream,
046: MediaType mediaType) {
047: this (inputStream, mediaType, UNKNOWN_SIZE);
048: }
049:
050: /**
051: * Constructor.
052: *
053: * @param inputStream
054: * The representation's stream.
055: * @param mediaType
056: * The representation's media type.
057: * @param expectedSize
058: * The expected input stream size.
059: */
060: public InputRepresentation(InputStream inputStream,
061: MediaType mediaType, long expectedSize) {
062: super (mediaType);
063: setSize(expectedSize);
064: this .inputStream = inputStream;
065: setAvailable(inputStream != null);
066: setTransient(true);
067: }
068:
069: /**
070: * Returns a stream with the representation's content.
071: *
072: * @return A stream with the representation's content.
073: */
074: public synchronized InputStream getStream() throws IOException {
075: InputStream result = this .inputStream;
076: this .inputStream = null;
077: setAvailable(false);
078: return result;
079: }
080:
081: /**
082: * Converts the representation to a string value. Be careful when using this
083: * method as the conversion of large content to a string fully stored in
084: * memory can result in OutOfMemoryErrors being thrown.
085: *
086: * @return The representation as a string value.
087: */
088: public String getText() throws IOException {
089: return ByteUtils.toString(getStream(), this .getCharacterSet());
090: }
091:
092: /**
093: * Writes the representation to a byte stream.
094: *
095: * @param outputStream
096: * The output stream.
097: */
098: public void write(OutputStream outputStream) throws IOException {
099: ByteUtils.write(getStream(), outputStream);
100: }
101:
102: }
|