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.IOException;
032: import java.io.UnsupportedEncodingException;
033: import java.io.Writer;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: /**
038: * Handles a stream which outputs to a writer.
039: */
040: public class WriterStreamImpl extends StreamImpl {
041: private static Logger log = Logger.getLogger(WriterStreamImpl.class
042: .getName());
043:
044: private Writer _writer;
045: private ByteToCharWriter _byteToChar = new ByteToCharWriter();
046: private boolean _isClosed;
047:
048: /**
049: * Sets the writer.
050: */
051: public void setWriter(Writer writer) {
052: _writer = writer;
053: _byteToChar.setWriter(writer);
054: _isClosed = false;
055:
056: try {
057: _byteToChar.setEncoding(null);
058: } catch (UnsupportedEncodingException e) {
059: log.log(Level.WARNING, e.toString(), e);
060: }
061: }
062:
063: /**
064: * Returns true if this is a writable stream.
065: */
066: public boolean canWrite() {
067: return true;
068: }
069:
070: /**
071: * Sets the write encoding.
072: */
073: public void setWriteEncoding(String encoding) {
074: try {
075: _byteToChar.setEncoding(encoding);
076: } catch (UnsupportedEncodingException e) {
077: log.log(Level.WARNING, e.toString(), e);
078: }
079: }
080:
081: /**
082: * Writes a buffer to the underlying stream.
083: *
084: * @param buffer the byte array to write.
085: * @param offset the offset into the byte array.
086: * @param length the number of bytes to write.
087: * @param isEnd true when the write is flushing a close.
088: */
089: public void write(byte[] buffer, int offset, int length,
090: boolean isEnd) throws IOException {
091: if (_isClosed)
092: return;
093:
094: for (int i = 0; i < length; i++)
095: _byteToChar.addByte(buffer[offset + i]);
096:
097: _byteToChar.flush();
098: }
099:
100: /**
101: * Flushes the write output.
102: */
103: public void flush() throws IOException {
104: }
105:
106: /**
107: * Closes the output.
108: */
109: public void close() {
110: _isClosed = true;
111: }
112: }
|