Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:ch.cyberduck.core.onedrive.OneDriveWriteFeatureTest.java

@Test
public void testWriteZeroLength() throws Exception {
    final OneDriveWriteFeature feature = new OneDriveWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);//  w ww  . ja va  2  s  . c  o m
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:org.cerberus.servlet.crud.countryenvironment.ReadApplicationObjectImage.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w w  w  . java  2 s  .com*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 * @throws CerberusException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, CerberusException {
    String charset = request.getCharacterEncoding();

    String application = ParameterParserUtil
            .parseStringParamAndDecodeAndSanitize(request.getParameter("application"), "", charset);
    String object = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("object"), "",
            charset);

    int width = (!StringUtils.isEmpty(request.getParameter("w"))) ? Integer.valueOf(request.getParameter("w"))
            : -1;
    int height = (!StringUtils.isEmpty(request.getParameter("h"))) ? Integer.valueOf(request.getParameter("h"))
            : -1;

    ApplicationContext appContext = WebApplicationContextUtils
            .getWebApplicationContext(this.getServletContext());
    IApplicationObjectService applicationObjectService = appContext.getBean(IApplicationObjectService.class);

    BufferedImage image = applicationObjectService.readImageByKey(application, object);
    BufferedImage b;
    if (image != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        /**
         * If width and height not defined, get image in real size
         */
        if (width == -1 && height == -1) {
            b = image;
        } else {
            ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(width, height, true));
            rop.setNumberOfThreads(4);
            b = rop.filter(image, null);
        }
        ImageIO.write(b, "png", baos);
        //        byte[] bytesOut = baos.toByteArray();
    } else {
        // create a buffered image
        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
        b = ImageIO.read(bis);
        bis.close();
    }

    response.setHeader("Last-Modified",
            DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());
    response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString());

    ImageIO.write(b, "png", response.getOutputStream());
}

From source file:ch.cyberduck.core.onedrive.OneDriveBufferWriteFeatureTest.java

@Test
public void testWriteUnknownLength() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(5 * 1024 * 1024);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);/*  w ww.j a  va  2 s.com*/
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    new StreamCopier(status, status).transfer(in, out);
    in.close();
    out.flush();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:ch.cyberduck.core.onedrive.OneDriveBufferWriteFeatureTest.java

@Test
public void testWriteZeroLength() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);/*from w ww .j  a  va 2s  . c  o  m*/
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    new StreamCopier(status, status).transfer(in, out);
    in.close();
    out.flush();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:ch.cyberduck.core.onedrive.OneDriveBufferWriteFeatureTest.java

@Test
public void testWrite() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(5 * 1024);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);// w w  w .  j  a va 2s.  c o m
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    new StreamCopier(status, status).transfer(in, out);
    in.close();
    out.flush();
    assertEquals(content.length, status.getOffset());
    assertEquals(content.length, status.getLength());
    out.close();
    assertEquals(content.length, status.getOffset());
    assertEquals(content.length, status.getLength());
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:org.apache.myfaces.shared.util.StateUtils.java

public static final byte[] decompress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] bytes");
    }/*from w ww.ja v  a2 s  .  c  o  m*/

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[bytes.length];
    int length;

    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        byte[] moreBytes = baos.toByteArray();
        baos.close();
        bais.close();
        gis.close();
        baos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public BufferedImage getImageFromByteArray(byte[] array) throws IOException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(array);
    BufferedImage originalImage = ImageIO.read(byteArrayInputStream);
    byteArrayInputStream.close();
    return originalImage;
}

From source file:ch.cyberduck.core.onedrive.OneDriveBufferWriteFeatureTest.java

@Test
public void testWriteOverwrite() throws Exception {
    final OneDriveBufferWriteFeature feature = new OneDriveBufferWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    {/* w w w .j  av a2  s  .com*/
        final byte[] content = RandomUtils.nextBytes(42512);
        final TransferStatus status = new TransferStatus();
        status.setLength(content.length);
        final HttpResponseOutputStream<Void> out = feature.write(file, status,
                new DisabledConnectionCallback());
        final ByteArrayInputStream in = new ByteArrayInputStream(content);
        new StreamCopier(status, status).transfer(in, out);
        in.close();
        out.flush();
        out.close();
        assertNull(out.getStatus());
        assertTrue(new DefaultFindFeature(session).find(file));
        final byte[] compare = new byte[content.length];
        final InputStream stream = new OneDriveReadFeature(session).read(file,
                new TransferStatus().length(content.length), new DisabledConnectionCallback());
        IOUtils.readFully(stream, compare);
        stream.close();
        assertArrayEquals(content, compare);
    }
    {
        final byte[] content = RandomUtils.nextBytes(33221);
        final TransferStatus status = new TransferStatus();
        status.setLength(content.length);
        final HttpResponseOutputStream<Void> out = feature.write(file, status,
                new DisabledConnectionCallback());
        final ByteArrayInputStream in = new ByteArrayInputStream(content);
        new StreamCopier(status, status).transfer(in, out);
        in.close();
        out.flush();
        out.close();
        assertNull(out.getStatus());
        assertTrue(new DefaultFindFeature(session).find(file));
        final byte[] compare = new byte[content.length];
        final InputStream stream = new OneDriveReadFeature(session).read(file,
                new TransferStatus().length(content.length), new DisabledConnectionCallback());
        IOUtils.readFully(stream, compare);
        stream.close();
        assertArrayEquals(content, compare);
    }
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:com.csipsimple.backup.SipProfilesHelper.java

/**
 * Read data from the input stream//from  ww w .  ja  v  a  2s . c o m
 * 
 * @param data the input stream
 * @return the data
 * @throws IOException I/O error
 */
private String readData(BackupDataInputStream data) throws IOException {
    String dataS;
    byte[] buf = new byte[data.size()];
    data.read(buf, 0, buf.length);
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);
    DataInputStream dis = new DataInputStream(bais);
    dataS = dis.readUTF();
    dis.close();
    bais.close();
    return dataS;
}

From source file:com.easyhome.common.modules.network.HttpManager.java

private static void imageContentToUpload4Byte(OutputStream out, byte[] fileContent) throws WeiboException {
    if (fileContent == null) {
        return;//from w ww.  j a v  a 2 s . c  o m
    }
    StringBuilder temp = new StringBuilder();

    temp.append(MP_BOUNDARY).append("\r\n");
    temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"").append("news_image")
            .append("\"\r\n");
    String filetype = "image/png";
    temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    ByteArrayInputStream input = null;
    try {
        out.write(res);
        input = new ByteArrayInputStream(fileContent);
        byte[] buffer = new byte[1024 * 50];
        while (true) {
            int count = input.read(buffer);
            if (count == -1) {
                break;
            }
            out.write(buffer, 0, count);
        }
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new WeiboException(e);
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                throw new WeiboException(e);
            }
        }
    }
}