01: /*
02: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.romaframework.core.domain.type;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.Serializable;
22:
23: import org.romaframework.core.config.Destroyable;
24:
25: /**
26: * Generic class to handle input stream. Used on upload of file via web.
27: *
28: * @author Luca Garulli (luca.garulli@assetdata.it)
29: */
30: public class Stream implements Serializable, Destroyable {
31: private byte[] buffer;
32: private transient InputStream inputStream;
33: private int size;
34: private String fileName;
35: private String contentType;
36:
37: public Stream(InputStream inputStream, int size, String fileName,
38: String contentType) {
39: this .inputStream = inputStream;
40: this .size = size;
41: this .fileName = fileName;
42: this .contentType = contentType;
43: }
44:
45: /**
46: * Destroy children recursively
47: */
48: public void destroy() {
49: if (inputStream != null) {
50: try {
51: inputStream.close();
52: } catch (IOException e) {
53: }
54: inputStream = null;
55: }
56: }
57:
58: /**
59: * Load the stream content and store it in the internal buffer. After
60: * completition the stream is closed.
61: *
62: * @throws IOException
63: */
64: public void load() throws IOException {
65: if (buffer != null || inputStream == null)
66: // ALREADY LOADED OR NOT INITIALIZED
67: return;
68:
69: synchronized (inputStream) {
70: buffer = new byte[size];
71: inputStream.read(buffer, 0, size);
72: inputStream.close();
73: }
74: }
75:
76: public byte[] getBuffer() {
77: return buffer;
78: }
79:
80: public String getContentType() {
81: return contentType;
82: }
83:
84: public String getFileName() {
85: return fileName;
86: }
87:
88: public InputStream getInputStream() {
89: return inputStream;
90: }
91:
92: public int getSize() {
93: return size;
94: }
95:
96: public void setContentType(String contentType) {
97: this.contentType = contentType;
98: }
99: }
|