KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.jdbc.UnicodeToBinaryStream 29 Jan 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.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.Reader JavaDoc;
31
32 /**
33  * An object that wraps around a Reader and translates the unicode stream into
34  * a stream of bytes that the database is able to transfer to the database.
35  * This object simply converts each char from the Reader into two bytes. See
36  * also BinaryToUnicodeReader for the Reader version of this class.
37  *
38  * @author Tobias Downer
39  */

40
41 final class UnicodeToBinaryStream extends InputStream JavaDoc {
42
43   /**
44    * The Reader we are wrapping.
45    */

46   private Reader JavaDoc reader;
47   
48   /**
49    * If this is 0 we are on the left byte of the character. If this is 1 we
50    * are on the right byte of the current character.
51    */

52   private int lr_byte;
53
54   /**
55    * The current character if 'lr_byte' is 1.
56    */

57   private int current_c;
58   
59   /**
60    * Constructs the stream.
61    */

62   public UnicodeToBinaryStream(Reader JavaDoc reader) {
63     // Note, we wrap the input Reader around a BufferedReader.
64
// This is a bit lazy. Perhaps a better implementation of this would
65
// implement 'read(byte[] buf, ...)' and provide its own buffering.
66
this.reader = new BufferedReader JavaDoc(reader);
67     lr_byte = 0;
68   }
69   
70   /**
71    * Reads the next character from the stream.
72    */

73   public int read() throws IOException JavaDoc {
74     if (lr_byte == 0) {
75       current_c = reader.read();
76       if (current_c == -1) {
77         return -1;
78       }
79       lr_byte = 1;
80       return (current_c >> 8) & 0x0FF;
81     }
82     else {
83       lr_byte = 0;
84       return current_c & 0x0FF;
85     }
86   }
87
88   public int available() throws IOException JavaDoc {
89     return 0;
90   }
91
92 }
93
94
Popular Tags