private void copy(String resource, String dir) throws IOException { String fileName = dir + resource.substring(resource.lastIndexOf("/")); File outputFile = new File(fileName); if (outputFile.createNewFile()) { InputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = getClass().getResourceAsStream(resource); outputStream = new FileOutputStream(outputFile); IOUtils.copy(inputStream, outputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { // do nothing } } if (outputStream != null) { try { outputStream.close(); } catch (Exception e) { // do nothing } } } } }
private void copy(String resource, String dir) throws IOException { String fileName = dir + resource.substring(resource.lastIndexOf("/")); File outputFile = new File(fileName); if (outputFile.createNewFile()) { InputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = getClass().getResourceAsStream(resource); outputStream = new FileOutputStream(outputFile); IOUtils.copy(inputStream, outputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { // do nothing } } if (outputStream != null) { try { outputStream.close(); } catch (Exception e) { // do nothing } } } } }
public String getParameter(String name) throws IOException { if (!paramsChecked) { try { String contentType = getContentType(); if ("application/x-www-form-urlencoded".equals(contentType)) { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(getInputStream(), out); collectParameters(out.toString(), params); } } finally { paramsChecked = true; } } return params.get(name); }
/** * Execute the request. * * @throws IOException if an I/O error occurs */ private byte[] execute() throws IOException { HttpExecutor executor = new HttpExecutor(socketFactory, socketAddress); try { InputStream stream = executor.execute(command, params, in); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); IOUtils.copy(stream, buffer); return buffer.toByteArray(); } finally { stream.close(); } } finally { executor.close(); } }
private void createOak16GCLog() throws IOException { try (InputStream source = GcJournalTest.class.getResourceAsStream("oak-1.6-gc.log")) { try (FileOutputStream target = new FileOutputStream(segmentFolder.newFile("gc.log"))) { IOUtils.copy(source, target); } } }
public void testCopyStream() throws IOException { final Random r = new Random(1); for (int length : Lists.newArrayList(0, 1, 1000, 4096, 4097, 1024*1024)) { byte[] inData = new byte[length]; r.nextBytes(inData); ByteArrayInputStream in = new ByteArrayInputStream(inData); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out); assertEquals(inData, out.toByteArray()); } }
@Override public void service(Request request, Response response) throws IOException { String file = request.getFile(); if (file.endsWith("/")) { file += "index.html"; } InputStream in = FileServlet.class.getResourceAsStream(file.substring(1)); if (in != null) { try { int dotIndex = file.lastIndexOf('.'); if (dotIndex != -1) { String contentType = MIME_TYPES.get(file.substring(dotIndex + 1)); if (contentType == null) { contentType = "application/octet-stream"; } response.setContentType(contentType); } IOUtils.copy(in, response.getOutputStream()); } finally { IOUtils.closeQuietly(in); } } else { response.setStatusCode(404); } }
bodyOut.write("Content-Type: application/octet-stream\r\n".getBytes()); bodyOut.write("\r\n".getBytes()); IOUtils.copy(in, bodyOut); bodyOut.write("\r\n".getBytes()); bodyOut.write("--".getBytes());
/** * Uploads data via HTTP put to the provided URI. * * @param uri The URI to upload to. * @param contentLength Value to set in the Content-Length header. * @param in - The input stream to upload. * @return HTTP response code from the upload request. Note that a successful * response for S3 is 200 - OK whereas for Azure it is 201 - Created. * @throws IOException */ public static int httpPut(@Nullable URI uri, long contentLength, InputStream in) throws IOException { // this weird combination of @Nullable and assertNotNull() is for IDEs not warning in test methods assertNotNull(uri); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Length", String.valueOf(contentLength)); connection.setRequestProperty("Date", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX") .withZone(ZoneOffset.UTC) .format(Instant.now())); OutputStream putStream = connection.getOutputStream(); IOUtils.copy(in, putStream); putStream.close(); return connection.getResponseCode(); }
@Override public void execute(MicroKernel mk, Request request, Response response) throws IOException, MicroKernelException { String blobId = request.getParameter("blob_id", ""); long pos = request.getParameter("pos", 0L); int length = request.getParameter("length", -1); if (request.getUserAgent() == null) { // let browsers guess the correct file format response.setContentType("application/octet-stream"); } OutputStream out = response.getOutputStream(); if (pos == 0L && length == -1) { /* return the complete binary */ InputStream in = new MicroKernelInputStream(mk, blobId); IOUtils.copy(in, out); } else { /* return some range */ byte[] buff = new byte[length]; int count = mk.read(blobId, pos, buff, 0, length); if (count > 0) { out.write(buff, 0, count); } } } }