1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 33 public class StringStream extends StreamImpl { 34 private String _string; 35 private int _length; 36 private int _index; 37 38 StringStream(String string) 39 { 40 _string = string; 42 _length = string.length(); 43 _index = 0; 44 } 45 46 public static ReadStream open(String string) 47 { 48 StringStream ss = new StringStream(string); 49 return new ReadStream(ss); 50 } 51 52 public Path getPath() { return new StringPath(_string); } 53 54 public boolean canRead() { return true; } 55 56 public int read(byte []buf, int offset, int length) throws IOException 58 { 59 if (_length - _index < length) 60 length = _length - _index; 61 62 for (int i = 0; i < length; i++) { 63 int ch = _string.charAt(_index + i); 64 65 if (ch < 0x80) 66 buf[offset++] = (byte) ch; 67 else if (ch < 0x800) { 68 buf[offset++] = (byte) (0xc0 | (ch >> 6)); 69 buf[offset++] = (byte) (0x80 | (ch & 0x3f)); 70 } 71 else if (ch < 0x8000) { 72 buf[offset++] = (byte) (0xe0 | (ch >> 12)); 73 buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f)); 74 buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f)); 75 } 76 } 77 78 _index += length; 79 80 return length > 0 ? length : -1; 81 } 82 83 public int getAvailable() throws IOException 84 { 85 return _length - _index; 86 } 87 } 88 | Popular Tags |