KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 /**
34  * A stream reading data from a string. The reader produces bytes using
35  * UTF-8.
36  */

37 public class StringReader extends StreamImpl {
38   private String JavaDoc string;
39   private int length;
40   private int index;
41
42   private StringReader(String JavaDoc string)
43   {
44     // this.path = new NullPath("string");
45
this.string = string;
46     this.length = string.length();
47     this.index = 0;
48   }
49
50   /**
51    * Creates a new ReadStream reading bytes from the given string.
52    *
53    * @param string the source string.
54    *
55    * @return a ReadStream reading from the string.
56    */

57   public static ReadStream open(String JavaDoc string)
58   {
59     StringReader ss = new StringReader(string);
60     ReadStream rs = new ReadStream(ss);
61     try {
62       rs.setEncoding("UTF-8");
63     } catch (Exception JavaDoc e) {
64     }
65     rs.setPath(new NullPath("string"));
66     return rs;
67   }
68
69   /**
70    * The string reader can always read.
71    */

72   public boolean canRead()
73   {
74     return true;
75   }
76
77   public int read(byte []buf, int offset, int length)
78     throws IOException JavaDoc
79   {
80     char ch;
81
82     int i = 0;
83     for (; index < this.length && i < length; index++) {
84       ch = string.charAt(index);
85
86       if (ch < 0x80) {
87         buf[offset + i] = (byte) ch;
88         i++;
89       }
90       else if (i + 1 >= length)
91         break;
92       else if (ch < 0x800) {
93         buf[offset + i] = (byte) (0xc0 + (ch >> 6));
94         buf[offset + i + 1] = (byte) (0x80 + (ch & 0x3f));
95         i += 2;
96       }
97       else if (i + 2 >= length)
98         break;
99       else {
100         buf[offset + i] = (byte) (0xe0 + (ch >> 12));
101         buf[offset + i + 1] = (byte) (0x80 + ((ch >> 6) & 0x3f));
102         buf[offset + i + 2] = (byte) (0x80 + (ch & 0x3f));
103         
104         i += 3;
105       }
106     }
107
108     return i > 0 ? i : -1;
109   }
110
111   /**
112    * Returns the number of characters available as an approximation to
113    * the number of bytes ready.
114    */

115   public int getAvailable() throws IOException JavaDoc
116   {
117     return length - index;
118   }
119 }
120
Popular Tags