KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > ReaderWriterStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.CharConversionException JavaDoc;
32 import java.io.EOFException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStreamWriter JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.io.Writer JavaDoc;
37
38 public class ReaderWriterStream extends StreamImpl {
39   private static NullPath nullPath;
40
41   private Reader JavaDoc is;
42   private Writer JavaDoc os;
43   private boolean flushOnNewline;
44   private boolean closeChildOnClose = true;
45
46   private int peek1;
47   private int peek2;
48
49   public ReaderWriterStream(Reader JavaDoc is, Writer JavaDoc os)
50   {
51     init(is, os);
52     if (nullPath == null)
53       nullPath = new NullPath("stream");
54     setPath(nullPath);
55   }
56
57   public ReaderWriterStream(Reader JavaDoc is, Writer JavaDoc os, Path path)
58   {
59     init(is, os);
60     setPath(path);
61   }
62
63   public void init(Reader JavaDoc is, Writer JavaDoc os)
64   {
65     this.is = is;
66     this.os = os;
67     setPath(nullPath);
68     peek1 = -1;
69     peek2 = -1;
70   }
71
72   public String JavaDoc getEncoding()
73   {
74     if (os instanceof OutputStreamWriter JavaDoc)
75       return Encoding.getMimeName(((OutputStreamWriter JavaDoc) os).getEncoding());
76     else
77       return null;
78   }
79
80   public boolean canRead()
81   {
82     return is != null;
83   }
84
85   public int read(byte []buf, int offset, int length) throws IOException JavaDoc
86   {
87     if (is == null)
88       return -1;
89
90     for (int i = 0; i < length; i++) {
91       int ch;
92       if (peek1 >= 0) {
93         buf[offset++] = (byte) peek1;
94         peek1 = peek2;
95         peek2 = -1;
96       }
97       else if ((ch = is.read()) < 0) {
98         return i == 0 ? -1 : i;
99       }
100       else if (ch < 0x80) {
101         buf[offset++] = (byte) ch;
102       }
103       else if (ch < 0x800) {
104         buf[offset++] = (byte) (0xc0 + (ch >> 6));
105         peek1 = 0x80 + (ch & 0x3f);
106       }
107       else {
108         buf[offset++] = (byte) (0xe0 + (ch >> 12));
109         peek1 = 0x80 + ((ch >> 6) & 0x3f);
110         peek2 = 0x80 + (ch & 0x3f);
111       }
112     }
113
114     return length;
115   }
116
117   public boolean canWrite()
118   {
119     return os != null;
120   }
121
122   public boolean getFlushOnNewline()
123   {
124     return flushOnNewline;
125   }
126
127   public void setFlushOnNewline(boolean value)
128   {
129     flushOnNewline = value;
130   }
131
132   /**
133    * Implementation of the writer write.
134    *
135    * @param buf byte buffer containing the bytes
136    * @param offset offset where to start writing
137    * @param length number of bytes to write
138    * @param isEnd true when the write is flushing a close.
139    */

140   public void write(byte []buf, int offset, int length, boolean isEnd)
141     throws IOException JavaDoc
142   {
143     int end = offset + length;
144     while (offset < end) {
145       int ch1 = buf[offset++] & 0xff;
146
147       if (ch1 < 0x80)
148         os.write(ch1);
149       else if ((ch1 & 0xe0) == 0xc0) {
150         if (offset >= end)
151           throw new EOFException JavaDoc("unexpected end of file in utf8 character");
152         
153         int ch2 = buf[offset++] & 0xff;
154         if ((ch2 & 0xc0) != 0x80)
155           throw new CharConversionException JavaDoc("illegal utf8 encoding");
156       
157         os.write(((ch1 & 0x1f) << 6) + (ch2 & 0x3f));
158       }
159       else if ((ch1 & 0xf0) == 0xe0) {
160         if (offset + 1 >= end)
161           throw new EOFException JavaDoc("unexpected end of file in utf8 character");
162         
163         int ch2 = buf[offset++] & 0xff;
164         int ch3 = buf[offset++] & 0xff;
165       
166         if ((ch2 & 0xc0) != 0x80)
167           throw new CharConversionException JavaDoc("illegal utf8 encoding");
168       
169         if ((ch3 & 0xc0) != 0x80)
170           throw new CharConversionException JavaDoc("illegal utf8 encoding");
171       
172         os.write(((ch1 & 0x1f) << 12) + ((ch2 & 0x3f) << 6) + (ch3 & 0x3f));
173       }
174       else
175         throw new CharConversionException JavaDoc("illegal utf8 encoding at (" +
176                                           (int) ch1 + ")");
177     }
178   }
179
180   public void flush() throws IOException JavaDoc
181   {
182     if (os != null)
183       os.flush();
184   }
185
186   public void setCloseChildOnClose(boolean close)
187   {
188     closeChildOnClose = close;
189   }
190
191   public void close() throws IOException JavaDoc
192   {
193     if (os != null && closeChildOnClose) {
194       os.close();
195       os = null;
196     }
197
198     if (is != null && closeChildOnClose) {
199       is.close();
200       is = null;
201     }
202   }
203 }
204
Popular Tags