KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Handles a stream which outputs to a writer.
39  */

40 public class WriterStreamImpl extends StreamImpl {
41   private static Logger JavaDoc log
42     = Logger.getLogger(WriterStreamImpl.class.getName());
43   
44   private Writer JavaDoc _writer;
45   private ByteToCharWriter _byteToChar = new ByteToCharWriter();
46   private boolean _isClosed;
47
48   /**
49    * Sets the writer.
50    */

51   public void setWriter(Writer JavaDoc writer)
52   {
53     _writer = writer;
54     _byteToChar.setWriter(writer);
55     _isClosed = false;
56
57     try {
58       _byteToChar.setEncoding(null);
59     } catch (UnsupportedEncodingException JavaDoc e) {
60       log.log(Level.WARNING, e.toString(), e);
61     }
62   }
63
64   /**
65    * Returns true if this is a writable stream.
66    */

67   public boolean canWrite()
68   {
69     return true;
70   }
71
72   /**
73    * Sets the write encoding.
74    */

75   public void setWriteEncoding(String JavaDoc encoding)
76   {
77     try {
78       _byteToChar.setEncoding(encoding);
79     } catch (UnsupportedEncodingException JavaDoc e) {
80       log.log(Level.WARNING, e.toString(), e);
81     }
82   }
83
84   /**
85    * Writes a buffer to the underlying stream.
86    *
87    * @param buffer the byte array to write.
88    * @param offset the offset into the byte array.
89    * @param length the number of bytes to write.
90    * @param isEnd true when the write is flushing a close.
91    */

92   public void write(byte []buffer, int offset, int length, boolean isEnd)
93     throws IOException JavaDoc
94   {
95     if (_isClosed)
96       return;
97     
98     for (int i = 0; i < length; i++)
99       _byteToChar.addByte(buffer[offset + i]);
100
101     _byteToChar.flush();
102   }
103
104   /**
105    * Flushes the write output.
106    */

107   public void flush() throws IOException JavaDoc
108   {
109   }
110
111   /**
112    * Closes the output.
113    */

114   public void close()
115   {
116     _isClosed = true;
117   }
118 }
119
Popular Tags