X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FIOUtils.java;h=61cd17840c88c659f225c5ee08b54af5a2dd4243;hp=b7452d2a4422d8acc1fd18d6d8b6fc54fa2e6c64;hb=a09f042a230804825c3d22726dce65e7b8ed814e;hpb=2824d1c165c501e2f3a8809044788b33b81f478a diff --git a/tests/org/cacert/gigi/testUtils/IOUtils.java b/tests/org/cacert/gigi/testUtils/IOUtils.java index b7452d2a..61cd1784 100644 --- a/tests/org/cacert/gigi/testUtils/IOUtils.java +++ b/tests/org/cacert/gigi/testUtils/IOUtils.java @@ -1,40 +1,63 @@ package org.cacert.gigi.testUtils; +import java.io.ByteArrayOutputStream; import java.io.CharArrayWriter; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.net.HttpURLConnection; import java.net.URLConnection; public class IOUtils { - private IOUtils() { - - } - - public static String readURL(URLConnection in) { - try { - if (!in.getContentType().equals("text/html; charset=UTF-8")) { - throw new Error("Unrecognized content-type: " + in.getContentType()); - } - return readURL(new InputStreamReader(in.getInputStream(), "UTF-8")); - } catch (IOException e) { - throw new Error(e); - } - - } - - public static String readURL(Reader in) { - CharArrayWriter caw = new CharArrayWriter(); - char[] buffer = new char[1024]; - int len = 0; - try { - while ((len = in.read(buffer)) > 0) { - caw.write(buffer, 0, len); - } - return new String(caw.toCharArray()); - } catch (IOException e) { - throw new Error(e); - } - - } + + private IOUtils() { + + } + + public static String readURL(URLConnection in) { + try { + 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")) { + if (in instanceof HttpURLConnection && ((HttpURLConnection) in).getResponseCode() != 200) { + System.err.println(readURL(new InputStreamReader(((HttpURLConnection) in).getErrorStream(), "UTF-8"))); + } + throw new Error("Unrecognized content-type: " + in.getContentType()); + } + return readURL(new InputStreamReader(in.getInputStream(), "UTF-8")); + } catch (IOException e) { + throw new Error(e); + } + + } + + public static String readURL(Reader in) { + CharArrayWriter caw = new CharArrayWriter(); + char[] buffer = new char[1024]; + int len = 0; + try { + while ((len = in.read(buffer)) > 0) { + caw.write(buffer, 0, len); + } + in.close(); + return new String(caw.toCharArray()); + } catch (IOException e) { + throw new Error(e); + } + + } + + public static byte[] readURL(InputStream in) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + try { + while ((len = in.read(buffer)) > 0) { + baos.write(buffer, 0, len); + } + in.close(); + return baos.toByteArray(); + } catch (IOException e) { + throw new Error(e); + } + } }