1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package com.sun.star.sdbcx.comp.hsqldb;
20 
21 @SuppressWarnings("ucd")
22 public class StorageAccess implements org.hsqldb.lib.Storage {
23     String key;
24     String name;
25     boolean readonly;
26     NativeStorageAccess access;
27     /** Creates a new instance of StorageAccess */
StorageAccess(String name,Boolean readonly,Object key)28     public StorageAccess(String name,Boolean readonly,Object key) throws java.io.IOException{
29         this.key = (String)key;
30         this.name = name;
31         this.readonly = readonly.booleanValue();
32         try {
33             access = new NativeStorageAccess(name,
34                     this.readonly ? "r" : "rw"
35                     ,key);
36         } catch(Exception ex1){
37             java.io.IOException ex2 = new java.io.IOException();
38             ex2.initCause(ex1);
39             throw ex2;
40         }
41     }
close()42     public void close() throws java.io.IOException{
43         access.close(name,key);
44     }
45 
getFilePointer()46     public long getFilePointer() throws java.io.IOException{
47         return access.getFilePointer(name,key);
48     }
49 
length()50     public long length() throws java.io.IOException{
51         return access.length(name,key);
52     }
53 
read()54     public int read() throws java.io.IOException{
55         return access.read(name,key);
56     }
57 
read(byte[] b, int off, int len)58     public void read(byte[] b, int off, int len) throws java.io.IOException{
59         access.read(name,key,b,off,len);
60     }
61 
62     // based on the same code that reads an int from the .data file in HSQLDB
readInt()63     public int readInt() throws java.io.IOException{
64         byte [] tmp = new byte [4];
65 
66         int count = access.read(name,key,tmp,0, 4);
67 
68         if (count != 4){
69             throw new java.io.IOException();
70         }
71 
72         count = 0;
73         int ch0 = tmp[count++] & 0xff;
74         int ch1 = tmp[count++] & 0xff;
75         int ch2 = tmp[count++] & 0xff;
76         int ch3 = tmp[count] & 0xff;
77 
78         return ((ch0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3));
79     }
80 
seek(long position)81     public void seek(long position) throws java.io.IOException{
82         access.seek(name,key,position);
83     }
84 
write(byte[] b, int offset, int length)85     public void write(byte[] b, int offset, int length) throws java.io.IOException{
86         access.write(name,key,b,offset,length);
87     }
88 
writeInt(int v)89     public void writeInt(int v) throws java.io.IOException{
90         byte [] oneByte = new byte [4];
91         oneByte[0] = (byte) ((v >>> 24) & 0xFF);
92         oneByte[1] = (byte) ((v >>> 16) & 0xFF);
93         oneByte[2] = (byte) ((v >>>  8) & 0xFF);
94         oneByte[3] = (byte) ((v >>>  0) & 0xFF);
95 
96         write(oneByte,0,4);
97     }
98 
isReadOnly()99     public boolean isReadOnly() {
100         return readonly;
101     }
102 
103     @SuppressWarnings("cast")
readLong()104     public long readLong() throws java.io.IOException {
105         return (((long) readInt()) << 32) + (((long) readInt()) & 0xFFFFFFFFL);
106     }
107 
wasNio()108     public boolean wasNio() {
109         return false;
110     }
111 
writeLong(long v)112     public void writeLong(long v) throws java.io.IOException {
113         byte [] oneByte = new byte [8];
114 
115         oneByte[0] = (byte) ((v >>> 56) & 0xFF);
116         oneByte[1] = (byte) ((v >>> 48) & 0xFF);
117         oneByte[2] = (byte) ((v >>> 40) & 0xFF);
118         oneByte[3] = (byte) ((v >>> 32) & 0xFF);
119         oneByte[4] = (byte) ((v >>> 24) & 0xFF);
120         oneByte[5] = (byte) ((v >>> 16) & 0xFF);
121         oneByte[6] = (byte) ((v >>>  8) & 0xFF);
122         oneByte[7] = (byte) ((v >>>  0) & 0xFF);
123 
124         write(oneByte,0,8);
125     }
126 }
127