KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.jdbc.BinaryToUnicodeReader 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.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.Reader JavaDoc;
30
31 /**
32  * A Reader implementation that wraps around a unicode encoded input stream
33  * that encodes each unicode character as 2 bytes. See
34  * UnicodeToBinaryStream for the InputStream version of this class.
35  *
36  * @author Tobias Downer
37  */

38
39 public final class BinaryToUnicodeReader extends Reader JavaDoc {
40
41   /**
42    * The wrapped InputStream.
43    */

44   private InputStream JavaDoc input;
45
46   /**
47    * Constructor. Note that we would typically assume that the given
48    * InputStream employs some type of buffering and that calls to 'read' are
49    * buffered and therefore work quickly.
50    */

51   public BinaryToUnicodeReader(InputStream JavaDoc input) {
52     this.input = input;
53   }
54
55   // ---------- Implemented from Reader ----------
56

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