KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > misc > ByteStore


1 package com.knowgate.misc;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.io.EOFException JavaDoc;
6
7 import com.knowgate.debug.DebugFile;
8
9 /*
10  * ByteStore.java
11  *
12  *
13  * Created: Sun Sep 19 17:22:13 1999
14  *
15  * Copyright (C) 1999-2000 Sebastian Schaffert
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */

31
32 /**
33  *
34  * @author Sebastian Schaffert
35  * @version
36  */

37 public class ByteStore implements Serializable JavaDoc {
38
39     byte[] bytes;
40
41     String JavaDoc content_type=null;
42     String JavaDoc content_encoding=null;
43     String JavaDoc name;
44     String JavaDoc description="";
45
46     public ByteStore(byte[] b, String JavaDoc sContentType) {
47     bytes=b;
48         content_type = sContentType;
49     }
50
51     public void setDescription(String JavaDoc s) {
52     description=s;
53     }
54
55     public String JavaDoc getDescription() {
56     return description;
57     }
58
59     public void setContentType(String JavaDoc s) {
60     content_type=s;
61     }
62
63     public String JavaDoc getContentType() {
64     return content_type;
65     /*
66     if(content_type != null) {
67         return content_type;
68     } else {
69         content_type=WebMailServer.getStorage().getMimeType(name);
70         return content_type;
71     }
72       */

73     }
74
75     public void setContentEncoding(String JavaDoc s) {
76     content_encoding=s;
77     }
78
79     public String JavaDoc getContentEncoding() {
80     return content_encoding;
81     }
82
83     public byte[] getBytes() {
84     return bytes;
85     }
86
87     public void setName(String JavaDoc s) {
88     name=s;
89     }
90
91     public String JavaDoc getName() {
92     return name;
93     }
94
95     public int getSize() {
96     return bytes.length;
97     }
98
99
100     /**
101      * Create a ByteStore out of an InputStream
102      */

103     public static ByteStore getBinaryFromIS(InputStream JavaDoc in, int nr_bytes_to_read, String JavaDoc 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-count);
112             } catch(EOFException JavaDoc ex) {
113                         if (DebugFile.trace) DebugFile.writeln("EOFException " + ex.getMessage());
114             lastread=0;
115             } catch(Exception JavaDoc z) {
116                         if (DebugFile.trace) DebugFile.writeln(z.getClass().getName() + " " + z.getMessage());
117             lastread=0;
118             }
119             count+=lastread;
120             if(lastread < 1) break;
121         }
122         }
123         byte[] s2=new byte[count+1];
124         for(int i=0; i<count+1;i++) {
125         s2[i]=s[i];
126         }
127         ByteStore d=new ByteStore(s2, sContentType);
128         return d;
129     } else {
130         return null;
131     }
132     }
133 } // ByteStore
134
Popular Tags