Example usage for java.io ByteArrayOutputStream flush

List of usage examples for java.io ByteArrayOutputStream flush

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:mvc.controller.UsuarioController.java

private void setImagePath(List<Usuario> listaUsuarios) throws IOException {

    for (Usuario usuario : listaUsuarios) {

        BufferedImage bImage = ImageIO.read(new File(usuario.getPhoto()));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bImage, "jpg", baos);
        baos.flush();
        byte[] imageInByteArray = baos.toByteArray();
        baos.close();//from   w  w w.ja v  a 2s.  com
        String b64 = DatatypeConverter.printBase64Binary(imageInByteArray);
        usuario.setPhoto(b64);

    }

}

From source file:no.finntech.shootout.avro.AvroBase.java

@Override
@Benchmark/*from   www  . j  a  va  2 s  . c  o m*/
public ByteArrayOutputStream write() throws IOException, CompressorException {
    DatumWriter<AvroView> datumWriter = new SpecificDatumWriter<>(AvroView.class);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = getEncoder(out);
    datumWriter.write(getObject(), encoder);
    encoder.flush();
    out.flush();
    out.close();
    return out;
}

From source file:org.seedstack.seed.rest.hal.HalRepresentationTest.java

@Test
public void test_hal_link_serialization() throws Exception {
    HalRepresentation halRepresentation = new HalRepresentation();
    halRepresentation.link("objects", "/pok");
    halRepresentation.embedded("objects", Lists.newArrayList(new Person("toto")));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectMapper objectMapper = new ObjectMapper();
    outputStream.write(objectMapper.writeValueAsBytes(halRepresentation));
    outputStream.flush();

    Assertions.assertThat(outputStream.toString()).isEqualTo(EXPECTED);
}

From source file:guru.bubl.service.resources.vertex.VertexImageResource.java

private byte[] resizeImageToMaxWidth(File image, Integer width) {
    try {/*from w w  w . j  a  v a 2 s  . c o  m*/
        BufferedImage originalImage = ImageIO.read(image);
        originalImage = originalImage.getWidth() > width
                ? Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, width, width)
                : ImageIO.read(image);
        //To save with original ratio uncomment next line and comment the above.
        //originalImage= Scalr.resize(originalImage, 153, 128);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "png", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        return imageInByte;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:bq.jpa.demo.datetype.service.EmployeeService.java

private byte[] readImage(String fileName) throws IOException {
    BufferedImage pngfile = ImageIO.read(getClass().getResourceAsStream(fileName));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(pngfile, "png", baos);
    baos.flush();
    return baos.toByteArray();
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * Serializes the given object (zipped) to a byte array.
 * /*  w  w  w.j a v a2 s.com*/
 * @param serializable object to serialize
 * @return the serialized object as a byte array.
 */
public static byte[] serialize(Serializable serializable) {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gZipOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        gZipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        objectOutputStream = new ObjectOutputStream(gZipOutputStream);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        gZipOutputStream.finish();
        gZipOutputStream.flush();
        byteArrayOutputStream.flush();
    } catch (final IOException e) {
        throw new IllegalStateException("Couldn't serialize object", e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
        IOUtils.closeQuietly(gZipOutputStream);
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:com.ikon.module.common.CommonWorkflowModule.java

/**
 * Get Process Definition Image/* www .  j  av  a  2  s  .c om*/
 */
public static byte[] getProcessDefinitionImage(long processDefinitionId, String node) throws WorkflowException {
    log.debug("getProcessDefinitionImage({}, {})", new Object[] { processDefinitionId, node });
    JbpmContext jbpmContext = JBPMUtils.getConfig().createJbpmContext();
    byte[] image = null;

    try {
        GraphSession graphSession = jbpmContext.getGraphSession();
        org.jbpm.graph.def.ProcessDefinition pd = graphSession.getProcessDefinition(processDefinitionId);
        FileDefinition fileDef = pd.getFileDefinition();

        WorkflowUtils.DiagramInfo dInfo = WorkflowUtils.getDiagramInfo(fileDef.getInputStream("gpd.xml"));
        WorkflowUtils.DiagramNodeInfo dNodeInfo = dInfo.getNodeMap().get(node);
        BufferedImage img = ImageIO.read(fileDef.getInputStream("processimage.jpg"));

        // Obtain all nodes Y and X
        List<Integer> ordenadas = new ArrayList<Integer>();
        List<Integer> abcisas = new ArrayList<Integer>();

        for (WorkflowUtils.DiagramNodeInfo nodeInfo : dInfo.getNodeMap().values()) {
            ordenadas.add(nodeInfo.getY());
            abcisas.add(nodeInfo.getX());
        }

        // Calculate minimal Y
        Collections.sort(ordenadas);
        int fixOrd = ordenadas.get(0) < 0 ? ordenadas.get(0) : 0;

        // Calculate minimal X
        Collections.sort(abcisas);
        int fixAbs = abcisas.get(0) < 0 ? abcisas.get(0) : 0;

        if (dNodeInfo != null) {
            // Select node
            log.debug("DiagramNodeInfo: {}", dNodeInfo);
            Graphics g = img.getGraphics();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25F));
            g2d.setColor(Color.blue);
            g2d.fillRect(dNodeInfo.getX() - fixAbs, dNodeInfo.getY() - fixOrd, dNodeInfo.getWidth(),
                    dNodeInfo.getHeight());
            g.dispose();
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", baos);
        image = baos.toByteArray();
        baos.flush();
        baos.close();
    } catch (JbpmException e) {
        throw new WorkflowException(e.getMessage(), e);
    } catch (IOException e) {
        throw new WorkflowException(e.getMessage(), e);
    } finally {
        jbpmContext.close();
    }

    log.debug("getProcessDefinitionImage: {}", image);
    return image;
}

From source file:com.k42b3.kadabra.handler.Ftp.java

public byte[] getContent(String path) throws Exception {
    logger.info(basePath + "/" + path);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    client.retrieveFile(basePath + "/" + path, baos);

    baos.flush();
    baos.close();//from   w w w  .  j  a  v  a 2 s.c  om

    return baos.toByteArray();
}

From source file:org.mcplissken.repository.models.content.Content.java

public void writeAsImage(File result) throws IOException {

    BufferedImage originalImage = ImageIO.read(result);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ImageIO.write(originalImage, getExtension(), baos);

    baos.flush();

    byte[] imageInByte = baos.toByteArray();

    data = ByteBuffer.wrap(imageInByte);

    baos.close();/*from   w w w  .  j a  v a 2 s.co  m*/
}

From source file:org.opensaml.soap.client.soap11.decoder.http.impl.HttpClientResponseSOAP11DecoderTest.java

private HttpResponse buildResponse(int statusResponseCode, Envelope envelope)
        throws MarshallingException, IOException {
    BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, statusResponseCode, null);
    Element envelopeElement = XMLObjectSupport.marshall(envelope);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SerializeSupport.writeNode(envelopeElement, baos);
    baos.flush();

    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    response.setEntity(entity);/*from   w ww.ja v a 2s .  co  m*/
    return response;
}