KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > ReaderToAscii


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.ReaderToAscii
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 /**
30     ReaderToAscii converts Reader (with characters) to a stream of ASCII characters.
31 */

32 public final class ReaderToAscii extends InputStream JavaDoc
33 {
34
35     private final Reader JavaDoc data;
36     private char[] conv;
37     private boolean closed;
38
39     public ReaderToAscii(Reader JavaDoc data)
40     {
41         this.data = data;
42         if (!(data instanceof UTF8Reader))
43             conv = new char[256];
44     }
45
46     public int read() throws IOException JavaDoc
47     {
48         if (closed)
49             throw new IOException JavaDoc();
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 '?'; // Question mark - out of range character.
59
}
60
61     public int read(byte[] buf, int off, int len) throws IOException JavaDoc
62     {
63         if (closed)
64             throw new IOException JavaDoc();
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) '?'; // Question mark - out of range character.
86

87             buf[off++] = cb;
88         }
89
90         return len;
91     }
92
93     public long skip(long len) throws IOException JavaDoc {
94         if (closed)
95             throw new IOException JavaDoc();
96
97         return data.skip(len);
98     }
99
100     public void close() throws IOException JavaDoc
101     {
102         if (!closed) {
103             closed = true;
104             data.close();
105         }
106     }
107 }
108
Popular Tags