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