xref: /AOO41X/main/connectivity/com/sun/star/sdbcx/comp/hsqldb/StorageAccess.java (revision c3ab0d6a971c09227d821facfabab1b8154c7921)
1*c3ab0d6aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c3ab0d6aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c3ab0d6aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c3ab0d6aSAndrew Rist  * distributed with this work for additional information
6*c3ab0d6aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c3ab0d6aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c3ab0d6aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c3ab0d6aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*c3ab0d6aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*c3ab0d6aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c3ab0d6aSAndrew Rist  * software distributed under the License is distributed on an
15*c3ab0d6aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c3ab0d6aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c3ab0d6aSAndrew Rist  * specific language governing permissions and limitations
18*c3ab0d6aSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*c3ab0d6aSAndrew Rist  *************************************************************/
21*c3ab0d6aSAndrew Rist 
22*c3ab0d6aSAndrew Rist 
23cdf0e10cSrcweir /*
24cdf0e10cSrcweir  * StorageAccess.java
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  * Created on 17. August 2004, 13:32
27cdf0e10cSrcweir  */
28cdf0e10cSrcweir 
29cdf0e10cSrcweir package com.sun.star.sdbcx.comp.hsqldb;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir  *
33cdf0e10cSrcweir  * @author  oj93728
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class StorageAccess implements org.hsqldb.lib.Storage {
37cdf0e10cSrcweir     String key;
38cdf0e10cSrcweir     String name;
39cdf0e10cSrcweir     boolean readonly;
40cdf0e10cSrcweir     NativeStorageAccess access;
41cdf0e10cSrcweir //	public SimpleLog appLog;
42cdf0e10cSrcweir     /** Creates a new instance of StorageAccess */
StorageAccess(String name,Boolean readonly,Object key)43cdf0e10cSrcweir     public StorageAccess(String name,Boolean readonly,Object key) throws java.io.IOException{
44cdf0e10cSrcweir         this.key = (String)key;
45cdf0e10cSrcweir         this.name = name;
46cdf0e10cSrcweir         this.readonly = readonly.booleanValue();
47cdf0e10cSrcweir         try {
48cdf0e10cSrcweir             access = new NativeStorageAccess(name,
49cdf0e10cSrcweir                     this.readonly ? "r" : "rw"
50cdf0e10cSrcweir                     ,key);
51cdf0e10cSrcweir         } catch(Exception e){
52cdf0e10cSrcweir             throw new java.io.IOException();
53cdf0e10cSrcweir         }
54cdf0e10cSrcweir 	//	appLog = new SimpleLog(name +".app3.log", true);
55cdf0e10cSrcweir     }
close()56cdf0e10cSrcweir     public void close() throws java.io.IOException{
57cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.close() ");
58cdf0e10cSrcweir 		//appLog.close();
59cdf0e10cSrcweir         access.close(name,key);
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
getFilePointer()62cdf0e10cSrcweir     public long getFilePointer() throws java.io.IOException{
63cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.getFilePointer() ");
64cdf0e10cSrcweir         return access.getFilePointer(name,key);
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
length()67cdf0e10cSrcweir     public long length() throws java.io.IOException{
68cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.length() ");
69cdf0e10cSrcweir         return access.length(name,key);
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
read()72cdf0e10cSrcweir     public int read() throws java.io.IOException{
73cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.read() ");
74cdf0e10cSrcweir         return access.read(name,key);
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
read(byte[] b, int off, int len)77cdf0e10cSrcweir     public void read(byte[] b, int off, int len) throws java.io.IOException{
78cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.read(" + b + ","+ off +","+len + ") ");
79cdf0e10cSrcweir         access.read(name,key,b,off,len);
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // fredt - this is based on the same code that reads an int from the .data file in HSQLDB
readInt()83cdf0e10cSrcweir     public int readInt() throws java.io.IOException{
84cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.readInt() ");
85cdf0e10cSrcweir         byte [] tmp = new byte [4];
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         int count = access.read(name,key,tmp,0, 4);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         if (count != 4){
90cdf0e10cSrcweir             throw new java.io.IOException();
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         count = 0;
94cdf0e10cSrcweir         int ch0 = tmp[count++] & 0xff;
95cdf0e10cSrcweir         int ch1 = tmp[count++] & 0xff;
96cdf0e10cSrcweir         int ch2 = tmp[count++] & 0xff;
97cdf0e10cSrcweir         int ch3 = tmp[count] & 0xff;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         return ((ch0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3));
100cdf0e10cSrcweir 
101cdf0e10cSrcweir /*
102cdf0e10cSrcweir         int ch [] = new int[4];
103cdf0e10cSrcweir         for(int i = 0;i < 4; ++i){
104cdf0e10cSrcweir             ch[i] = tmp[i];
105cdf0e10cSrcweir             if (ch[i] < 0 ){
106cdf0e10cSrcweir                 ch[i] = 256 + ch[i];
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
111cdf0e10cSrcweir 	    throw new java.io.IOException();
112cdf0e10cSrcweir 	return ((ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0));
113cdf0e10cSrcweir         //return access.readInt(name,key);
114cdf0e10cSrcweir */
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
seek(long position)117cdf0e10cSrcweir     public void seek(long position) throws java.io.IOException{
118cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.seek("+position +") ");
119cdf0e10cSrcweir         access.seek(name,key,position);
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
write(byte[] b, int offset, int length)122cdf0e10cSrcweir     public void write(byte[] b, int offset, int length) throws java.io.IOException{
123cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.write(" + b + "," + offset +","+length+") ");
124cdf0e10cSrcweir         access.write(name,key,b,offset,length);
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
writeInt(int v)127cdf0e10cSrcweir     public void writeInt(int v) throws java.io.IOException{
128cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.writeInt(" +v+") ");
129cdf0e10cSrcweir         byte [] oneByte = new byte [4];
130cdf0e10cSrcweir         oneByte[0] = (byte) ((v >>> 24) & 0xFF);
131cdf0e10cSrcweir         oneByte[1] = (byte) ((v >>> 16) & 0xFF);
132cdf0e10cSrcweir         oneByte[2] = (byte) ((v >>>  8) & 0xFF);
133cdf0e10cSrcweir         oneByte[3] = (byte) ((v >>>  0) & 0xFF);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         write(oneByte,0,4);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
isReadOnly()138cdf0e10cSrcweir     public boolean isReadOnly() {
139cdf0e10cSrcweir         return readonly;
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     // fredt - minor change of brackets
readLong()143cdf0e10cSrcweir     public long readLong() throws java.io.IOException {
144cdf0e10cSrcweir         // return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
145cdf0e10cSrcweir         return (((long) readInt()) << 32) + (((long) readInt()) & 0xFFFFFFFFL);
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
wasNio()148cdf0e10cSrcweir     public boolean wasNio() {
149cdf0e10cSrcweir         return false;
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
writeLong(long v)152cdf0e10cSrcweir     public void writeLong(long v) throws java.io.IOException {
153cdf0e10cSrcweir 		//appLog.sendLine("NIOScaledRAFile.writeLong(" +v+") ");
154cdf0e10cSrcweir         byte [] oneByte = new byte [8];
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         oneByte[0] = (byte) ((v >>> 56) & 0xFF);
157cdf0e10cSrcweir         oneByte[1] = (byte) ((v >>> 48) & 0xFF);
158cdf0e10cSrcweir         oneByte[2] = (byte) ((v >>> 40) & 0xFF);
159cdf0e10cSrcweir         oneByte[3] = (byte) ((v >>> 32) & 0xFF);
160cdf0e10cSrcweir         oneByte[4] = (byte) ((v >>> 24) & 0xFF);
161cdf0e10cSrcweir         oneByte[5] = (byte) ((v >>> 16) & 0xFF);
162cdf0e10cSrcweir         oneByte[6] = (byte) ((v >>>  8) & 0xFF);
163cdf0e10cSrcweir         oneByte[7] = (byte) ((v >>>  0) & 0xFF);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         write(oneByte,0,8);
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168