KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.caucho.util.CharBuffer;
32
33 import java.io.CharConversionException JavaDoc;
34 import java.io.EOFException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.UnsupportedEncodingException JavaDoc;
37
38 public class StringWriter extends StreamImpl {
39   private WriteStream ws;
40   private CharBuffer cb;
41
42   public StringWriter()
43   {
44   }
45   
46   public StringWriter(CharBuffer cb)
47   {
48     this.cb = cb;
49   }
50
51   /**
52    * Opens a write stream using this StringWriter as the resulting string
53    */

54   public WriteStream openWrite()
55   {
56     if (cb != null)
57       cb.clear();
58     else
59       cb = CharBuffer.allocate();
60
61     if (ws == null)
62       ws = new WriteStream(this);
63     else
64       ws.init(this);
65
66     try {
67       ws.setEncoding("utf-8");
68     } catch (UnsupportedEncodingException JavaDoc e) {
69     }
70
71     return ws;
72   }
73
74   public String JavaDoc getString()
75   {
76     try {
77       ws.close();
78     } catch (IOException JavaDoc e) {
79     }
80
81     String JavaDoc str = cb.close();
82
83     cb = null;
84
85     return str;
86   }
87
88   /**
89    * Returns true since StringWriter is for writing.
90    */

91   public boolean canWrite()
92   {
93     return true;
94   }
95
96   /**
97    * Writes a utf-8 encoded buffer to the underlying string.
98    *
99    * @param buf byte buffer containing the bytes
100    * @param offset offset where to start writing
101    * @param length number of bytes to write
102    * @param isEnd true when the write is flushing a close.
103    */

104   public void write(byte []buf, int offset, int length, boolean isEnd)
105     throws IOException JavaDoc
106   {
107     int end = offset + length;
108     while (offset < end) {
109       int ch1 = buf[offset++] & 0xff;
110
111       if (ch1 < 0x80)
112         cb.append((char) ch1);
113       else if ((ch1 & 0xe0) == 0xc0) {
114         if (offset >= end)
115           throw new EOFException JavaDoc("unexpected end of file in utf8 character");
116         
117         int ch2 = buf[offset++] & 0xff;
118         if ((ch2 & 0xc0) != 0x80)
119           throw new CharConversionException JavaDoc("illegal utf8 encoding");
120       
121         cb.append((char) (((ch1 & 0x1f) << 6) + (ch2 & 0x3f)));
122       }
123       else if ((ch1 & 0xf0) == 0xe0) {
124         if (offset + 1 >= end)
125           throw new EOFException JavaDoc("unexpected end of file in utf8 character");
126         
127         int ch2 = buf[offset++] & 0xff;
128         int ch3 = buf[offset++] & 0xff;
129       
130         if ((ch2 & 0xc0) != 0x80)
131           throw new CharConversionException JavaDoc("illegal utf8 encoding");
132       
133         if ((ch3 & 0xc0) != 0x80)
134           throw new CharConversionException JavaDoc("illegal utf8 encoding");
135       
136         cb.append((char) (((ch1 & 0x1f) << 12) + ((ch2 & 0x3f) << 6) + (ch3 & 0x3f)));
137       }
138       else
139         throw new CharConversionException JavaDoc("illegal utf8 encoding at (" +
140                                           (int) ch1 + ")");
141     }
142   }
143 }
144
Popular Tags