Example usage for org.springframework.util StreamUtils copyToByteArray

List of usage examples for org.springframework.util StreamUtils copyToByteArray

Introduction

In this page you can find the example usage for org.springframework.util StreamUtils copyToByteArray.

Prototype

public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException 

Source Link

Document

Copy the contents of the given InputStream into a new byte array.

Usage

From source file:com.hillert.botanic.model.Image.java

public Image(String name, Resource data, Plant plant) {
    this.name = name;
    this.plant = plant;

    InputStream is = null;/*  w w  w. j a  v  a2 s.com*/
    try {
        is = data.getInputStream();
        this.image = StreamUtils.copyToByteArray(is);
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:io.spring.initializr.generator.ProjectResourceLocator.java

/**
 * Return the binary content of the resource at the specified location.
 * @param location a resource location//from  w  ww.  ja v a  2 s . c o m
 * @return the content of the resource
 */
@Cacheable("initializr.project-resources")
public byte[] getBinaryResource(String location) {
    try (InputStream stream = getInputStream(location)) {
        return StreamUtils.copyToByteArray(stream);
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot get resource", ex);
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java

@Test
public void test01_01_fileDownload() throws IOException {
    ResponseEntity<byte[]> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_1",
            byte[].class);
    ClassPathResource images = new ClassPathResource("/image/Duke.png");

    byte[] expected = StreamUtils.copyToByteArray(images.getInputStream());

    HttpHeaders headers = response.getHeaders();
    System.out.println("test01_01_fileDownload: X-Track=" + headers.getFirst("X-Track"));
    assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=Duke.png"));

    MediaType contentType = headers.getContentType();
    assertThat(contentType.getType(), is("image"));
    assertThat(contentType.getSubtype(), is("png"));

    assertThat(response.getBody(), is(expected));
}

From source file:org.zalando.stups.swagger.codegen.SwaggerCodegenController.java

@RequestMapping(value = "/v2/api-docs")
public String getApi() throws IOException {
    final Resource r = resourceLoader.getResource(swaggerCodegenProperties.getApiClasspathLocation());
    return new String(StreamUtils.copyToByteArray(r.getInputStream()));

}

From source file:com.github.ukase.toolkit.WrappedUserAgentCallback.java

@Override
public ImageResource getImageResource(String uri) {
    if (source.hasResource(uri)) {
        try (InputStream stream = source.getResource(uri)) {
            Image image = Image.getInstance(StreamUtils.copyToByteArray(stream));
            scaleToOutputResolution(image);
            return new ImageResource(uri, new ITextFSImage(image));
        } catch (IOException | DocumentException e) {
            log.error("Cannot read image [" + uri + "]", e);
        }//from  w  w w.j a v  a  2 s  .c o  m
    }
    return delegate.getImageResource(uri);
}

From source file:com.github.ukase.toolkit.WrappedUserAgentCallback.java

@Override
public byte[] getBinaryResource(String uri) {
    if (source.hasResource(uri)) {
        try (InputStream stream = source.getResource(uri)) {
            return StreamUtils.copyToByteArray(stream);
        } catch (IOException e) {
            log.error("Cannot map resource to byte array [" + uri + "]", e);
        }// ww w  .ja v a2s  .c  o  m
    }
    return delegate.getBinaryResource(uri);
}

From source file:org.crazydog.util.spring.DigestUtils.java

private static byte[] digest(String algorithm, InputStream inputStream) throws IOException {
    MessageDigest messageDigest = getDigest(algorithm);
    if (inputStream instanceof UpdateMessageDigestInputStream) {
        ((UpdateMessageDigestInputStream) inputStream).updateMessageDigest(messageDigest);
        return messageDigest.digest();
    } else {//w  w w  . j  av a 2s.  c o  m
        return messageDigest.digest(StreamUtils.copyToByteArray(inputStream));
    }
}

From source file:com.dreamworks.dsp.server.EmbeddedSftpServer.java

private PublicKey decodePublicKey() throws Exception {
    InputStream stream = new ClassPathResource("keys/sftp_rsa.pub").getInputStream();
    byte[] decodeBuffer = Base64.decodeBase64(StreamUtils.copyToByteArray(stream));
    ByteBuffer bb = ByteBuffer.wrap(decodeBuffer);
    int len = bb.getInt();
    byte[] type = new byte[len];
    bb.get(type);/*ww  w  .j  a va 2s. com*/
    if ("ssh-rsa".equals(new String(type))) {
        BigInteger e = decodeBigInt(bb);
        BigInteger m = decodeBigInt(bb);
        RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
        return KeyFactory.getInstance("RSA").generatePublic(spec);

    } else {
        throw new IllegalArgumentException("Only supports RSA");
    }
}

From source file:eu.openanalytics.shinyproxy.controllers.BaseController.java

protected String resolveImageURI(String resourceURI) {
    if (resourceURI == null || resourceURI.isEmpty())
        return resourceURI;
    if (imageCache.containsKey(resourceURI))
        return imageCache.get(resourceURI);

    String resolvedValue = resourceURI;
    if (resourceURI.toLowerCase().startsWith("file://")) {
        String mimetype = URLConnection.guessContentTypeFromName(resourceURI);
        if (mimetype == null) {
            logger.warn("Cannot determine mimetype for resource: " + resourceURI);
        } else {/*from   w ww.  jav a2 s  .  c  o m*/
            try (InputStream input = new URL(resourceURI).openConnection().getInputStream()) {
                byte[] data = StreamUtils.copyToByteArray(input);
                String encoded = Base64.getEncoder().encodeToString(data);
                resolvedValue = String.format("data:%s;base64,%s", mimetype, encoded);
            } catch (IOException e) {
                logger.warn("Failed to convert file URI to data URI: " + resourceURI, e);
            }
        }
    }
    imageCache.put(resourceURI, resolvedValue);
    return resolvedValue;
}

From source file:demo.SourceHttpMessageConverter.java

private SAXSource readSAXSource(InputStream body) throws IOException {
    try {//from w w w . ja  v  a 2 s.com
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        byte[] bytes = StreamUtils.copyToByteArray(body);
        return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
    } catch (SAXException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
    }
}