KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > AsciiInputStream


1 /**
2  * com.mckoi.database.jdbc.AsciiInputStream 21 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.io.*;
28
29 /**
30  * An InputStream that converts a Reader to a plain ascii stream. This
31  * cuts out the top 8 bits of the unicode char.
32  *
33  * @author Tobias Downer
34  */

35
36 class AsciiInputStream extends InputStream { // extends InputStreamFilter {
37

38   private Reader reader;
39
40   public AsciiInputStream(Reader reader) {
41     this.reader = reader;
42   }
43
44   public AsciiInputStream(String JavaDoc s) {
45     this(new StringReader(s));
46   }
47
48   public int read() throws IOException {
49     int i = reader.read();
50     if (i == -1) return i;
51     else return (i & 0x0FF);
52   }
53
54   public int read(byte[] b, int off, int len) throws IOException {
55     int end = off + len;
56     int read_count = 0;
57     for (int i = off; i < end; ++i) {
58       int val = read();
59       if (val == -1) {
60         if (read_count == 0) {
61           return -1;
62         }
63         else {
64           return read_count;
65         }
66       }
67       b[i] = (byte) val;
68       ++read_count;
69     }
70     return read_count;
71   }
72
73   public long skip(long n) throws IOException {
74     return reader.skip(n);
75   }
76
77   public int available() throws IOException {
78     // NOTE: This is valid according to JDBC spec.
79
return 0;
80   }
81
82   public void reset() throws IOException {
83     reader.reset();
84   }
85
86 }
87
Popular Tags