1 16 package scriptella.configuration; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.Reader ; 21 22 23 29 class ReaderInputStream extends InputStream { 30 private Reader reader; 31 32 public ReaderInputStream(Reader reader) { 33 this.reader = reader; 34 } 35 36 public int read() throws IOException { 37 return reader.read(); 38 } 39 40 private char[] tmpBuf; 42 public int read(final byte b[], final int off, final int len) 43 throws IOException { 44 char[] c; 45 if (tmpBuf != null && tmpBuf.length >= len) { 46 c = tmpBuf; 47 } else { 48 c = new char[len]; 49 } 50 51 int n = reader.read(c, 0, len); 52 tmpBuf = c; 53 54 if (n > 0) { 55 for (int i = 0; i < n; i++) { 56 b[off + i] = (byte) c[i]; 57 } 58 } 59 60 return n; 61 } 62 63 public void close() throws IOException { 64 tmpBuf = null; 65 reader.close(); 66 } 67 } 68 | Popular Tags |