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 com.caucho.util.CharBuffer;
032:
033: import java.io.CharConversionException;
034: import java.io.EOFException;
035: import java.io.IOException;
036: import java.io.UnsupportedEncodingException;
037:
038: public class StringWriter extends StreamImpl {
039: private WriteStream ws;
040: private CharBuffer cb;
041:
042: public StringWriter() {
043: }
044:
045: public StringWriter(CharBuffer cb) {
046: this .cb = cb;
047: }
048:
049: /**
050: * Opens a write stream using this StringWriter as the resulting string
051: */
052: public WriteStream openWrite() {
053: if (cb != null)
054: cb.clear();
055: else
056: cb = CharBuffer.allocate();
057:
058: if (ws == null)
059: ws = new WriteStream(this );
060: else
061: ws.init(this );
062:
063: try {
064: ws.setEncoding("utf-8");
065: } catch (UnsupportedEncodingException e) {
066: }
067:
068: return ws;
069: }
070:
071: public String getString() {
072: try {
073: ws.close();
074: } catch (IOException e) {
075: }
076:
077: String str = cb.close();
078:
079: cb = null;
080:
081: return str;
082: }
083:
084: /**
085: * Returns true since StringWriter is for writing.
086: */
087: public boolean canWrite() {
088: return true;
089: }
090:
091: /**
092: * Writes a utf-8 encoded buffer to the underlying string.
093: *
094: * @param buf byte buffer containing the bytes
095: * @param offset offset where to start writing
096: * @param length number of bytes to write
097: * @param isEnd true when the write is flushing a close.
098: */
099: public void write(byte[] buf, int offset, int length, boolean isEnd)
100: throws IOException {
101: int end = offset + length;
102: while (offset < end) {
103: int ch1 = buf[offset++] & 0xff;
104:
105: if (ch1 < 0x80)
106: cb.append((char) ch1);
107: else if ((ch1 & 0xe0) == 0xc0) {
108: if (offset >= end)
109: throw new EOFException(
110: "unexpected end of file in utf8 character");
111:
112: int ch2 = buf[offset++] & 0xff;
113: if ((ch2 & 0xc0) != 0x80)
114: throw new CharConversionException(
115: "illegal utf8 encoding");
116:
117: cb.append((char) (((ch1 & 0x1f) << 6) + (ch2 & 0x3f)));
118: } else if ((ch1 & 0xf0) == 0xe0) {
119: if (offset + 1 >= end)
120: throw new EOFException(
121: "unexpected end of file in utf8 character");
122:
123: int ch2 = buf[offset++] & 0xff;
124: int ch3 = buf[offset++] & 0xff;
125:
126: if ((ch2 & 0xc0) != 0x80)
127: throw new CharConversionException(
128: "illegal utf8 encoding");
129:
130: if ((ch3 & 0xc0) != 0x80)
131: throw new CharConversionException(
132: "illegal utf8 encoding");
133:
134: cb.append((char) (((ch1 & 0x1f) << 12)
135: + ((ch2 & 0x3f) << 6) + (ch3 & 0x3f)));
136: } else
137: throw new CharConversionException(
138: "illegal utf8 encoding at (" + (int) ch1 + ")");
139: }
140: }
141: }
|