114 |
URLConnection connection = url.openConnection(); |
URLConnection connection = url.openConnection(); |
115 |
|
|
116 |
connection.setConnectTimeout(timeout); |
connection.setConnectTimeout(timeout); |
117 |
|
connection.setReadTimeout(timeout); |
118 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
119 |
|
|
120 |
return readInputStream(is); |
return readInputStream(is); |
138 |
|
|
139 |
static byte[] readInputStream(InputStream is) throws IOException{ |
static byte[] readInputStream(InputStream is) throws IOException{ |
140 |
byte buffer[] = new byte[1024]; |
byte buffer[] = new byte[1024]; |
141 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
//try /; //start buffer size - instead of default 32bytes |
142 |
try { |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) { |
143 |
int count; |
int count; |
144 |
|
|
145 |
while ( (count = is.read(buffer)) != -1) { |
while ( (count = is.read(buffer)) != -1) { |
146 |
baos.write(buffer, 0, count); |
baos.write(buffer, 0, count); |
147 |
} |
} |
148 |
} finally { |
is.close(); |
149 |
try { |
return baos.toByteArray(); |
150 |
is.close(); |
} |
|
baos.close(); |
|
|
} catch (Exception e) {} //never mind if close throws an exception |
|
|
} |
|
|
|
|
|
return baos.toByteArray(); |
|
151 |
} |
} |
152 |
|
|
153 |
} |
} |