KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > messages > MsgBlob


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.cs.messages;
22
23 import java.io.BufferedInputStream JavaDoc;
24 import java.io.BufferedOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
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 JavaDoc;
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 JavaDoc rawout,int length,boolean update) throws IOException JavaDoc {
60         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(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 JavaDoc();
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 JavaDoc rawin,YapSocket sock,boolean update) throws IOException JavaDoc {
83         BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(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