001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/util/FileItem.java $
003: * $Id: FileItem.java 21413 2007-02-13 17:22:50Z jimeng@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.io.UnsupportedEncodingException;
027:
028: /**
029: * <p>
030: * FileItem is ...
031: * </p>
032: */
033: public class FileItem {
034: /** The chunk size used when streaming (100k). */
035: protected static final int STREAM_BUFFER_SIZE = 102400;
036:
037: /** Body stored in memory, filled in using this stream. */
038: protected ByteArrayOutputStream m_body = new ByteArrayOutputStream();
039:
040: protected byte[] m_bodyBytes = null;
041:
042: /** file name. */
043: protected String m_name = null;
044:
045: /** file type. */
046: protected String m_type = null;
047:
048: /** Stream from which body can be read */
049: protected InputStream m_inputStream;
050:
051: /**
052: * Construct
053: *
054: * @param fileName
055: * The file name.
056: * @param fileType
057: * The file type.
058: */
059: public FileItem(String fileName, String fileType) {
060: if (fileName != null)
061: m_name = fileName.trim();
062: if (fileType != null)
063: m_type = fileType.trim();
064: m_body = null;
065: m_bodyBytes = null;
066: m_inputStream = null;
067: }
068:
069: public FileItem(String fileName, String fileType, byte[] body) {
070: if (fileName != null)
071: m_name = fileName.trim();
072: if (fileType != null)
073: m_type = fileType.trim();
074: m_body = null;
075: m_bodyBytes = body;
076: m_inputStream = null;
077: }
078:
079: public FileItem(String fileName, String fileType, InputStream stream) {
080: if (fileName != null)
081: m_name = fileName.trim();
082: if (fileType != null)
083: m_type = fileType.trim();
084: m_body = null;
085: m_bodyBytes = null;
086: m_inputStream = stream;
087: }
088:
089: /**
090: */
091: public String getFileName() {
092: return m_name;
093: }
094:
095: /**
096: */
097: public String getContentType() {
098: return m_type;
099: }
100:
101: /**
102: * Access the body as a String.
103: */
104: public String getString() {
105: String rv = null;
106:
107: if (m_body == null && m_bodyBytes == null
108: && this .m_inputStream != null) {
109: stream2bodyBytes();
110: }
111:
112: try {
113: // this should give us byte for byte translation, no encoding/decoding
114: if (m_body != null) {
115: rv = m_body.toString("ISO8859_1");
116: } else {
117: rv = new String(m_bodyBytes, "ISO8859_1");
118: }
119: } catch (UnsupportedEncodingException ignore) {
120: }
121:
122: m_body = null;
123: m_bodyBytes = null;
124:
125: return rv;
126: }
127:
128: /**
129: * Access the body as a byte array. This consumes the entry.
130: */
131: public byte[] get() {
132: if (m_body == null && m_bodyBytes == null
133: && this .m_inputStream != null) {
134: stream2bodyBytes();
135: }
136:
137: if (m_body != null) {
138: byte[] content = m_body.toByteArray();
139: m_body = null;
140: return content;
141: } else {
142: byte[] content = m_bodyBytes;
143: m_bodyBytes = null;
144: return content;
145: }
146: }
147:
148: /**
149: * Access the input stream from which the body can be read.
150: * @return
151: */
152: public InputStream getInputStream() {
153: return this .m_inputStream;
154: }
155:
156: /**
157: */
158: OutputStream getOutputStream() {
159: return m_body;
160: }
161:
162: protected void stream2bodyBytes() {
163: if (this .m_inputStream != null) {
164: m_body = new ByteArrayOutputStream();
165:
166: // chunk
167: byte[] chunk = new byte[STREAM_BUFFER_SIZE];
168: int lenRead;
169: try {
170: while ((lenRead = this .m_inputStream.read(chunk)) != -1) {
171: m_body.write(chunk, 0, lenRead);
172: }
173: } catch (IOException ignoree) {
174: } finally {
175: if (m_inputStream != null) {
176: try {
177: m_inputStream.close();
178: m_inputStream = null;
179: } catch (IOException e) {
180: }
181: }
182: }
183:
184: }
185: }
186: }
|