xref: /AOO41X/main/connectivity/com/sun/star/sdbcx/comp/hsqldb/StorageAccess.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 /*
28  * StorageAccess.java
29  *
30  * Created on 17. August 2004, 13:32
31  */
32 
33 package com.sun.star.sdbcx.comp.hsqldb;
34 
35 /**
36  *
37  * @author  oj93728
38  */
39 
40 public class StorageAccess implements org.hsqldb.lib.Storage {
41     String key;
42     String name;
43     boolean readonly;
44     NativeStorageAccess access;
45 //	public SimpleLog appLog;
46     /** Creates a new instance of StorageAccess */
47     public StorageAccess(String name,Boolean readonly,Object key) throws java.io.IOException{
48         this.key = (String)key;
49         this.name = name;
50         this.readonly = readonly.booleanValue();
51         try {
52             access = new NativeStorageAccess(name,
53                     this.readonly ? "r" : "rw"
54                     ,key);
55         } catch(Exception e){
56             throw new java.io.IOException();
57         }
58 	//	appLog = new SimpleLog(name +".app3.log", true);
59     }
60     public void close() throws java.io.IOException{
61 		//appLog.sendLine("NIOScaledRAFile.close() ");
62 		//appLog.close();
63         access.close(name,key);
64     }
65 
66     public long getFilePointer() throws java.io.IOException{
67 		//appLog.sendLine("NIOScaledRAFile.getFilePointer() ");
68         return access.getFilePointer(name,key);
69     }
70 
71     public long length() throws java.io.IOException{
72 		//appLog.sendLine("NIOScaledRAFile.length() ");
73         return access.length(name,key);
74     }
75 
76     public int read() throws java.io.IOException{
77 		//appLog.sendLine("NIOScaledRAFile.read() ");
78         return access.read(name,key);
79     }
80 
81     public void read(byte[] b, int off, int len) throws java.io.IOException{
82 		//appLog.sendLine("NIOScaledRAFile.read(" + b + ","+ off +","+len + ") ");
83         access.read(name,key,b,off,len);
84     }
85 
86     // fredt - this is based on the same code that reads an int from the .data file in HSQLDB
87     public int readInt() throws java.io.IOException{
88 		//appLog.sendLine("NIOScaledRAFile.readInt() ");
89         byte [] tmp = new byte [4];
90 
91         int count = access.read(name,key,tmp,0, 4);
92 
93         if (count != 4){
94             throw new java.io.IOException();
95         }
96 
97         count = 0;
98         int ch0 = tmp[count++] & 0xff;
99         int ch1 = tmp[count++] & 0xff;
100         int ch2 = tmp[count++] & 0xff;
101         int ch3 = tmp[count] & 0xff;
102 
103         return ((ch0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3));
104 
105 /*
106         int ch [] = new int[4];
107         for(int i = 0;i < 4; ++i){
108             ch[i] = tmp[i];
109             if (ch[i] < 0 ){
110                 ch[i] = 256 + ch[i];
111             }
112         }
113 
114 	if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
115 	    throw new java.io.IOException();
116 	return ((ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0));
117         //return access.readInt(name,key);
118 */
119     }
120 
121     public void seek(long position) throws java.io.IOException{
122 		//appLog.sendLine("NIOScaledRAFile.seek("+position +") ");
123         access.seek(name,key,position);
124     }
125 
126     public void write(byte[] b, int offset, int length) throws java.io.IOException{
127 		//appLog.sendLine("NIOScaledRAFile.write(" + b + "," + offset +","+length+") ");
128         access.write(name,key,b,offset,length);
129     }
130 
131     public void writeInt(int v) throws java.io.IOException{
132 		//appLog.sendLine("NIOScaledRAFile.writeInt(" +v+") ");
133         byte [] oneByte = new byte [4];
134         oneByte[0] = (byte) ((v >>> 24) & 0xFF);
135         oneByte[1] = (byte) ((v >>> 16) & 0xFF);
136         oneByte[2] = (byte) ((v >>>  8) & 0xFF);
137         oneByte[3] = (byte) ((v >>>  0) & 0xFF);
138 
139         write(oneByte,0,4);
140     }
141 
142     public boolean isReadOnly() {
143         return readonly;
144     }
145 
146     // fredt - minor change of brackets
147     public long readLong() throws java.io.IOException {
148         // return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
149         return (((long) readInt()) << 32) + (((long) readInt()) & 0xFFFFFFFFL);
150     }
151 
152     public boolean wasNio() {
153         return false;
154     }
155 
156     public void writeLong(long v) throws java.io.IOException {
157 		//appLog.sendLine("NIOScaledRAFile.writeLong(" +v+") ");
158         byte [] oneByte = new byte [8];
159 
160         oneByte[0] = (byte) ((v >>> 56) & 0xFF);
161         oneByte[1] = (byte) ((v >>> 48) & 0xFF);
162         oneByte[2] = (byte) ((v >>> 40) & 0xFF);
163         oneByte[3] = (byte) ((v >>> 32) & 0xFF);
164         oneByte[4] = (byte) ((v >>> 24) & 0xFF);
165         oneByte[5] = (byte) ((v >>> 16) & 0xFF);
166         oneByte[6] = (byte) ((v >>>  8) & 0xFF);
167         oneByte[7] = (byte) ((v >>>  0) & 0xFF);
168 
169         write(oneByte,0,8);
170     }
171 }
172