001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package communications;
012:
013: import util.ReportError;
014:
015: import java.io.DataInputStream;
016: import java.io.InputStream;
017: import java.net.URL;
018: import java.net.URLConnection;
019:
020: import java.io.IOException;
021: import java.lang.NullPointerException;
022: import java.net.MalformedURLException;
023:
024: /**
025: * Manage "read" style server connections.
026: * <p>
027: * Note that currently the internal URLConnection
028: * subverts defaults by doing <i>setAllowUserInteraction(true)</i>
029: * and <i>setUseCaches(false)</i>.
030: * Eventually it will be possible to set these differently,
031: * currently they are hard coded this way.
032: */
033: public class ReadConnection {
034: /**
035: * The URL string.
036: */
037: protected String url_string;
038:
039: /**
040: * The URL.
041: */
042: protected URL url;
043:
044: /**
045: * The URL connection.
046: */
047: protected URLConnection urlc;
048:
049: /**
050: * The input stream.
051: */
052: protected InputStream is;
053:
054: /**
055: * The data input stream.
056: */
057: protected DataInputStream dis;
058:
059: /**
060: * Indicates if there is an established data input stream.
061: */
062: protected boolean validIn;
063:
064: /**
065: * Indicates if there is an established connection.
066: */
067: protected boolean validCon;
068:
069: /**
070: * Constructs a ReadConnection instance.
071: * @param s the URL string
072: */
073: public ReadConnection(String s) {
074: url_string = s;
075: url = null;
076: urlc = null;
077: is = null;
078: dis = null;
079: validIn = false;
080: validCon = false;
081: }
082:
083: /**
084: * Set the URL.
085: * @param s the URL string
086: */
087: public void setURL(String s) {
088: url_string = new String(s);
089: validIn = false;
090: }
091:
092: /**
093: * Return whether the connection is valid.
094: */
095: public boolean isValidCon() {
096: return validCon;
097: }
098:
099: /**
100: * Open the connection.
101: */
102: protected boolean openCon() {
103: if (validCon)
104: return true;
105:
106: try {
107: url = new URL(url_string);
108: } catch (MalformedURLException e) {
109: ReportError.reportError(util.ReportError.INTERNAL,
110: "ReadConnection", "openCon", Messages.MALFORMEDURL);
111: return false;
112: }
113:
114: try {
115: urlc = url.openConnection();
116: urlc.setAllowUserInteraction(true);
117: urlc.setUseCaches(false);
118: } catch (IOException e) {
119: ReportError.reportError(util.ReportError.INTERNAL,
120: "ReadConnection", "openCon",
121: Messages.URLCONNECTIONOPENFAIL);
122: return false;
123: }
124:
125: validCon = true;
126: return true;
127: }
128:
129: /**
130: * Return whether the input stream is valid.
131: */
132: public boolean isValidIn() {
133: return validIn;
134: }
135:
136: /**
137: * Open the input stream.
138: */
139: public boolean openIn() {
140: if (validIn)
141: return true;
142:
143: openCon();
144:
145: try {
146: is = urlc.getInputStream();
147: } catch (IOException e) {
148: ReportError.reportError(util.ReportError.INTERNAL,
149: "ReadConnection", "openIn",
150: Messages.DATAINPUTSTREAMOPENFAIL);
151: return false;
152: }
153:
154: dis = new DataInputStream(is);
155:
156: validIn = true;
157: return true;
158: }
159:
160: /**
161: * Close the input stream.
162: */
163: public boolean closeIn() {
164: if (!validIn)
165: return true;
166:
167: validIn = false;
168: validCon = false;
169:
170: try {
171: dis.close();
172: } catch (IOException e) {
173: ReportError.reportError(util.ReportError.INTERNAL,
174: "ReadConnection", "closeIn",
175: Messages.DATAINPUTSTREAMCLOSEFAIL);
176: return false;
177: }
178:
179: return true;
180: }
181:
182: /**
183: * Read a line from the input stream.
184: * @deprecated 3.01C
185: */
186: public String readLine() {
187: String s;
188:
189: if (!validIn)
190: return null;
191:
192: try {
193: s = dis.readLine();
194: } catch (IOException e) {
195: ReportError.reportError(util.ReportError.INTERNAL,
196: "ReadConnection", "readLine",
197: Messages.DATAINPUTSTREAMREADFAIL);
198: return null;
199: }
200:
201: return s;
202: }
203:
204: /**
205: * Read the entire input stream. Use with discretion!
206: */
207: public String doRead() {
208:
209: try {
210:
211: if (openIn() == false)
212: return null;
213:
214: int r = 0, n = 0;
215: byte b[] = new byte[10000];
216: String s = "";
217:
218: while (r != -1) {
219: if ((r = is.read(b, n, b.length - n)) != -1)
220: n += r;
221: if (r == -1 || n == b.length) {
222: s = s.concat(new String(b, 0, 0, n));
223: n = 0;
224: // Buffer strategy is rapid growth with plateau.
225: // StringBuffer is too wasteful at large sizes
226: // - esp with 4M applet array limits lurking...
227: if (b.length == 10000)
228: b = new byte[100000];
229: else if (b.length == 100000)
230: b = new byte[1000000];
231: }
232: //System.out.println(""+r+" "+n+" "+b.length+" "+s.length());
233: }
234:
235: closeIn();
236: return s;
237:
238: } catch (Exception e) {
239: ReportError.reportError(util.ReportError.INTERNAL,
240: "ReadConnection", "doRead",
241: Messages.DATAINPUTSTREAMREADFAIL);
242: }
243:
244: return null;
245: }
246:
247: /**
248: * Returns the input stream
249: * @deprecated 3.01C
250: */
251: public DataInputStream getInputStream() {
252: return dis;
253: }
254:
255: /**
256: * Returns the input stream
257: */
258: public InputStream getIS() {
259: return is;
260: }
261:
262: }
|