1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 35 38 public class VfsStream extends StreamImpl { 39 private static byte []unixNewline = new byte[] { (byte) '\n' }; 40 41 private InputStream is; 42 private OutputStream os; 43 private boolean flushOnNewline; 44 private boolean closeChildOnClose = true; 45 private byte []newline = unixNewline; 46 47 private long position; 48 49 52 public VfsStream() 53 { 54 } 55 56 59 public VfsStream(InputStream is, OutputStream os) 60 { 61 init(is, os); 62 } 63 64 public VfsStream(InputStream is, OutputStream os, Path path) 65 { 66 init(is, os); 67 setPath(path); 68 } 69 70 77 public void init(InputStream is, OutputStream os) 78 { 79 this.is = is; 80 this.os = os; 81 setPath(null); 82 flushOnNewline = false; 83 closeChildOnClose = true; 84 position = 0; 85 } 86 87 public void setNewline(byte []newline) 88 { 89 this.newline = newline; 90 } 91 92 public byte []getNewline() 93 { 94 return newline; 95 } 96 97 public static ReadWritePair openReadWrite(InputStream is, OutputStream os) 98 { 99 VfsStream s = new VfsStream(is, os); 100 WriteStream writeStream = new WriteStream(s); 101 ReadStream readStream = new ReadStream(s, writeStream); 102 return new ReadWritePair(readStream, writeStream); 103 } 104 105 112 public static ReadStream openRead(InputStream is) 113 { 114 VfsStream s = new VfsStream(is, null); 115 return new ReadStream(s); 116 } 117 118 public static ReadStream openRead(InputStream is, WriteStream ws) 119 { 120 VfsStream s = new VfsStream(is, null); 121 return new ReadStream(s, ws); 122 } 123 124 public static WriteStream openWrite(OutputStream os) 125 { 126 VfsStream s = new VfsStream(null, os); 127 return new WriteStream(s); 128 } 129 130 public boolean canRead() 131 { 132 return is != null; 133 } 134 135 public int read(byte []buf, int offset, int length) throws IOException  136 { 137 if (is == null) 138 return -1; 139 140 int len = is.read(buf, offset, length); 141 142 if (len > 0) 143 position += len; 144 145 return len; 146 } 147 148 public boolean hasSkip() 149 { 150 return true; 151 } 152 153 public long skip(long n) 154 throws IOException  155 { 156 return is.skip(n); 157 } 158 159 public int getAvailable() throws IOException  160 { 161 if (is == null) 162 return -1; 163 else 164 return is.available(); 165 } 166 167 public long getReadPosition() 168 { 169 return position; 170 } 171 172 public boolean canWrite() 173 { 174 return os != null; 175 } 176 177 public boolean getFlushOnNewline() 178 { 179 return flushOnNewline; 180 } 181 182 public void setFlushOnNewline(boolean value) 183 { 184 flushOnNewline = value; 185 } 186 187 195 public void write(byte []buf, int offset, int length, boolean isEnd) 196 throws IOException  197 { 198 if (os != null) 199 os.write(buf, offset, length); 200 } 201 202 public void flushToDisk() throws IOException  203 { 204 flush(); 205 } 206 207 public void flush() throws IOException  208 { 209 if (os != null) { 210 os.flush(); 211 } 212 } 213 214 public void setCloseChildOnClose(boolean close) 215 { 216 closeChildOnClose = close; 217 } 218 219 public void close() throws IOException  220 { 221 try { 222 if (os != null && closeChildOnClose) { 223 os.close(); 224 os = null; 225 } 226 } finally { 227 if (is != null && closeChildOnClose) { 228 is.close(); 229 is = null; 230 } 231 } 232 } 233 } 234 | Popular Tags |