KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > net > HttpUtils


1 /*
2  * HttpUtils.java
3  *
4  * Copyright (c) 2000 Walt Disney Internet Group. All Rights Reserved.
5  *
6  * Original author: Brian S O'Neill
7  *
8  * $Workfile:: HttpUtils.java $
9  * $Author:: Briano $
10  * $Revision:: 2 $
11  * $Date:: 01/04/03 6:41p $
12  */

13
14 package com.go.trove.net;
15
16 import java.io.*;
17
18 /******************************************************************************
19  *
20  * @author Brian S O'Neill
21  * @version
22  * <!--$$Revision:--> 2 <!-- $-->, <!--$$JustDate:--> 01/04/03 <!-- $-->
23  */

24 public class HttpUtils {
25     /**
26      * Reads a line from an HTTP InputStream, using the given buffer for
27      * temporary storage.
28      *
29      * @param in stream to read from
30      * @param buffer temporary buffer to use
31      * @throws IllegalArgumentException if the given InputStream doesn't
32      * support marking
33      */

34     public static String JavaDoc readLine(InputStream in, byte[] buffer)
35         throws IllegalArgumentException JavaDoc, IOException
36     {
37         return readLine(in, buffer, -1);
38     }
39
40     /**
41      * Reads a line from an HTTP InputStream, using the given buffer for
42      * temporary storage.
43      *
44      * @param in stream to read from
45      * @param buffer temporary buffer to use
46      * @throws IllegalArgumentException if the given InputStream doesn't
47      * support marking
48      * @throws LineTooLongException when line is longer than the limit
49      */

50     public static String JavaDoc readLine(InputStream in, byte[] buffer, int limit)
51         throws IllegalArgumentException JavaDoc, IOException, LineTooLongException
52     {
53         if (!in.markSupported()) {
54             throw new IllegalArgumentException JavaDoc
55                 ("InputStream doesn't support marking: " + in.getClass());
56         }
57
58         String JavaDoc line = null;
59
60         int cursor = 0;
61         int len = buffer.length;
62
63         int count = 0;
64         int c;
65     loop:
66         while ((c = in.read()) >= 0) {
67             if (limit >= 0 && ++count > limit) {
68                 throw new LineTooLongException(limit);
69             }
70
71             switch (c) {
72             case '\r':
73                 in.mark(1);
74                 if (in.read() != '\n') {
75                     in.reset();
76                 }
77                 // fall through
78
case '\n':
79                 if (line == null && cursor == 0) {
80                     return "";
81                 }
82                 break loop;
83             default:
84                 if (cursor >= len) {
85                     if (line == null) {
86                         line = new String JavaDoc(buffer, "8859_1");
87                     }
88                     else {
89                         line = line.concat(new String JavaDoc(buffer, "8859_1"));
90                     }
91                     cursor = 0;
92                 }
93                 buffer[cursor++] = (byte)c;
94             }
95         }
96
97         if (cursor > 0) {
98             if (line == null) {
99                 line = new String JavaDoc(buffer, 0, cursor, "8859_1");
100             }
101             else {
102                 line = line.concat(new String JavaDoc(buffer, 0, cursor, "8859_1"));
103             }
104         }
105
106         return line;
107     }
108
109     /**
110      * Reads a line from an HTTP InputStream, using the given buffer for
111      * temporary storage.
112      *
113      * @param in stream to read from
114      * @param buffer temporary buffer to use
115      * @throws IllegalArgumentException if the given InputStream doesn't
116      * support marking
117      */

118     public static String JavaDoc readLine(InputStream in, char[] buffer)
119         throws IllegalArgumentException JavaDoc, IOException
120     {
121         return readLine(in, buffer, -1);
122     }
123
124     /**
125      * Reads a line from an HTTP InputStream, using the given buffer for
126      * temporary storage.
127      *
128      * @param in stream to read from
129      * @param buffer temporary buffer to use
130      * @throws IllegalArgumentException if the given InputStream doesn't
131      * support marking
132      * @throws LineTooLongException when line is longer than the limit
133      */

134     public static String JavaDoc readLine(InputStream in, char[] buffer, int limit)
135         throws IllegalArgumentException JavaDoc, IOException, LineTooLongException
136     {
137         if (!in.markSupported()) {
138             throw new IllegalArgumentException JavaDoc
139                 ("InputStream doesn't support marking: " + in.getClass());
140         }
141
142         String JavaDoc line = null;
143
144         int cursor = 0;
145         int len = buffer.length;
146
147         int count = 0;
148         int c;
149     loop:
150         while ((c = in.read()) >= 0) {
151             if (limit >= 0 && ++count > limit) {
152                 throw new LineTooLongException(limit);
153             }
154
155             switch (c) {
156             case '\r':
157                 in.mark(1);
158                 if (in.read() != '\n') {
159                     in.reset();
160                 }
161                 // fall through
162
case '\n':
163                 if (line == null && cursor == 0) {
164                     return "";
165                 }
166                 break loop;
167             default:
168                 if (cursor >= len) {
169                     if (line == null) {
170                         line = new String JavaDoc(buffer);
171                     }
172                     else {
173                         line = line.concat(new String JavaDoc(buffer));
174                     }
175                     cursor = 0;
176                 }
177                 buffer[cursor++] = (char)c;
178             }
179         }
180
181         if (cursor > 0) {
182             if (line == null) {
183                 line = new String JavaDoc(buffer, 0, cursor);
184             }
185             else {
186                 line = line.concat(new String JavaDoc(buffer, 0, cursor));
187             }
188         }
189
190         return line;
191     }
192 }
193
Popular Tags