1 21 package com.db4o.cs.messages; 22 23 import java.io.BufferedInputStream ; 24 import java.io.BufferedOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 29 import com.db4o.*; 30 import com.db4o.ext.Status; 31 import com.db4o.foundation.network.YapSocket; 32 33 public abstract class MsgBlob extends MsgD implements BlobStatus{ 34 35 public BlobImpl _blob; 36 int _currentByte; 37 int _length; 38 39 public double getStatus() { 40 if (_length != 0) { 41 return (double) _currentByte / (double) _length; 42 } 43 return Status.ERROR; 44 } 45 46 public abstract void processClient(YapSocket sock) throws IOException ; 47 48 BlobImpl serverGetBlobImpl() { 49 BlobImpl blobImpl = null; 50 int id = _payLoad.readInt(); 51 YapStream stream = stream(); 52 synchronized (stream.i_lock) { 53 blobImpl = (BlobImpl) stream.getByID1(transaction(), id); 54 stream.activate1(transaction(), blobImpl, 3); 55 } 56 return blobImpl; 57 } 58 59 protected void copy(YapSocket sock,OutputStream rawout,int length,boolean update) throws IOException { 60 BufferedOutputStream out = new BufferedOutputStream (rawout); 61 byte[] buffer=new byte[BlobImpl.COPYBUFFER_LENGTH]; 62 int totalread=0; 63 while(totalread<length) { 64 int stilltoread=length-totalread; 65 int readsize=(stilltoread<buffer.length ? stilltoread : buffer.length); 66 int curread=sock.read(buffer,0,readsize); 67 68 if(curread < 0){ 69 throw new IOException (); 70 } 71 72 out.write(buffer,0,curread); 73 totalread+=curread; 74 if(update) { 75 _currentByte+=curread; 76 } 77 } 78 out.flush(); 79 out.close(); 80 } 81 82 protected void copy(InputStream rawin,YapSocket sock,boolean update) throws IOException { 83 BufferedInputStream in = new BufferedInputStream (rawin); 84 byte[] buffer=new byte[BlobImpl.COPYBUFFER_LENGTH]; 85 int bytesread=-1; 86 while((bytesread=rawin.read(buffer))>=0) { 87 sock.write(buffer,0,bytesread); 88 if(update) { 89 _currentByte+=bytesread; 90 } 91 } 92 in.close(); 93 } 94 } 95 | Popular Tags |