001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/StringEntity.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 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: import java.io.UnsupportedEncodingException;
039:
040: import org.apache.http.protocol.HTTP;
041:
042: /**
043: * An entity whose content is retrieved from a string.
044: *
045: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
046: *
047: * @version $Revision: 604625 $
048: *
049: * @since 4.0
050: */
051: public class StringEntity extends AbstractHttpEntity implements
052: Cloneable {
053:
054: protected final byte[] content;
055:
056: public StringEntity(final String s, String charset)
057: throws UnsupportedEncodingException {
058: super ();
059: if (s == null) {
060: throw new IllegalArgumentException(
061: "Source string may not be null");
062: }
063: if (charset == null) {
064: charset = HTTP.DEFAULT_CONTENT_CHARSET;
065: }
066: this .content = s.getBytes(charset);
067: setContentType(HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM
068: + charset);
069: }
070:
071: public StringEntity(final String s)
072: throws UnsupportedEncodingException {
073: this (s, null);
074: }
075:
076: public boolean isRepeatable() {
077: return true;
078: }
079:
080: public long getContentLength() {
081: return this .content.length;
082: }
083:
084: public InputStream getContent() throws IOException {
085: return new ByteArrayInputStream(this .content);
086: }
087:
088: public void writeTo(final OutputStream outstream)
089: throws IOException {
090: if (outstream == null) {
091: throw new IllegalArgumentException(
092: "Output stream may not be null");
093: }
094: outstream.write(this .content);
095: outstream.flush();
096: }
097:
098: /**
099: * Tells that this entity is not streaming.
100: *
101: * @return <code>false</code>
102: */
103: public boolean isStreaming() {
104: return false;
105: }
106:
107: public Object clone() throws CloneNotSupportedException {
108: return super .clone();
109: }
110:
111: } // class StringEntity
|