1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 33 36 public class JniFileStream extends StreamImpl { 37 private int _fd; 38 private long _pos; 39 40 private boolean _canRead; 41 private boolean _canWrite; 42 43 46 public JniFileStream(int fd, boolean canRead, boolean canWrite) 47 { 48 init(fd, canRead, canWrite); 49 } 50 51 void init(int fd, boolean canRead, boolean canWrite) 52 { 53 _fd = fd; 54 _pos = 0; 55 56 _canRead = canRead; 57 _canWrite = canWrite; 58 } 59 60 public boolean canRead() 61 { 62 return _canRead && _fd >= 0; 63 } 64 65 public boolean hasSkip() 66 { 67 return _fd >= 0; 68 } 69 70 public long skip(long length) 71 throws IOException 72 { 73 long pos = nativeSkip(_fd, length); 74 75 if (pos < 0) 76 return -1; 77 else { 78 _pos = pos; 79 return length; 80 } 81 } 82 83 86 public int read(byte []buf, int offset, int length) 87 throws IOException 88 { 89 if (buf == null) 90 throw new NullPointerException (); 91 else if (offset < 0 || buf.length < offset + length) 92 throw new ArrayIndexOutOfBoundsException (); 93 94 int result = nativeRead(_fd, buf, offset, length); 95 96 if (result > 0) 97 _pos += result; 98 99 return result; 100 } 101 102 public int getAvailable() throws IOException 104 { 105 if (_fd < 0) { 106 return -1; 107 } 108 else if (getPath() instanceof FilesystemPath) { 109 long length = getPath().getLength(); 110 111 return (int) (length - _pos); 112 } 113 else 114 return nativeAvailable(_fd); 115 } 116 117 120 public boolean canWrite() 121 { 122 return _canWrite && _fd >= 0; 123 } 124 125 128 public void write(byte []buf, int offset, int length, boolean isEnd) 129 throws IOException 130 { 131 if (buf == null) 132 throw new NullPointerException (); 133 else if (offset < 0 || buf.length < offset + length) 134 throw new ArrayIndexOutOfBoundsException (); 135 136 nativeWrite(_fd, buf, offset, length); 137 } 138 139 public void seekStart(long offset) 140 throws IOException 141 { 142 nativeSeekStart(_fd, offset); 143 } 144 145 public void seekEnd(long offset) 146 throws IOException 147 { 148 nativeSeekEnd(_fd, offset); 149 } 150 151 public void flush() 152 throws IOException 153 { 154 } 155 156 public void flushToDisk() 157 throws IOException 158 { 159 nativeFlushToDisk(_fd); 160 } 161 162 public void close() 163 throws IOException 164 { 165 int fd; 166 167 synchronized (this) { 168 fd = _fd; 169 _fd = -1; 170 } 171 172 nativeClose(fd); 173 } 174 175 protected void finalize() 176 throws IOException 177 { 178 close(); 179 } 180 181 184 native int nativeRead(int fd, byte []buf, int offset, int length) 185 throws IOException ; 186 187 190 native int nativeAvailable(int fd) 191 throws IOException ; 192 193 196 native int nativeWrite(int fd, byte []buf, int offset, int length) 197 throws IOException ; 198 199 202 native long nativeSkip(int fd, long skip) 203 throws IOException ; 204 205 208 native int nativeFlushToDisk(int fd) 209 throws IOException ; 210 211 214 native int nativeClose(int fd) 215 throws IOException ; 216 217 220 native int nativeSeekStart(int fd, long offset) 221 throws IOException ; 222 223 226 native int nativeSeekEnd(int fd, long offset) 227 throws IOException ; 228 229 232 public String toString() 233 { 234 return "JniFileStream[" + getPath().getNativePath() + "]"; 235 } 236 237 static { 238 try { 239 System.loadLibrary("resin"); 240 } catch (Throwable e) { 241 System.err.println("Can't open Resin JNI library: " + e); 242 } 243 } 244 } 245 246 | Popular Tags |