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