Example usage for org.apache.commons.io IOUtils toByteArray

List of usage examples for org.apache.commons.io IOUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toByteArray.

Prototype

public static byte[] toByteArray(String input) throws IOException 

Source Link

Document

Get the contents of a String as a byte[] using the default character encoding of the platform.

Usage

From source file:com.imag.nespros.network.devices.AMIDevice.java

public AMIDevice(String name) {
    super(name);//from   w  w  w  .j  a v a2  s.c om
    this.setCpuSpeed(10);
    this.setTotalMemory(128);
    this.setDeviceType(DeviceType.AMI);
    this.setDeviceName(name);
    //String imageURI = getClass().getClassLoader().getResource("image"+File.separator+"meter.jpeg").getFile();
    //icon = new MyLayeredIcon(new ImageIcon("icons"+File.separator+"meter.jpeg").getImage());
    // icon = new MyLayeredIcon(new ImageIcon(imageURI).getImage());
    try {
        byte[] imageInByte;
        imageInByte = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("image/meter.jpeg"));
        icon = new MyLayeredIcon(new ImageIcon(imageInByte).getImage());
    } catch (IOException ex) {
        Logger.getLogger(AMIDevice.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.samczsun.helios.utils.Utils.java

public static String readProcess(Process process) {
    StringBuilder result = new StringBuilder();
    result.append("--- BEGIN PROCESS DUMP ---").append("\n");
    result.append("---- STDOUT ----").append("\n");
    InputStream inputStream = process.getInputStream();
    byte[] inputStreamBytes = new byte[0];
    try {/*w ww  .  java 2 s  .c  o m*/
        inputStreamBytes = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        result.append("An error occured while reading from stdout").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (inputStreamBytes.length > 0) {
            result.append(new String(inputStreamBytes, StandardCharsets.UTF_8));
        }
    }
    result.append("---- STDERR ----").append("\n");
    inputStream = process.getErrorStream();
    inputStreamBytes = new byte[0];
    try {
        inputStreamBytes = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        result.append("An error occured while reading from stderr").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (inputStreamBytes.length > 0) {
            result.append(new String(inputStreamBytes, StandardCharsets.UTF_8));
        }
    }

    result.append("---- EXIT VALUE ----").append("\n");

    int exitValue = -0xCAFEBABE;
    try {
        exitValue = process.waitFor();
    } catch (InterruptedException e) {
        result.append("An error occured while obtaining the exit value").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (exitValue != -0xCAFEBABE) {
            result.append("Process finished with exit code ").append(exitValue).append("\n");
        }
    }

    return result.toString();
}

From source file:cpcc.core.utils.JsonStreamResponseTest.java

@Test
public void shouldReturnJsonObjectAsStream() throws IOException {
    JSONObject obj = new JSONObject("{\"a\":10}");
    byte[] expected = obj.toCompactString().getBytes("UTF-8");

    JsonStreamResponse sut = new JsonStreamResponse(obj);

    byte[] actual = IOUtils.toByteArray(sut.getStream());

    assertThat(sut.getContentType()).isEqualTo("application/json");
    assertThat(actual).isEqualTo(expected);
}

From source file:com.sagalasan.eveapi.engine.RequestEngine.java

public static byte[] xmlRequest(XmlRequest xmlRequest) {
    try {/*from  ww w.  jav a2s . c  o m*/
        URL url = new URL(xmlRequest.buildUri());
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        return IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }

    return null;
}

From source file:hu.jozsef.vesza.so.servlets.MealImageService.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Long mealId = new Long(request.getParameter("identifier"));
    Meal fetchedMeal = objectify.load().type(Meal.class).id(mealId).now();

    if (fetchedMeal.getImage() != null) {
        String imageUrl = "/WEB-INF/" + fetchedMeal.getImage();
        String imageRealPath = this.getServletContext().getRealPath(imageUrl);
        InputStream imgStream = new FileInputStream(new File(imageRealPath));

        Blob image = new Blob(IOUtils.toByteArray(imgStream));
        response.setContentType("image/jpeg");
        response.getOutputStream().write(image.getBytes());
    } else {//from   w w  w  . jav  a  2 s . c  o m
        response.getWriter().print("No image");
    }
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testResponse() throws Exception {
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();/*from  ww  w.  j  ava 2s .c  om*/
    assertThat(data, is(mockData));
}

From source file:cpcc.core.utils.ResourceStreamResponseTest.java

@Test
public void shouldReturnResourceAsStream() throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    byte[] expected = IOUtils.toByteArray(classLoader.getResourceAsStream(PNG_RESOURCE_NAME));
    assertThat(expected).hasSize(731);//from ww  w .  ja v  a 2s .c om

    ResourceStreamResponse sut = new ResourceStreamResponse(CONTENT_TYPE_PNG, PNG_RESOURCE_NAME);

    byte[] actual = IOUtils.toByteArray(sut.getStream());

    assertThat(sut.getContentType()).isEqualTo(CONTENT_TYPE_PNG);
    assertThat(actual).isEqualTo(expected);
}

From source file:eu.europa.esig.dss.RemoteDocument.java

public RemoteDocument(DSSDocument document) throws IOException {
    this.bytes = IOUtils.toByteArray(document.openStream());
    this.mimeType = document.getMimeType();
    this.name = document.getName();
    this.absolutePath = document.getAbsolutePath();
}

From source file:edu.cornell.med.icb.goby.reads.ReadProtobuffCollectionHandler.java

@Override
public GeneratedMessage parse(final InputStream compressedBytes) throws IOException {
    final byte[] bytes = IOUtils.toByteArray(compressedBytes);
    final CodedInputStream codedInput = CodedInputStream.newInstance(bytes);
    codedInput.setSizeLimit(Integer.MAX_VALUE);

    return Reads.ReadCollection.parseFrom(codedInput);
}

From source file:hu.jozsef.vesza.so.servlets.LocationImageService.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Long eventId = new Long(request.getParameter("identifier"));
    Event fetchedEvent = objectify.load().type(Event.class).id(eventId).now();
    String imageUrl = "/WEB-INF/" + fetchedEvent.getLocation().getShortName() + ".png";
    String imageRealPath = this.getServletContext().getRealPath(imageUrl);
    InputStream imgStream = new FileInputStream(new File(imageRealPath));

    Blob image = new Blob(IOUtils.toByteArray(imgStream));
    if (image != null) {
        response.setContentType("image/jpeg");
        response.getOutputStream().write(image.getBytes());
    } else {/*from w w  w  .j  a va2  s. com*/
        response.getWriter().write("no image");
    }

}