1 21 package com.db4o; 22 23 import java.io.*; 24 25 import com.db4o.ext.*; 26 import com.db4o.types.*; 27 28 35 public class BlobImpl implements Blob, Cloneable , Db4oTypeImpl { 36 37 public final static int COPYBUFFER_LENGTH=4096; 38 39 public String fileName; 40 public String 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 checkExt(File file) { 57 String 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 createDefault(Transaction a_trans) { 84 BlobImpl bi = null; 85 try { 86 bi = (BlobImpl) this.clone(); 87 bi.setTrans(a_trans); 88 } catch (CloneNotSupportedException e) { 89 return null; 90 } 91 return bi; 92 } 93 94 public FileInputStream getClientInputStream() throws Exception { 95 return new FileInputStream(i_file); 96 } 97 98 public FileOutputStream getClientOutputStream() throws Exception { 99 return new FileOutputStream(i_file); 100 } 101 102 public String 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 } 166 167 public File serverFile(String promptName, boolean writeToServer) throws IOException { 168 synchronized (i_stream.i_lock) { 169 i_stream.activate1(i_trans, this, 2); 170 } 171 String 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 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 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 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 serverPath() throws IOException { 210 String 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 obj) { 246 } 248 249 public Object storedTo(Transaction a_trans){ 250 return this; 251 } 252 253 public void setYapObject(YapObject a_yapObject) { 254 } 256 257 258 } | Popular Tags |