01: /*
02: * Copyright (c) 1998-199 Caucho Technology -- all rights reserved
03: *
04: * @author Scott Ferguson
05: *
06: * $Id: InputStreamEcmaWrap.java,v 1.1.1.1 2004/09/11 05:14:19 cvs Exp $
07: */
08:
09: 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) throws IOException {
18: return is.read();
19: }
20:
21: public static String read(InputStream is) throws IOException {
22: int ch = is.read();
23:
24: if (ch == -1)
25: return null;
26:
27: return String.valueOf((char) ch);
28: }
29:
30: public static String read(InputStream is, int length)
31: throws IOException {
32: CharBuffer bb = new CharBuffer();
33:
34: for (; length > 0; length--) {
35: int ch = is.read();
36:
37: if (ch == -1)
38: break;
39:
40: bb.append((char) ch);
41: }
42:
43: if (bb.length() == 0)
44: return null;
45:
46: return bb.toString();
47: }
48:
49: public static String readln(InputStream is) throws IOException {
50: CharBuffer bb = new CharBuffer();
51:
52: boolean hasCr = false;
53: boolean hasData = false;
54: while (true) {
55: int ch = is.read();
56:
57: if (ch == -1)
58: break;
59: else if (ch == '\n') {
60: hasData = true;
61: break;
62: } else if (hasCr) {
63: hasData = true;
64: break;
65: }
66:
67: if (ch == '\r')
68: hasCr = true;
69: else
70: bb.append((char) ch);
71: }
72:
73: if (bb.length() == 0 && !hasData)
74: return null;
75:
76: return bb.toString();
77: }
78: }
|