001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs;
030:
031: import java.io.CharConversionException;
032: import java.io.EOFException;
033: import java.io.IOException;
034: import java.io.OutputStreamWriter;
035: import java.io.Reader;
036: import java.io.Writer;
037:
038: public class ReaderWriterStream extends StreamImpl {
039: private static NullPath nullPath;
040:
041: private Reader is;
042: private Writer os;
043: private boolean flushOnNewline;
044: private boolean closeChildOnClose = true;
045:
046: private int peek1;
047: private int peek2;
048:
049: public ReaderWriterStream(Reader is, Writer os) {
050: init(is, os);
051: if (nullPath == null)
052: nullPath = new NullPath("stream");
053: setPath(nullPath);
054: }
055:
056: public ReaderWriterStream(Reader is, Writer os, Path path) {
057: init(is, os);
058: setPath(path);
059: }
060:
061: public void init(Reader is, Writer os) {
062: this .is = is;
063: this .os = os;
064: setPath(nullPath);
065: peek1 = -1;
066: peek2 = -1;
067: }
068:
069: public String getEncoding() {
070: if (os instanceof OutputStreamWriter)
071: return Encoding.getMimeName(((OutputStreamWriter) os)
072: .getEncoding());
073: else
074: return null;
075: }
076:
077: public boolean canRead() {
078: return is != null;
079: }
080:
081: public int read(byte[] buf, int offset, int length)
082: throws IOException {
083: if (is == null)
084: return -1;
085:
086: for (int i = 0; i < length; i++) {
087: int ch;
088: if (peek1 >= 0) {
089: buf[offset++] = (byte) peek1;
090: peek1 = peek2;
091: peek2 = -1;
092: } else if ((ch = is.read()) < 0) {
093: return i == 0 ? -1 : i;
094: } else if (ch < 0x80) {
095: buf[offset++] = (byte) ch;
096: } else if (ch < 0x800) {
097: buf[offset++] = (byte) (0xc0 + (ch >> 6));
098: peek1 = 0x80 + (ch & 0x3f);
099: } else {
100: buf[offset++] = (byte) (0xe0 + (ch >> 12));
101: peek1 = 0x80 + ((ch >> 6) & 0x3f);
102: peek2 = 0x80 + (ch & 0x3f);
103: }
104: }
105:
106: return length;
107: }
108:
109: public boolean canWrite() {
110: return os != null;
111: }
112:
113: public boolean getFlushOnNewline() {
114: return flushOnNewline;
115: }
116:
117: public void setFlushOnNewline(boolean value) {
118: flushOnNewline = value;
119: }
120:
121: /**
122: * Implementation of the writer write.
123: *
124: * @param buf byte buffer containing the bytes
125: * @param offset offset where to start writing
126: * @param length number of bytes to write
127: * @param isEnd true when the write is flushing a close.
128: */
129: public void write(byte[] buf, int offset, int length, boolean isEnd)
130: throws IOException {
131: int end = offset + length;
132: while (offset < end) {
133: int ch1 = buf[offset++] & 0xff;
134:
135: if (ch1 < 0x80)
136: os.write(ch1);
137: else if ((ch1 & 0xe0) == 0xc0) {
138: if (offset >= end)
139: throw new EOFException(
140: "unexpected end of file in utf8 character");
141:
142: int ch2 = buf[offset++] & 0xff;
143: if ((ch2 & 0xc0) != 0x80)
144: throw new CharConversionException(
145: "illegal utf8 encoding");
146:
147: os.write(((ch1 & 0x1f) << 6) + (ch2 & 0x3f));
148: } else if ((ch1 & 0xf0) == 0xe0) {
149: if (offset + 1 >= end)
150: throw new EOFException(
151: "unexpected end of file in utf8 character");
152:
153: int ch2 = buf[offset++] & 0xff;
154: int ch3 = buf[offset++] & 0xff;
155:
156: if ((ch2 & 0xc0) != 0x80)
157: throw new CharConversionException(
158: "illegal utf8 encoding");
159:
160: if ((ch3 & 0xc0) != 0x80)
161: throw new CharConversionException(
162: "illegal utf8 encoding");
163:
164: os.write(((ch1 & 0x1f) << 12) + ((ch2 & 0x3f) << 6)
165: + (ch3 & 0x3f));
166: } else
167: throw new CharConversionException(
168: "illegal utf8 encoding at (" + (int) ch1 + ")");
169: }
170: }
171:
172: public void flush() throws IOException {
173: if (os != null)
174: os.flush();
175: }
176:
177: public void setCloseChildOnClose(boolean close) {
178: closeChildOnClose = close;
179: }
180:
181: public void close() throws IOException {
182: if (os != null && closeChildOnClose) {
183: os.close();
184: os = null;
185: }
186:
187: if (is != null && closeChildOnClose) {
188: is.close();
189: is = null;
190: }
191: }
192: }
|