KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > java > io > InputStreamEcmaWrap


1 /*
2  * Copyright (c) 1998-199 Caucho Technology -- all rights reserved
3  *
4  * @author Scott Ferguson
5  *
6  * $Id: InputStreamEcmaWrap.java,v 1.1.1.1 2004/09/11 05:14:19 cvs Exp $
7  */

8
9 package com.caucho.eswrap.java.io;
10
11 import com.caucho.util.CharBuffer;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 public class InputStreamEcmaWrap {
17   public static int readByte(InputStream JavaDoc is)
18   throws IOException JavaDoc
19   {
20     return is.read();
21   }
22
23   public static String JavaDoc read(InputStream JavaDoc is)
24   throws IOException JavaDoc
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 JavaDoc read(InputStream JavaDoc is, int length)
35   throws IOException JavaDoc
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 JavaDoc readln(InputStream JavaDoc is)
55   throws IOException JavaDoc
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