KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > io > SymbianIoAdapter


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.io;
22
23 import java.io.*;
24
25 /**
26  * Workaround for two I/O bugs in Symbian JDK versions:<br>
27  * - seek() cannot move beyond the current file length.<br>
28  * Fix: Write padding bytes up to the seek target if necessary<br>
29  * - Under certain (rare) conditions, calls to RAF.length() seems
30  * to garble up following reads.<br>
31  * Fix: Use a second RAF handle to the file for length() calls
32  * only.<br><br>
33  *
34  * <b>Usage:</b><br>
35  * Db4o.configure().io(new com.db4o.io.SymbianIoAdapter())<br><br>
36  *
37  * TODO:<br>
38  * - BasicClusterTest C/S fails (in AllTests context only)
39  *
40  * @sharpen.ignore
41  */

42 public class SymbianIoAdapter extends RandomAccessFileAdapter {
43     private byte[] _seekBytes=new byte[500];
44     private String JavaDoc _path;
45     private long _pos;
46     private long _length;
47     
48     protected SymbianIoAdapter(String JavaDoc path, boolean lockFile, long initialLength) throws IOException {
49         super(path, lockFile, initialLength);
50         _path=path;
51         _pos=0;
52         setLength();
53     }
54     
55     private void setLength() throws IOException {
56         _length=retrieveLength();
57     }
58
59         
60     private long retrieveLength() throws IOException {
61         RandomAccessFile file=new RandomAccessFile(_path,"r");
62         try {
63             return file.length();
64         } finally {
65             file.close();
66         }
67     }
68     
69     
70     public SymbianIoAdapter() {
71         super();
72     }
73
74     public IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength) throws IOException {
75         return new SymbianIoAdapter(path,lockFile,initialLength);
76     }
77
78     public long getLength() throws IOException {
79         setLength();
80         return _length;
81     }
82     
83     public int read(byte[] bytes, int length) throws IOException {
84         int ret=super.read(bytes, length);
85         _pos+=ret;
86         return ret;
87     }
88     
89     public void write(byte[] buffer, int length) throws IOException {
90         super.write(buffer, length);
91         _pos+=length;
92         if(_pos>_length) {
93             setLength();
94         }
95     }
96     
97     public void seek(long pos) throws IOException {
98         if (pos > _length) {
99             setLength();
100         }
101         if (pos > _length) {
102             int len = (int) (pos - _length);
103             super.seek(_length);
104             _pos=_length;
105             if (len < 500) {
106                 write(_seekBytes, len);
107             } else {
108                 write(new byte[len]);
109             }
110         }
111         super.seek(pos);
112         _pos=pos;
113     }
114 }
Popular Tags