]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/ByteFileReader.java
PropertyMap.get does not return null if there is no mapping for the key
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / ByteFileReader.java
1 package org.simantics.db.common;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.ReadableByteChannel;
8
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.service.Bytes;
11
12 final public class ByteFileReader {
13
14         public static int BUFFER = 65536;
15
16         final private FileInputStream fs;
17         final private ReadableByteChannel channel;
18         final private ByteBuffer byteBuffer;
19         
20         final private byte[] bytes = new byte[BUFFER];
21         private int size;
22
23         protected int byteIndex = 0;
24
25         final protected ReadableByteChannel getChannel() {
26                 return channel;
27         }
28         
29         final protected ByteBuffer getByteBuffer() {
30                 return byteBuffer;
31         }
32
33         final protected byte[] getBytes() {
34                 return bytes;
35         }
36
37         final public byte[] readBytes(int amount) throws IOException {
38
39                 byte[] result = new byte[amount];
40                 
41                 int has = size-byteIndex;
42                 if(amount >= has) {
43                         ReadableByteChannel c = channel;
44                         System.arraycopy(bytes, byteIndex, result, 0, has);
45                         ByteBuffer bb2 = ByteBuffer.wrap(result);
46                         bb2.position(has);
47                         while(has < amount)
48                                 has += c.read(bb2);
49                         getSome();
50                 } else {
51                         System.arraycopy(bytes, byteIndex, result, 0, amount);
52                         byteIndex += amount;
53                 }
54
55                 return result;
56                 
57         }
58
59         final public void getSome() throws IOException {
60
61                 ReadableByteChannel c = channel;
62                 ByteBuffer bb = byteBuffer;
63                 bb.position(0);
64                 bb.limit(BUFFER);
65                 size = c.read(bb);
66                 if(size == 0) {
67                         long start = System.nanoTime();
68                         while(size == 0) {
69                                 if(System.nanoTime() - start > 10000000000L) throw new IOException("Timeout");
70                                 size = c.read(bb);
71                         }
72                 }
73                 bb.position(0);
74                 byteIndex = 0;
75                 
76         }
77         
78         final public int readShort() throws IOException {
79
80                 if(byteIndex >= (size-3)) {
81                         short result = 0;
82                         if(byteIndex == size) {
83                                 getSome();
84                         }
85                         result |= ((short)(bytes[byteIndex++]&0xff)<<0);
86                         if(byteIndex == size) {
87                                 getSome();
88                         }
89                         result |= ((short)(bytes[byteIndex++]&0xff)<<8);
90                         if(byteIndex == size) {
91                                 getSome();
92                         }
93                         return result;
94                 } else {
95                         int result = Bytes.readLE2(bytes, byteIndex);
96                         byteIndex += 2;
97                         return result;
98                 }
99                 
100         }
101         
102         final public int readInt() throws IOException {
103
104                 if(byteIndex >= (size-5)) {
105                         int result = 0;
106                         if(byteIndex == size) {
107                                 getSome();
108                         }
109                         result |= ((int)(bytes[byteIndex++]&0xff)<<0);
110                         if(byteIndex == size) {
111                                 getSome();
112                         }
113                         result |= ((int)(bytes[byteIndex++]&0xff)<<8);
114                         if(byteIndex == size) {
115                                 getSome();
116                         }
117                         result |= ((int)(bytes[byteIndex++]&0xff)<<16);
118                         if(byteIndex == size) {
119                                 getSome();
120                         }
121                         result |= ((int)(bytes[byteIndex++]&0xff)<<24);
122                         if(byteIndex == size) {
123                                 getSome();
124                         }
125                         return result;
126                 } else {
127                         int result = Bytes.readLE4(bytes, byteIndex);
128                         byteIndex += 4;
129                         return result;
130                 }
131                 
132         }
133         
134         final public long readLong() throws IOException {
135
136                 if(byteIndex >= (size-9)) {
137                         long result = 0;
138                         if(byteIndex == size) {
139                                 getSome();
140                         }
141                         result |= ((long)(bytes[byteIndex++]&0xff)<<0);
142                         if(byteIndex == size) {
143                                 getSome();
144                         }
145                         result |= ((long)(bytes[byteIndex++]&0xff)<<8);
146                         if(byteIndex == size) {
147                                 getSome();
148                         }
149                         result |= ((long)(bytes[byteIndex++]&0xff)<<16);
150                         if(byteIndex == size) {
151                                 getSome();
152                         }
153                         result |= ((long)(bytes[byteIndex++]&0xff)<<24);
154                         if(byteIndex == size) {
155                                 getSome();
156                         }
157                         result |= ((long)(bytes[byteIndex++]&0xff)<<32);
158                         if(byteIndex == size) {
159                                 getSome();
160                         }
161                         result |= ((long)(bytes[byteIndex++]&0xff)<<40);
162                         if(byteIndex == size) {
163                                 getSome();
164                         }
165                         result |= ((long)(bytes[byteIndex++]&0xff)<<48);
166                         if(byteIndex == size) {
167                                 getSome();
168                         }
169                         result |= ((long)(bytes[byteIndex++]&0xff)<<56);
170                         if(byteIndex == size) {
171                                 getSome();
172                         }
173                         return result;
174                 } else {
175                         long result = Bytes.readLE8(bytes, byteIndex);
176                         byteIndex += 8;
177                         return result;
178                 }
179                 
180         }
181
182         public void close() {
183                 try {
184                         fs.close();
185                 } catch (IOException e) {
186                         Logger.defaultLogError(e);
187                 }
188         }
189         
190         public ByteFileReader(File file) throws IOException {
191
192                 byteBuffer = ByteBuffer.wrap(bytes);
193
194                 fs = new FileInputStream(file);
195                 
196                 channel = fs.getChannel();
197                 
198                 this.size = channel.read(byteBuffer);
199                 
200                 byteBuffer.position(0);
201                 
202         }
203         
204 }