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:
033: /**
034: * A stream reading data from a string. The reader produces bytes using
035: * UTF-8.
036: */
037: public class StringReader extends StreamImpl {
038: private String string;
039: private int length;
040: private int index;
041:
042: private StringReader(String string) {
043: // this.path = new NullPath("string");
044: this .string = string;
045: this .length = string.length();
046: this .index = 0;
047: }
048:
049: /**
050: * Creates a new ReadStream reading bytes from the given string.
051: *
052: * @param string the source string.
053: *
054: * @return a ReadStream reading from the string.
055: */
056: public static ReadStream open(String string) {
057: StringReader ss = new StringReader(string);
058: ReadStream rs = new ReadStream(ss);
059: try {
060: rs.setEncoding("UTF-8");
061: } catch (Exception e) {
062: }
063: rs.setPath(new NullPath("string"));
064: return rs;
065: }
066:
067: /**
068: * The string reader can always read.
069: */
070: public boolean canRead() {
071: return true;
072: }
073:
074: public int read(byte[] buf, int offset, int length)
075: throws IOException {
076: char ch;
077:
078: int i = 0;
079: for (; index < this .length && i < length; index++) {
080: ch = string.charAt(index);
081:
082: if (ch < 0x80) {
083: buf[offset + i] = (byte) ch;
084: i++;
085: } else if (i + 1 >= length)
086: break;
087: else if (ch < 0x800) {
088: buf[offset + i] = (byte) (0xc0 + (ch >> 6));
089: buf[offset + i + 1] = (byte) (0x80 + (ch & 0x3f));
090: i += 2;
091: } else if (i + 2 >= length)
092: break;
093: else {
094: buf[offset + i] = (byte) (0xe0 + (ch >> 12));
095: buf[offset + i + 1] = (byte) (0x80 + ((ch >> 6) & 0x3f));
096: buf[offset + i + 2] = (byte) (0x80 + (ch & 0x3f));
097:
098: i += 3;
099: }
100: }
101:
102: return i > 0 ? i : -1;
103: }
104:
105: /**
106: * Returns the number of characters available as an approximation to
107: * the number of bytes ready.
108: */
109: public int getAvailable() throws IOException {
110: return length - index;
111: }
112: }
|