@Override public void withHttpURLConnection(OutputStream outputStream) throws IOException { IOUtils.copy(inputStream, outputStream); inputStream.close(); outputStream.close(); } };
public static byte[] readToBytes(InputStream inputStream) throws IOException { if (inputStream == null) { return new byte[0]; } final ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(inputStream, output); return output.toByteArray(); }
private static File createTempDispatcherJar() throws IOException { final InputStream input = AgentAttacher.class.getClassLoader() .getResourceAsStream("stagemonitor-dispatcher.jar.gradlePleaseDontExtract"); if (input == null) { throw new IllegalStateException("If you see this exception inside you IDE, you have to execute gradle " + "processResources before running the tests."); } final File tempDispatcherJar = File.createTempFile("stagemonitor-dispatcher", ".jar"); tempDispatcherJar.deleteOnExit(); IOUtils.copy(input, new FileOutputStream(tempDispatcherJar)); return tempDispatcherJar; }
@Override public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded, DynamicType dynamicType) { if (!exportClassesWithName.contains(typeDescription.getName())) { return; } final File exportedClass; try { exportedClass = File.createTempFile(typeDescription.getName(), ".class"); IOUtils.copy(new ByteArrayInputStream(dynamicType.getBytes()), new FileOutputStream(exportedClass)); logger.info("Exported class modified by Byte Buddy: {}", exportedClass.getAbsolutePath()); exportedClasses.add(exportedClass.getAbsolutePath()); } catch (IOException e) { logger.warn(e.getMessage(), e); } } }
@Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String requestURI = req.getRequestURI().substring(req.getContextPath().length()).replace("..", ""); setResponseHeaders(req, res, requestURI); InputStream inputStream = null; try { inputStream = getClass().getClassLoader().getResourceAsStream(StringUtils.removeStart(requestURI, "/")); if (inputStream == null) { res.sendError(HttpServletResponse.SC_NOT_FOUND); return; } IOUtils.copy(inputStream, res.getOutputStream()); res.getOutputStream().flush(); res.flushBuffer(); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ioe) { // ignore } } }
@Test public void testCopy() throws IOException { final byte[] array = new byte[] { 1, 2, 3 }; final ByteArrayInputStream bios = new ByteArrayInputStream(array); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(bios, baos); Assert.assertArrayEquals(array, baos.toByteArray()); }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { IOUtils.copy(IOUtils.getResourceAsStream("wsdl.xml"), response.getOutputStream()); baseRequest.setHandled(true); } });