1 21 22 package org.apache.derby.impl.jdbc; 23 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.io.IOException ; 27 import java.sql.SQLException ; 28 29 32 public final class ReaderToAscii extends InputStream 33 { 34 35 private final Reader data; 36 private char[] conv; 37 private boolean closed; 38 39 public ReaderToAscii(Reader data) 40 { 41 this.data = data; 42 if (!(data instanceof UTF8Reader)) 43 conv = new char[256]; 44 } 45 46 public int read() throws IOException 47 { 48 if (closed) 49 throw new IOException (); 50 51 int c = data.read(); 52 if (c == -1) 53 return -1; 54 55 if (c <= 255) 56 return c & 0xFF; 57 else 58 return '?'; } 60 61 public int read(byte[] buf, int off, int len) throws IOException 62 { 63 if (closed) 64 throw new IOException (); 65 66 if (data instanceof UTF8Reader) { 67 68 return ((UTF8Reader) data).readAsciiInto(buf, off, len); 69 } 70 71 if (len > conv.length) 72 len = conv.length; 73 74 len = data.read(conv, 0, len); 75 if (len == -1) 76 return -1; 77 78 for (int i = 0; i < len; i++) { 79 char c = conv[i]; 80 81 byte cb; 82 if (c <= 255) 83 cb = (byte) c; 84 else 85 cb = (byte) '?'; 87 buf[off++] = cb; 88 } 89 90 return len; 91 } 92 93 public long skip(long len) throws IOException { 94 if (closed) 95 throw new IOException (); 96 97 return data.skip(len); 98 } 99 100 public void close() throws IOException 101 { 102 if (!closed) { 103 closed = true; 104 data.close(); 105 } 106 } 107 } 108 | Popular Tags |