1 package org.catacomb.util;
2
3
4
5 import org.catacomb.interlish.structure.ProgressReport;
6 import org.catacomb.report.E;
7
8 import java.io.*;
9 import java.net.URL;
10
11
12
13 public class NetUtil {
14
15
16
17 public static byte[] readHeader(URL url, int n) {
18 byte[] ret = null;
19 try {
20 InputStream ins = url.openStream();
21 ret = new byte[n];
22 int nread = ins.read(ret);
23 if (nread != n) {
24 E.error("readNBytes wanted " + n + " but got " + nread);
25 }
26 ins.close();
27 } catch (Exception ex) {
28 E.error("readNBytes problem " + ex);
29 }
30 return ret;
31 }
32
33
34
35 public static byte[] readBytes(URL u) {
36 byte[] ret = null;
37 try {
38 InputStream ins = u.openStream();
39 BufferedInputStream bis = new BufferedInputStream(ins);
40
41 ByteArrayOutputStream baos = new ByteArrayOutputStream();
42
43 byte[] bb = new byte[4096];
44 int nread = bis.read(bb);
45 while (nread > 0) {
46 baos.write(bb, 0, nread);
47 nread = bis.read(bb);
48 }
49 ret = baos.toByteArray();
50 ins.close();
51
52 } catch (Exception ex) {
53 E.error("readNBytes problem " + ex);
54 }
55 return ret;
56 }
57
58
59
60 public static String readStringFromURL(URL url) {
61 return readStringFromURL(url, null);
62 }
63
64
65 public static String readStringFromURL(String surl, ProgressReport report) {
66 String ret = null;
67 try {
68 URL url = new URL(surl);
69 ret = readStringFromURL(url, report);
70 } catch (Exception ex) {
71 E.error("url conversion failed for " + surl + " " + ex);
72 }
73 return ret;
74 }
75
76
77
78 public static String readStringFromURL(URL url, ProgressReport report) {
79
80 StringBuffer sb = new StringBuffer();
81 try {
82 InputStream in = url.openStream();
83 BufferedReader bis = new BufferedReader(new InputStreamReader(in));
84
85 String inline;
86 if (report != null) {
87 report.setText("reading " + url);
88 report.update();
89 }
90 int nline = 0;
91
92 while ((inline = bis.readLine()) != null) {
93 sb.append(inline);
94 sb.append("\n");
95 nline += 1;
96
97 if (report != null && (nline % 100) == 0) {
98 report.setText("line " + nline);
99 report.update();
100 }
101 }
102
103 if (report != null) {
104 report.setFraction(1.0);
105 report.update();
106 }
107
108 } catch (Exception ex) {
109 E.error("URL read error " + ex);
110
111 }
112 return sb.toString();
113 }
114
115
116
117
118 }