SliceWriter.java
Go to the documentation of this file.00001 package theba.core.io;
00002
00003 import java.io.FileNotFoundException;
00004 import java.io.IOException;
00005 import java.io.RandomAccessFile;
00006
00012 public class SliceWriter extends SliceReader {
00013
00014 public SliceWriter(final String filename, int width, int height, int depth,
00015 int cacheSize) {
00016 super(width, height, depth, cacheSize);
00017 this.filename = filename;
00018 file = null;
00019 try {
00020 file = new RandomAccessFile(filename, "rw");
00021 } catch (FileNotFoundException e) {
00022 e.printStackTrace();
00023 }
00024 }
00025
00026 @Override
00027 public synchronized void putSlice(short[] data, int pos) {
00028 short[] cached = getSlice(pos);
00029 System.arraycopy(data, 0, cached, 0, cached.length);
00030 }
00031
00032 public static void main(String[] args) {
00033 short[] data = new short[10 * 10];
00034 data[0] = 0;
00035 data[1] = 255;
00036 data[2] = 512;
00037 System.out.println(data[0]);
00038 System.out.println(data[1]);
00039 System.out.println(data[2]);
00040 SliceWriter sw = new SliceWriter("test", 10, 10, 5, 10);
00041 sw.putSlice(data, 0);
00042 sw.putSlice(data, 4);
00043 sw.flush();
00044
00045 try {
00046 sw.getFile().close();
00047 } catch (IOException e) {
00048
00049 e.printStackTrace();
00050 }
00051
00052 SliceReader r = new SliceReader("test", 10, 10, 1, 10);
00053 data = r.getSlice(0);
00054 System.out.println(data[0]);
00055 System.out.println(data[1]);
00056 System.out.println(data[2]);
00057 System.out.println(data[0]);
00058 System.out.println(data[1]);
00059 System.out.println(data[2]);
00060 }
00061
00062 @Override
00063 public synchronized void updateCache() {
00064 while (queue.size() > cacheSize) {
00065 int deletedSlice = queue.removeFirst();
00066 short[] sliceCache = cache.remove(deletedSlice);
00067 if (sliceCache != null) {
00068 try {
00069 file.seek(sliceCache.length * 2 * deletedSlice);
00070 byte[] out = DataConversion.shortToBytes(sliceCache);
00071 file.write(out, 0, out.length);
00072 } catch (Exception e) {
00073 e.printStackTrace();
00074 }
00075 preallocatedDataArrays.add(sliceCache);
00076 } else
00077 System.err.println("UpdateCache: this slce was not cached "
00078 + deletedSlice);
00079 }
00080 }
00081
00082 @Override
00083 public synchronized void flush() {
00084 while (queue.size() > 0) {
00085 int deletedSlice = queue.removeFirst();
00086 short[] sliceCache = cache.remove(deletedSlice);
00087 if (sliceCache != null) {
00088 try {
00089 file.seek(sliceCache.length * 2 * deletedSlice);
00090 byte[] out = DataConversion.shortToBytes(sliceCache);
00091 file.write(out, 0, out.length);
00092 } catch (Exception e) {
00093 e.printStackTrace();
00094 }
00095 preallocatedDataArrays.add(sliceCache);
00096 } else
00097 System.err.println("Flush : this slice was not cached: "
00098 + deletedSlice);
00099 }
00100 }
00101
00102 @Override
00103 public synchronized void close() {
00104 try {
00105 file.close();
00106 } catch (IOException e) {
00107 e.printStackTrace();
00108 }
00109 }
00110 }