1 package org.cacert.gigi.testUtils;
3 import java.io.ByteArrayOutputStream;
4 import java.io.CharArrayWriter;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
9 import java.net.HttpURLConnection;
10 import java.net.URLConnection;
12 public class IOUtils {
18 public static String readURL(URLConnection in) {
20 if ( !in.getContentType().equals("text/html; charset=UTF-8") && !in.getContentType().equals("text/plain; charset=UTF-8") && !in.getContentType().equals("application/json; charset=UTF-8")) {
21 if (in instanceof HttpURLConnection && ((HttpURLConnection) in).getResponseCode() != 200) {
22 System.err.println(readURL(new InputStreamReader(((HttpURLConnection) in).getErrorStream(), "UTF-8")));
24 throw new Error("Unrecognized content-type: " + in.getContentType());
26 return readURL(new InputStreamReader(in.getInputStream(), "UTF-8"));
27 } catch (IOException e) {
33 public static String readURL(Reader in) {
34 CharArrayWriter caw = new CharArrayWriter();
35 char[] buffer = new char[1024];
38 while ((len = in.read(buffer)) > 0) {
39 caw.write(buffer, 0, len);
42 return new String(caw.toCharArray());
43 } catch (IOException e) {
49 public static byte[] readURL(InputStream in) {
50 ByteArrayOutputStream baos = new ByteArrayOutputStream();
51 byte[] buffer = new byte[1024];
54 while ((len = in.read(buffer)) > 0) {
55 baos.write(buffer, 0, len);
58 return baos.toByteArray();
59 } catch (IOException e) {