1 24 25 package com.mckoi.store; 26 27 import java.io.*; 28 29 35 36 public class StreamFile { 37 38 41 private final File file; 42 43 46 private RandomAccessFile data; 47 48 51 private long end_pointer; 52 53 56 private OutputStream output_stream; 57 58 61 public StreamFile(File file, String mode) throws IOException { 62 this.file = file; 63 data = new RandomAccessFile(file, mode); 64 end_pointer = data.length(); 65 output_stream = new SFOutputStream(); 66 } 67 68 71 public void close() throws IOException { 72 synchronized (data) { 73 data.close(); 74 } 75 } 76 77 80 public void synch() throws IOException { 81 synchronized (data) { 82 try { 83 data.getFD().sync(); 84 } 85 catch (SyncFailedException e) { 86 } 92 } 93 } 94 95 98 public void delete() throws IOException { 99 file.delete(); 100 } 101 102 106 public void readFully(final long position, 107 byte[] buf, int off, int len) throws IOException { 108 synchronized (data) { 109 data.seek(position); 110 int to_read = len; 111 while (to_read > 0) { 112 int read = data.read(buf, off, to_read); 113 to_read -= read; 114 off += read; 115 } 116 } 117 } 118 119 122 public long length() { 123 synchronized (data) { 124 return end_pointer; 125 } 126 } 127 128 132 public OutputStream getOutputStream() throws IOException { 133 return output_stream; 134 } 135 136 140 public InputStream getInputStream() throws IOException { 141 return new SFInputStream(); 142 } 143 144 146 147 148 class SFOutputStream extends OutputStream { 149 150 public void write(int i) throws IOException { 151 synchronized (data) { 152 data.seek(end_pointer); 153 data.write(i); 154 ++end_pointer; 155 } 156 } 157 158 public void write(byte[] buf, int off, int len) throws IOException { 159 if (len > 0) { 160 synchronized (data) { 161 data.seek(end_pointer); 162 data.write(buf, off, len); 163 end_pointer += len; 164 } 165 } 166 } 167 168 } 169 170 171 172 class SFInputStream extends InputStream { 173 174 private long fp = 0; 175 176 public int read() throws IOException { 177 synchronized (data) { 178 if (fp >= end_pointer) { 179 return -1; 180 } 181 data.seek(fp); 182 ++fp; 183 return data.read(); 184 185 } 186 } 187 188 public int read(byte[] buf, int off, int len) throws IOException { 189 synchronized (data) { 190 if (len == 0) { 191 return 0; 192 } 193 len = (int) Math.min((long) len, end_pointer - fp); 194 if (len <= 0) { 195 return -1; 196 } 197 198 data.seek(fp); 199 int act_read = data.read(buf, off, len); 200 fp += act_read; 201 return act_read; 202 } 203 } 204 205 public long skip(long v) throws IOException { 206 synchronized (data) { 207 fp += v; 208 } 209 return v; 210 } 211 212 } 213 214 } 215 216 | Popular Tags |