KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.jdbc.AsciiReader 01 Feb 2003
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.Reader JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * A java.io.Reader implementation that wraps around an ascii input stream
33  * (8-bit per char stream).
34  *
35  * @author Tobias Downer
36  */

37
38 public final class AsciiReader extends Reader JavaDoc {
39
40   /**
41    * The 8-bit per character Ascii input straem.
42    */

43   private InputStream JavaDoc input;
44
45   /**
46    * Constructs the reader.
47    */

48   public AsciiReader(InputStream JavaDoc input) {
49     this.input = input;
50   }
51
52   // ---------- Implemented from Reader ----------
53

54   public int read() throws IOException JavaDoc {
55     int v = input.read();
56     if (v == -1) {
57       return -1;
58     }
59     else {
60       return (char) v;
61     }
62   }
63
64   public int read(char[] buf, int off, int len) throws IOException JavaDoc {
65     if (len < 0) {
66       throw new IOException JavaDoc("len < 0");
67     }
68     if (off < 0 || off + len > buf.length) {
69       throw new IOException JavaDoc("Out of bounds.");
70     }
71     if (len == 0) {
72       return 0;
73     }
74     int read = 0;
75     while (len > 0) {
76       int v = input.read();
77       if (v == -1) {
78         if (read == 0) {
79           return -1;
80         }
81         else {
82           return read;
83         }
84       }
85       buf[off] = (char) v;
86       ++off;
87       ++read;
88       --len;
89     }
90     return read;
91   }
92
93   public long skip(long n) throws IOException JavaDoc {
94     return input.skip(n);
95   }
96
97   public boolean ready() throws IOException JavaDoc {
98     return false;
99   }
100
101   public void mark(int readAheadLimit) throws IOException JavaDoc {
102     input.mark(readAheadLimit);
103   }
104
105   public void reset() throws IOException JavaDoc {
106     input.reset();
107   }
108
109   public void close() throws IOException JavaDoc {
110     input.close();
111   }
112
113 }
114
115
Popular Tags