KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > BlobImpl


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import java.io.*;
24
25 import com.db4o.ext.*;
26 import com.db4o.types.*;
27
28 /**
29  * Transfer of blobs to and from the db4o system,
30  * if users use the Blob Db4oType.
31  *
32  * @moveto com.db4o.inside.blobs
33  * @exclude
34  */

35 public class BlobImpl implements Blob, Cloneable JavaDoc, Db4oTypeImpl {
36
37     public final static int COPYBUFFER_LENGTH=4096;
38     
39     public String JavaDoc fileName;
40     public String JavaDoc i_ext;
41     private transient File i_file;
42     private transient BlobStatus i_getStatusFrom;
43     public int i_length;
44     private transient double i_status = Status.UNUSED;
45     private transient YapStream i_stream;
46     private transient Transaction i_trans;
47
48     public int adjustReadDepth(int a_depth) {
49         return 1;
50     }
51     
52     public boolean canBind() {
53         return true;
54     }
55
56     private String JavaDoc checkExt(File file) {
57         String JavaDoc name = file.getName();
58         int pos = name.lastIndexOf(".");
59         if (pos > 0) {
60             i_ext = name.substring(pos);
61             return name.substring(0, pos);
62         }
63         
64         i_ext = "";
65         return name;
66         
67     }
68
69     private void copy(File from, File to) throws IOException {
70         to.delete();
71         BufferedInputStream in = new BufferedInputStream(new FileInputStream(from));
72         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
73         byte[] buffer=new byte[COPYBUFFER_LENGTH];
74         int bytesread=-1;
75         while ((bytesread=in.read(buffer))>=0) {
76             out.write(buffer,0,bytesread);
77         }
78         out.flush();
79         out.close();
80         in.close();
81     }
82
83     public Object JavaDoc createDefault(Transaction a_trans) {
84         BlobImpl bi = null;
85         try {
86             bi = (BlobImpl) this.clone();
87             bi.setTrans(a_trans);
88         } catch (CloneNotSupportedException JavaDoc e) {
89             return null;
90         }
91         return bi;
92     }
93
94     public FileInputStream getClientInputStream() throws Exception JavaDoc {
95         return new FileInputStream(i_file);
96     }
97
98     public FileOutputStream getClientOutputStream() throws Exception JavaDoc {
99         return new FileOutputStream(i_file);
100     }
101
102     public String JavaDoc getFileName() {
103         return fileName;
104     }
105
106     public int getLength() {
107         return i_length;
108     }
109
110     public double getStatus() {
111         if (i_status == Status.PROCESSING && i_getStatusFrom != null) {
112             return i_getStatusFrom.getStatus();
113         }
114         if (i_status == Status.UNUSED) {
115             if (i_length > 0) {
116                 i_status = Status.AVAILABLE;
117             }
118         }
119         return i_status;
120     }
121
122     public void getStatusFrom(BlobStatus from) {
123         i_getStatusFrom = from;
124     }
125
126     public boolean hasClassIndex() {
127         return false;
128     }
129
130     public void readFrom(File file) throws IOException {
131         if (!file.exists()) {
132             throw new IOException(Messages.get(41, file.getAbsolutePath()));
133         }
134         i_length = (int) file.length();
135         checkExt(file);
136         if (i_stream.isClient()) {
137             i_file = file;
138             ((BlobTransport)i_stream).readBlobFrom(i_trans, this, file);
139         } else {
140             readLocal(file);
141         }
142     }
143
144     public void readLocal(File file) throws IOException {
145         boolean copied = false;
146         if (fileName == null) {
147             File newFile = new File(serverPath(), file.getName());
148             if (!newFile.exists()) {
149                 copy(file, newFile);
150                 copied = true;
151                 fileName = newFile.getName();
152             }
153         }
154         if (!copied) {
155             copy(file, serverFile(checkExt(file), true));
156         }
157         synchronized (i_stream.i_lock) {
158             i_stream.setInternal(i_trans, this, false);
159         }
160         i_status = Status.COMPLETED;
161     }
162     
163     public void preDeactivate(){
164         // do nothing
165
}
166
167     public File serverFile(String JavaDoc promptName, boolean writeToServer) throws IOException {
168         synchronized (i_stream.i_lock) {
169             i_stream.activate1(i_trans, this, 2);
170         }
171         String JavaDoc path = serverPath();
172         i_stream.configImpl().ensureDirExists(path);
173         if (writeToServer) {
174             if (fileName == null) {
175                 if (promptName != null) {
176                     fileName = promptName;
177                 } else {
178                     fileName = "b_" + System.currentTimeMillis();
179                 }
180                 String JavaDoc tryPath = fileName + i_ext;
181                 int i = 0;
182                 while (new File(path, tryPath).exists()) {
183                     tryPath = fileName + "_" + i++ +i_ext;
184                     if (i == 99) {
185                         // should never happen
186
i_status = Status.ERROR;
187                         throw new IOException(Messages.get(40));
188                     }
189                 }
190                 fileName = tryPath;
191                 synchronized (i_stream.i_lock) {
192                     i_stream.setInternal(i_trans, this, false);
193                 }
194             }
195         } else {
196             if (fileName == null) {
197                 throw new IOException(Messages.get(38));
198             }
199         }
200         String JavaDoc lastTryPath = path + File.separator + fileName;
201         if (!writeToServer) {
202             if (!(new File(lastTryPath).exists())) {
203                 throw new IOException(Messages.get(39));
204             }
205         }
206         return new File(lastTryPath);
207     }
208
209     private String JavaDoc serverPath() throws IOException {
210         String JavaDoc path = i_stream.configImpl().blobPath();
211         if (path == null) {
212             path = "blobs";
213         }
214         i_stream.configImpl().ensureDirExists(path);
215         return path;
216     }
217
218     public void setStatus(double status) {
219         i_status = status;
220     }
221
222     public void setTrans(Transaction a_trans) {
223         i_trans = a_trans;
224         i_stream = a_trans.stream();
225     }
226
227     public void writeLocal(File file) throws IOException {
228         copy(serverFile(null, false), file);
229         i_status = Status.COMPLETED;
230     }
231
232     public void writeTo(File file) throws IOException {
233         if (getStatus() == Status.UNUSED) {
234             throw new IOException(Messages.get(43));
235         }
236         if (i_stream.isClient()) {
237             i_file = file;
238             i_status = Status.QUEUED;
239             ((BlobTransport)i_stream).writeBlobTo(i_trans, this, file);
240         } else {
241             writeLocal(file);
242         }
243     }
244     
245     public void replicateFrom(Object JavaDoc obj) {
246         // do nothing
247
}
248     
249     public Object JavaDoc storedTo(Transaction a_trans){
250         return this;
251     }
252     
253     public void setYapObject(YapObject a_yapObject) {
254         // not necessary
255
}
256
257  
258 }
Popular Tags