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 MWriteBlob extends MsgBlob implements ServerSideMessage {
30:
31: public void processClient(Socket4 sock) throws IOException {
32: Msg message = Msg.readMessage(messageDispatcher(),
33: transaction(), sock);
34: if (message.equals(Msg.OK)) {
35: try {
36: _currentByte = 0;
37: _length = this ._blob.getLength();
38: _blob.getStatusFrom(this );
39: _blob.setStatus(Status.PROCESSING);
40: FileInputStream inBlob = this ._blob
41: .getClientInputStream();
42: copy(inBlob, sock, true);
43: sock.flush();
44: ObjectContainerBase stream = stream();
45: message = Msg.readMessage(messageDispatcher(),
46: transaction(), sock);
47: if (message.equals(Msg.OK)) {
48:
49: // make sure to load the filename to i_blob
50: // to allow client databasefile switching
51: stream.deactivate(transaction(), _blob,
52: Integer.MAX_VALUE);
53: stream.activate(transaction(), _blob,
54: Integer.MAX_VALUE);
55:
56: this ._blob.setStatus(Status.COMPLETED);
57: } else {
58: this ._blob.setStatus(Status.ERROR);
59: }
60:
61: } catch (Exception e) {
62: e.printStackTrace();
63: }
64: }
65: }
66:
67: public boolean processAtServer() {
68: try {
69: BlobImpl blobImpl = this .serverGetBlobImpl();
70: if (blobImpl != null) {
71: blobImpl.setTrans(transaction());
72: File file = blobImpl.serverFile(null, true);
73: Socket4 sock = serverMessageDispatcher().socket();
74: Msg.OK.write(sock);
75: FileOutputStream fout = new FileOutputStream(file);
76: copy(sock, fout, blobImpl.getLength(), false);
77: Msg.OK.write(sock);
78: }
79: } catch (Exception e) {
80: }
81: return true;
82: }
83: }
|