01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.internal.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.Socket4;
32: import com.db4o.internal.*;
33:
34: public abstract class MsgBlob extends MsgD implements BlobStatus {
35:
36: public BlobImpl _blob;
37: int _currentByte;
38: int _length;
39:
40: public double getStatus() {
41: if (_length != 0) {
42: return (double) _currentByte / (double) _length;
43: }
44: return Status.ERROR;
45: }
46:
47: public abstract void processClient(Socket4 sock) throws IOException;
48:
49: BlobImpl serverGetBlobImpl() {
50: BlobImpl blobImpl = null;
51: int id = _payLoad.readInt();
52: ObjectContainerBase stream = stream();
53: synchronized (stream._lock) {
54: blobImpl = (BlobImpl) stream.getByID(transaction(), id);
55: stream.activate(transaction(), blobImpl, 3);
56: }
57: return blobImpl;
58: }
59:
60: protected void copy(Socket4 sock, OutputStream rawout, int length,
61: boolean update) throws IOException {
62: BufferedOutputStream out = new BufferedOutputStream(rawout);
63: byte[] buffer = new byte[BlobImpl.COPYBUFFER_LENGTH];
64: int totalread = 0;
65: while (totalread < length) {
66: int stilltoread = length - totalread;
67: int readsize = (stilltoread < buffer.length ? stilltoread
68: : buffer.length);
69: int curread = sock.read(buffer, 0, readsize);
70:
71: if (curread < 0) {
72: throw new IOException();
73: }
74:
75: out.write(buffer, 0, curread);
76: totalread += curread;
77: if (update) {
78: _currentByte += curread;
79: }
80: }
81: out.flush();
82: out.close();
83: }
84:
85: protected void copy(InputStream rawin, Socket4 sock, boolean update)
86: throws IOException {
87: BufferedInputStream in = new BufferedInputStream(rawin);
88: byte[] buffer = new byte[BlobImpl.COPYBUFFER_LENGTH];
89: int bytesread = -1;
90: while ((bytesread = rawin.read(buffer)) >= 0) {
91: sock.write(buffer, 0, bytesread);
92: if (update) {
93: _currentByte += bytesread;
94: }
95: }
96: in.close();
97: }
98: }
|