/** * Gets the contents of a <code>URL</code> as a <code>byte[]</code>. * * @param url the <code>URL</code> to read * @return the requested byte array * @throws NullPointerException if the input is null * @throws IOException if an I/O exception occurs * @since 2.4 */ public static byte[] toByteArray(final URL url) throws IOException { final URLConnection conn = url.openConnection(); try { return IOUtils.toByteArray(conn); } finally { close(conn); } }
/** * 通过 URL 读取文本 * * @param url 地址 * @param size 大小 * @return 文本 */ public static String getStringFromURL(String url, int size) { URLConnection conn = null; BufferedInputStream bin = null; try { conn = new URL(url).openConnection(); bin = new BufferedInputStream(conn.getInputStream()); return getStringFromInputStream(bin, size, conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding()); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.close(conn); IOUtils.closeQuietly(bin); } return null; }
return "[IP ERROR]"; } finally { IOUtils.close(con); IOUtils.closeQuietly(bufferedReader); IOUtils.closeQuietly(inputStreamReader);
@Test public void testToByteArray_URLConnection() throws Exception { final URLConnection urlConn = m_testFile.toURI().toURL().openConnection(); byte[] actual; try { actual = IOUtils.toByteArray(urlConn); } finally { IOUtils.close(urlConn); } assertEquals(FILE_SIZE, actual.length); }
@VisibleForTesting void closeInputStream(RunnerState rs) throws IOException { if (in != null) { IOUtils.close(cachedConnection); in = null; } cachedConnection = null; runnerState = rs; }
@VisibleForTesting void closeInputStream(RunnerState rs) throws IOException { if (in != null) { IOUtils.close(cachedConnection); in = null; } cachedConnection = null; runnerState = rs; }
/** * Gets the contents of a <code>URL</code> as a <code>byte[]</code>. * * @param url the <code>URL</code> to read * @return the requested byte array * @throws NullPointerException if the input is null * @throws IOException if an I/O exception occurs * @since 2.4 */ public static byte[] toByteArray(final URL url) throws IOException { final URLConnection conn = url.openConnection(); try { return IOUtils.toByteArray(conn); } finally { close(conn); } }
/** * Gets the contents of a <code>URL</code> as a <code>byte[]</code>. * * @param url the <code>URL</code> to read * @return the requested byte array * @throws NullPointerException if the input is null * @throws IOException if an I/O exception occurs * @since 2.4 */ public static byte[] toByteArray(final URL url) throws IOException { final URLConnection conn = url.openConnection(); try { return IOUtils.toByteArray(conn); } finally { close(conn); } }
/** * Get the contents of a <code>URL</code> as a <code>byte[]</code>. * * @param url * the <code>URL</code> to read * @return the requested byte array * @throws NullPointerException * if the input is null * @throws IOException * if an I/O exception occurs * @since 2.4 */ public static byte[] toByteArray(URL url) throws IOException { URLConnection conn = url.openConnection(); try { return IOUtils.toByteArray(conn); } finally { close(conn); } }
/** * Retrieve a {@link Response} from a request, that was sent through * {@link HttpURLConnection}. * * @param connection {@link HttpURLConnection}, that contains the server response * @return {@link Response} of the request * @throws IOException if the {@link Response} can't be obtained */ protected Response createResponse(HttpURLConnection connection) throws IOException { //final String encoding = StringUtils.defaultIfBlank( StringUtils.trimToNull( connection.getContentEncoding() ), getEncoding() ); final String encoding = getEncoding(); try (InputStream input = ("gzip".equalsIgnoreCase(connection.getContentEncoding())) ? new GZIPInputStream(new BufferedInputStream(connection.getInputStream())) : new BufferedInputStream(connection.getInputStream())) { // create response return new Response( connection.getResponseCode(), connection.getResponseMessage(), connection.getHeaderField(RESPONSE_HEADER_REQUEST_REFNUM), IOUtils.toString(input, encoding)); } finally { IOUtils.close(connection); } }
/** * @param repoURL - URL on the repo * @param fullDebug - should this dump the full cloudflare debug info in the console * @return boolean representing if the file exists */ public static boolean CloudFlareInspector (String repoURL, boolean fullDebug) { try { boolean ret; HttpURLConnection connection = (HttpURLConnection) new URL(repoURL + "cdn-cgi/trace").openConnection(); if (!fullDebug) { connection.setRequestMethod("HEAD"); } Logger.logDebug("CF-RAY: " + connection.getHeaderField("CF-RAY")); if (fullDebug) { Logger.logDebug("CF Debug Info: \n" + IOUtils.toString(connection.getInputStream())); } ret = connection.getResponseCode() == 200; IOUtils.close(connection); return ret; } catch (Exception e) { return false; } }
private void sendUsageStatistics(Map<String, String> parameters) { final Logger logger = getLogger(); HttpURLConnection urlConnection = null; try { urlConnection = openUrlConnection(); urlConnection.setAllowUserInteraction(false); urlConnection.setConnectTimeout(2000); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setInstanceFollowRedirects(false); urlConnection.setReadTimeout(5000); urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); urlConnection.connect(); writeParameters(parameters, urlConnection); processResponse(urlConnection); } catch (IOException e) { logger.debug("Failed to send statistics!", e); logger.info("Failed sending usage statistics."); } finally { IOUtils.close(urlConnection); } }
IOUtils.close(connection);
IOUtils.close(connection);
IOUtils.close(connection);