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.*;
24:
25: import com.db4o.ext.*;
26: import com.db4o.foundation.network.*;
27: import com.db4o.internal.*;
28:
29: public class MReadBlob extends MsgBlob implements ServerSideMessage {
30: public void processClient(Socket4 sock) throws IOException {
31: Msg message = Msg.readMessage(messageDispatcher(),
32: transaction(), sock);
33: if (message.equals(Msg.LENGTH)) {
34: try {
35: _currentByte = 0;
36: _length = message.payLoad().readInt();
37: _blob.getStatusFrom(this );
38: _blob.setStatus(Status.PROCESSING);
39: copy(sock, this ._blob.getClientOutputStream(), _length,
40: true);
41: message = Msg.readMessage(messageDispatcher(),
42: transaction(), sock);
43: if (message.equals(Msg.OK)) {
44: this ._blob.setStatus(Status.COMPLETED);
45: } else {
46: this ._blob.setStatus(Status.ERROR);
47: }
48: } catch (Exception e) {
49: }
50: } else if (message.equals(Msg.ERROR)) {
51: this ._blob.setStatus(Status.ERROR);
52: }
53:
54: }
55:
56: public boolean processAtServer() {
57: try {
58: BlobImpl blobImpl = this .serverGetBlobImpl();
59: if (blobImpl != null) {
60: blobImpl.setTrans(transaction());
61: File file = blobImpl.serverFile(null, false);
62: int length = (int) file.length();
63: Socket4 sock = serverMessageDispatcher().socket();
64: Msg.LENGTH.getWriterForInt(transaction(), length)
65: .write(sock);
66: FileInputStream fin = new FileInputStream(file);
67: copy(fin, sock, false);
68: sock.flush();
69: Msg.OK.write(sock);
70: }
71: } catch (Exception e) {
72: write(Msg.ERROR);
73: }
74: return true;
75: }
76: }
|