1 package org.simantics.db.common;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.ReadableByteChannel;
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.service.Bytes;
12 final public class ByteFileReader {
14 public static int BUFFER = 65536;
16 final private FileInputStream fs;
17 final private ReadableByteChannel channel;
18 final private ByteBuffer byteBuffer;
20 final private byte[] bytes = new byte[BUFFER];
23 protected int byteIndex = 0;
25 final protected ReadableByteChannel getChannel() {
29 final protected ByteBuffer getByteBuffer() {
33 final protected byte[] getBytes() {
37 final public byte[] readBytes(int amount) throws IOException {
39 byte[] result = new byte[amount];
41 int has = size-byteIndex;
43 ReadableByteChannel c = channel;
44 System.arraycopy(bytes, byteIndex, result, 0, has);
45 ByteBuffer bb2 = ByteBuffer.wrap(result);
51 System.arraycopy(bytes, byteIndex, result, 0, amount);
59 final public void getSome() throws IOException {
61 ReadableByteChannel c = channel;
62 ByteBuffer bb = byteBuffer;
67 long start = System.nanoTime();
69 if(System.nanoTime() - start > 10000000000L) throw new IOException("Timeout");
78 final public int readShort() throws IOException {
80 if(byteIndex >= (size-3)) {
82 if(byteIndex == size) {
85 result |= ((short)(bytes[byteIndex++]&0xff)<<0);
86 if(byteIndex == size) {
89 result |= ((short)(bytes[byteIndex++]&0xff)<<8);
90 if(byteIndex == size) {
95 int result = Bytes.readLE2(bytes, byteIndex);
102 final public int readInt() throws IOException {
104 if(byteIndex >= (size-5)) {
106 if(byteIndex == size) {
109 result |= ((int)(bytes[byteIndex++]&0xff)<<0);
110 if(byteIndex == size) {
113 result |= ((int)(bytes[byteIndex++]&0xff)<<8);
114 if(byteIndex == size) {
117 result |= ((int)(bytes[byteIndex++]&0xff)<<16);
118 if(byteIndex == size) {
121 result |= ((int)(bytes[byteIndex++]&0xff)<<24);
122 if(byteIndex == size) {
127 int result = Bytes.readLE4(bytes, byteIndex);
134 final public long readLong() throws IOException {
136 if(byteIndex >= (size-9)) {
138 if(byteIndex == size) {
141 result |= ((long)(bytes[byteIndex++]&0xff)<<0);
142 if(byteIndex == size) {
145 result |= ((long)(bytes[byteIndex++]&0xff)<<8);
146 if(byteIndex == size) {
149 result |= ((long)(bytes[byteIndex++]&0xff)<<16);
150 if(byteIndex == size) {
153 result |= ((long)(bytes[byteIndex++]&0xff)<<24);
154 if(byteIndex == size) {
157 result |= ((long)(bytes[byteIndex++]&0xff)<<32);
158 if(byteIndex == size) {
161 result |= ((long)(bytes[byteIndex++]&0xff)<<40);
162 if(byteIndex == size) {
165 result |= ((long)(bytes[byteIndex++]&0xff)<<48);
166 if(byteIndex == size) {
169 result |= ((long)(bytes[byteIndex++]&0xff)<<56);
170 if(byteIndex == size) {
175 long result = Bytes.readLE8(bytes, byteIndex);
182 public void close() {
185 } catch (IOException e) {
186 Logger.defaultLogError(e);
190 public ByteFileReader(File file) throws IOException {
192 byteBuffer = ByteBuffer.wrap(bytes);
194 fs = new FileInputStream(file);
196 channel = fs.getChannel();
198 this.size = channel.read(byteBuffer);
200 byteBuffer.position(0);