001: package com.knowgate.misc;
002:
003: import java.io.InputStream;
004: import java.io.Serializable;
005: import java.io.EOFException;
006:
007: import com.knowgate.debug.DebugFile;
008:
009: /*
010: * ByteStore.java
011: *
012: *
013: * Created: Sun Sep 19 17:22:13 1999
014: *
015: * Copyright (C) 1999-2000 Sebastian Schaffert
016: *
017: * This program is free software; you can redistribute it and/or
018: * modify it under the terms of the GNU General Public License
019: * as published by the Free Software Foundation; either version 2
020: * of the License, or (at your option) any later version.
021: *
022: * This program is distributed in the hope that it will be useful,
023: * but WITHOUT ANY WARRANTY; without even the implied warranty of
024: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
025: * GNU General Public License for more details.
026: *
027: * You should have received a copy of the GNU General Public License
028: * along with this program; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
030: */
031:
032: /**
033: *
034: * @author Sebastian Schaffert
035: * @version
036: */
037: public class ByteStore implements Serializable {
038:
039: byte[] bytes;
040:
041: String content_type = null;
042: String content_encoding = null;
043: String name;
044: String description = "";
045:
046: public ByteStore(byte[] b, String sContentType) {
047: bytes = b;
048: content_type = sContentType;
049: }
050:
051: public void setDescription(String s) {
052: description = s;
053: }
054:
055: public String getDescription() {
056: return description;
057: }
058:
059: public void setContentType(String s) {
060: content_type = s;
061: }
062:
063: public String getContentType() {
064: return content_type;
065: /*
066: if(content_type != null) {
067: return content_type;
068: } else {
069: content_type=WebMailServer.getStorage().getMimeType(name);
070: return content_type;
071: }
072: */
073: }
074:
075: public void setContentEncoding(String s) {
076: content_encoding = s;
077: }
078:
079: public String getContentEncoding() {
080: return content_encoding;
081: }
082:
083: public byte[] getBytes() {
084: return bytes;
085: }
086:
087: public void setName(String s) {
088: name = s;
089: }
090:
091: public String getName() {
092: return name;
093: }
094:
095: public int getSize() {
096: return bytes.length;
097: }
098:
099: /**
100: * Create a ByteStore out of an InputStream
101: */
102: public static ByteStore getBinaryFromIS(InputStream in,
103: int nr_bytes_to_read, String sContentType) {
104: byte[] s = new byte[nr_bytes_to_read + 100];
105: int count = 0;
106: int lastread = 0;
107: if (in != null) {
108: synchronized (in) {
109: while (count < s.length) {
110: try {
111: lastread = in.read(s, count, nr_bytes_to_read
112: - count);
113: } catch (EOFException ex) {
114: if (DebugFile.trace)
115: DebugFile.writeln("EOFException "
116: + ex.getMessage());
117: lastread = 0;
118: } catch (Exception z) {
119: if (DebugFile.trace)
120: DebugFile.writeln(z.getClass().getName()
121: + " " + z.getMessage());
122: lastread = 0;
123: }
124: count += lastread;
125: if (lastread < 1)
126: break;
127: }
128: }
129: byte[] s2 = new byte[count + 1];
130: for (int i = 0; i < count + 1; i++) {
131: s2[i] = s[i];
132: }
133: ByteStore d = new ByteStore(s2, sContentType);
134: return d;
135: } else {
136: return null;
137: }
138: }
139: } // ByteStore
|