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
00019 public class OStreamFile implements Sct.OStream {
00020
00022 public OStreamFile(NameFile name) throws IOException {
00023 file = new FileOutputStream(name.getName()).getChannel();
00024 buffer = ByteBuffer.allocate(2*1024*1024);
00025 buffer.order(ByteOrder.nativeOrder());
00026 }
00027
00028
00037 public OStreamFile(byte[] output) {
00038 buffer = ByteBuffer.wrap(output);
00039 buffer.order(ByteOrder.nativeOrder());
00040 }
00041
00045 public void close() throws IOException {
00046 if (file != null && file.isOpen()) {
00047 write();
00048 file.force(true);
00049 file.close();
00050 } else {
00051
00052 while (buffer.position() < buffer.capacity())
00053 buffer.put((byte)0);
00054 }
00055 }
00056
00057 public void writeBoolean(String name, boolean b) throws java.io.IOException {
00058 checkAndFlush(1);
00059 if (b) buffer.put((byte)1);
00060 else buffer.put((byte)0);
00061 }
00062
00063 public void writeBooleanArray(String name, boolean[] b) throws java.io.IOException {
00064 checkAndFlush(b.length + 4);
00065 buffer.putInt(b.length);
00066 for (int i=0; i<b.length; ++i) {
00067 if (b[i]) buffer.put((byte)1);
00068 else buffer.put((byte)0);
00069 }
00070 }
00071
00072 public void writeByte(String name, byte b, boolean sign) throws java.io.IOException {
00073 checkAndFlush(1);
00074 buffer.put(b);
00075 }
00076
00077 public void writeByteArray(String name, byte[] b, boolean sign) throws java.io.IOException {
00078 checkAndFlush(b.length + 4);
00079 buffer.putInt(b.length);
00080 buffer.put(b);
00081 }
00082
00083 public void writeDouble(String name, double d) throws java.io.IOException {
00084 checkAndFlush(8);
00085 buffer.putDouble(d);
00086 }
00087
00088 public void writeDoubleArray(String name, double[] d) throws java.io.IOException {
00089 checkAndFlush(8 * d.length + 4);
00090 buffer.putInt(d.length);
00091 buffer.position(buffer.position() + buffer.asDoubleBuffer().put(d).position() * 8);
00092 }
00093
00094 public void writeFloat(String name, float f) throws java.io.IOException {
00095 checkAndFlush(4);
00096 buffer.putFloat(f);
00097 }
00098
00099 public void writeFloatArray(String name, float[] f) throws java.io.IOException {
00100 checkAndFlush(4 * f.length + 4);
00101 buffer.putInt(f.length);
00102 buffer.position(buffer.position() + buffer.asFloatBuffer().put(f).position() * 4);
00103 }
00104
00105 public void writeInt(String name, int i, boolean sign) throws java.io.IOException {
00106 checkAndFlush(4);
00107 buffer.putInt(i);
00108 }
00109
00110 public void writeIntArray(String name, int[] i, boolean sign) throws java.io.IOException {
00111 checkAndFlush(4 * i.length + 4);
00112 buffer.putInt(i.length);
00113 buffer.position(buffer.position() + buffer.asIntBuffer().put(i).position() * 4);
00114 }
00115
00116 public void writeLong(String name, long i, boolean sign) throws java.io.IOException {
00117 checkAndFlush(8);
00118 buffer.putLong(i);
00119 }
00120
00121 public void writeLongArray(String name, long[] i, boolean sign) throws java.io.IOException {
00122 checkAndFlush(8 * i.length + 4);
00123 buffer.putInt(i.length);
00124 buffer.position(buffer.position() + buffer.asLongBuffer().put(i).position() * 8);
00125 }
00126
00127 public void writeShort(String name, short s, boolean sign) throws java.io.IOException {
00128 checkAndFlush(2);
00129 buffer.putShort(s);
00130 }
00131
00132 public void writeShortArray(String name, short[] s, boolean sign) throws java.io.IOException {
00133 checkAndFlush(2 * s.length + 4);
00134 buffer.putInt(s.length);
00135 buffer.position(buffer.position() + buffer.asShortBuffer().put(s).position() * 2);
00136 }
00137
00138 public void writeString(String name, String s) throws java.io.IOException {
00139 checkAndFlush(s.length() + 5);
00140 buffer.putInt(s.length() + 1);
00141 buffer.put(s.getBytes());
00142 buffer.put((byte) 0);
00143 }
00144
00145 public void writeStringArray(String name, String[] s) throws java.io.IOException {
00146 checkAndFlush(4);
00147 buffer.putInt(s.length);
00148 for (int i=0; i<s.length; ++i) {
00149 writeString(name, s[i]);
00150 }
00151 }
00152
00153 private void checkAndFlush(int bytes) throws IOException {
00154 if (buffer.remaining() < bytes) {
00155 write();
00156 buffer.clear();
00157 }
00158 }
00159
00160
00161 private void write() throws IOException {
00162 buffer.limit(buffer.position());
00163 buffer.position(0);
00164 if (file != null && file.isOpen()) file.write(buffer);
00165 }
00166
00167
00168 private FileChannel file;
00169 private ByteBuffer buffer;
00170 }