1 8 9 package com.caucho.eswrap.java.io; 10 11 import com.caucho.util.CharBuffer; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 public class InputStreamEcmaWrap { 17 public static int readByte(InputStream is) 18 throws IOException 19 { 20 return is.read(); 21 } 22 23 public static String read(InputStream is) 24 throws IOException 25 { 26 int ch = is.read(); 27 28 if (ch == -1) 29 return null; 30 31 return String.valueOf((char) ch); 32 } 33 34 public static String read(InputStream is, int length) 35 throws IOException 36 { 37 CharBuffer bb = new CharBuffer(); 38 39 for (; length > 0; length--) { 40 int ch = is.read(); 41 42 if (ch == -1) 43 break; 44 45 bb.append((char) ch); 46 } 47 48 if (bb.length() == 0) 49 return null; 50 51 return bb.toString(); 52 } 53 54 public static String readln(InputStream is) 55 throws IOException 56 { 57 CharBuffer bb = new CharBuffer(); 58 59 boolean hasCr = false; 60 boolean hasData = false; 61 while (true) { 62 int ch = is.read(); 63 64 if (ch == -1) 65 break; 66 else if (ch == '\n') { 67 hasData = true; 68 break; 69 } 70 else if (hasCr) { 71 hasData = true; 72 break; 73 } 74 75 if (ch == '\r') 76 hasCr = true; 77 else 78 bb.append((char) ch); 79 } 80 81 if (bb.length() == 0 && ! hasData) 82 return null; 83 84 return bb.toString(); 85 } 86 } 87 88 | Popular Tags |