00001
00002
00003
00004
00005
00006
00007 package Sct.File;
00008
00009 import java.io.*;
00010 import java.nio.*;
00011 import java.nio.channels.FileChannel;
00012
00017 public class IStreamFile implements Sct.IStream {
00018
00020 public IStreamFile(NameFile name) throws IOException {
00021 FileChannel fc = new FileInputStream(name.getName()).getChannel();
00022 buffer = ByteBuffer.allocate((int)fc.size());
00023 fc.read(buffer);
00024 fc.close();
00025 buffer.position(0);
00026 buffer.order(ByteOrder.nativeOrder());
00027 }
00028
00033 public IStreamFile(byte[] input) {
00034 buffer = ByteBuffer.wrap(input);
00035 buffer.position(0);
00036 buffer.order(ByteOrder.nativeOrder());
00037 }
00038
00039 public boolean readBoolean(String name) throws java.io.IOException {
00040 if (buffer.get() == 0) return false;
00041 else return true;
00042 }
00043
00044 public boolean[] readBooleanArray(String name) throws java.io.IOException {
00045 int size = buffer.getInt();
00046 boolean[] bools = new boolean[size];
00047 for (int i=0; i<size; ++i) {
00048 if (buffer.get() == 0) bools[i] = false;
00049 else bools[i] = true;
00050 }
00051 return bools;
00052 }
00053
00054 public byte readByte(String name) throws java.io.IOException {
00055 return buffer.get();
00056 }
00057
00058 public byte[] readByteArray(String name) throws java.io.IOException {
00059 int size = buffer.getInt();
00060 byte[] bytes = new byte[size];
00061 buffer.get(bytes);
00062 return bytes;
00063 }
00064
00065 public double readDouble(String name) throws java.io.IOException {
00066 return buffer.getDouble();
00067 }
00068
00069 public double[] readDoubleArray(String name) throws java.io.IOException {
00070 int size = buffer.getInt();
00071 double[] doubles = new double[size];
00072 buffer.position(buffer.position() + buffer.asDoubleBuffer().get(doubles).position() * 8);
00073 return doubles;
00074 }
00075
00076 public float readFloat(String name) throws java.io.IOException {
00077 return buffer.getFloat();
00078 }
00079
00080 public float[] readFloatArray(String name) throws java.io.IOException {
00081 int size = buffer.getInt();
00082 float[] floats = new float[size];
00083 buffer.position(buffer.position() + buffer.asFloatBuffer().get(floats).position() * 4);
00084 return floats;
00085 }
00086
00087 public int readInt(String name) throws java.io.IOException {
00088 return buffer.getInt();
00089 }
00090
00091 public int[] readIntArray(String name) throws java.io.IOException {
00092 int size = buffer.getInt();
00093 int[] ints = new int[size];
00094 buffer.position(buffer.position() + buffer.asIntBuffer().get(ints).position() * 4);
00095 return ints;
00096 }
00097
00098 public long readLong(String name) throws java.io.IOException {
00099 return buffer.getLong();
00100 }
00101
00102 public long[] readLongArray(String name) throws java.io.IOException {
00103 int size = buffer.getInt();
00104 long[] longs = new long[size];
00105 buffer.position(buffer.position() + buffer.asLongBuffer().get(longs).position() * 8);
00106 return longs;
00107 }
00108
00109 public short readShort(String name) throws java.io.IOException {
00110 return buffer.getShort();
00111 }
00112
00113 public short[] readShortArray(String name) throws java.io.IOException {
00114 int size = buffer.getInt();
00115 short[] shorts = new short[size];
00116 buffer.position(buffer.position() + buffer.asShortBuffer().get(shorts).position() * 2);
00117 return shorts;
00118 }
00119
00120 public String readString(String name) throws java.io.IOException {
00121 int size = buffer.getInt();
00122 byte[] bytes = new byte[size - 1];
00123 buffer.get(bytes);
00124 buffer.get();
00125 return new String(bytes);
00126 }
00127
00128 public String[] readStringArray(String name) throws java.io.IOException {
00129 int size = buffer.getInt();
00130 String[] strings = new String[size];
00131 for (int i=0; i<size; ++i) {
00132 strings[i] = readString("");
00133 }
00134 return strings;
00135 }
00136
00140 private void align(Buffer a, Buffer b) {
00141 b.position(a.position());
00142 }
00143
00144 private ByteBuffer buffer;
00145 }