KickJava   Java API By Example, From Geeks To Geeks.

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


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