List of usage examples for java.io ByteArrayInputStream close
public void close() throws IOException
From source file:ch.cyberduck.core.b2.B2LargeUploadWriteFeatureTest.java
@Test public void testWriteZeroLength() throws Exception { final B2Session session = new B2Session(new Host(new B2Protocol(), new B2Protocol().getDefaultHostname(), new Credentials(System.getProperties().getProperty("b2.user"), System.getProperties().getProperty("b2.key")))); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final B2LargeUploadWriteFeature feature = new B2LargeUploadWriteFeature(session); final Path container = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume)); final byte[] content = RandomUtils.nextBytes(0); final TransferStatus status = new TransferStatus(); status.setLength(-1L);//from ww w. j av a 2s . c om final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)); final StatusOutputStream<VersionId> out = feature.write(file, status, new DisabledConnectionCallback()); final ByteArrayInputStream in = new ByteArrayInputStream(content); assertEquals(content.length, IOUtils.copyLarge(in, out)); in.close(); out.close(); assertNotNull(out.getStatus()); assertTrue(new DefaultFindFeature(session).find(file)); final byte[] compare = new byte[content.length]; final InputStream stream = new B2ReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback()); IOUtils.readFully(stream, compare); stream.close(); assertArrayEquals(content, compare); new B2DeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
From source file:ch.cyberduck.core.manta.MantaWriteFeatureTest.java
@Test public void testWrite() throws Exception { final MantaWriteFeature feature = new MantaWriteFeature(session); final Path container = new MantaDirectoryFeature(session).mkdir(randomDirectory(), "", new TransferStatus()); final byte[] content = RandomUtils.nextBytes(5 * 1024 * 1024); final TransferStatus status = new TransferStatus(); status.setLength(content.length);//from w ww.j av a2 s.co 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); final byte[] buffer = new byte[32 * 1024]; assertEquals(content.length, IOUtils.copyLarge(in, out, buffer)); in.close(); out.close(); assertNull(out.getStatus()); assertTrue(new DefaultFindFeature(session).find(file)); final byte[] compare = new byte[content.length]; final InputStream stream = new MantaReadFeature(session).read(file, new TransferStatus().length(content.length), new DisabledConnectionCallback()); IOUtils.readFully(stream, compare); stream.close(); assertArrayEquals(content, compare); new MantaDeleteFeature(session).delete(Collections.singletonList(container), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
From source file:io.cortical.retina.core.ImagesTest.java
/** * {@link Images#getImage(io.cortical.retina.model.Model, int, ImagePlotShape, ImageEncoding, double)} * /*from www .ja v a 2 s . co m*/ * @throws ApiException : should never be thrown. * @throws IOException */ @Test public void testGetImage() throws ApiException, IOException { ImagePlotShape shape = ImagePlotShape.CIRCLE; ImageEncoding encoding = ImageEncoding.BASE64_PNG; double sparsity = 0.02; when(api.getImageForExpression(eq(TERM_1_JSON), eq(NOT_NULL_RETINA), eq(1), eq(shape.name().toLowerCase()), eq(encoding.machineRepresentation()), eq(sparsity))) .thenReturn(new ByteArrayInputStream(new byte[] { "i".getBytes()[0] })); ByteArrayInputStream stream = images.getImage(TERM_1, 1, shape, encoding, sparsity); assertNotNull(stream); assertEquals(105, stream.read()); stream.close(); verify(api, times(1)).getImageForExpression(eq(TERM_1_JSON), eq(NOT_NULL_RETINA), eq(1), eq(shape.name().toLowerCase()), eq(encoding.machineRepresentation()), eq(sparsity)); }
From source file:com.bitranger.parknshop.util.ObjUtils.java
public static Object fromBytes(byte[] plainObj) { Object var = null; ByteArrayInputStream baIS = new ByteArrayInputStream(plainObj); ObjectInputStream objIS = null; try {/*ww w . j av a 2 s .c o m*/ objIS = new ObjectInputStream(baIS); var = objIS.readObject(); } catch (Exception e) { throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e); } finally { try { baIS.close(); if (objIS != null) { objIS.close(); } } catch (IOException e) { throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e); } } return var; }
From source file:org.apache.myfaces.shared_ext202patch.util.StateUtils.java
public static final byte[] decompress(byte[] bytes) { if (bytes == null) throw new NullPointerException("byte[] bytes"); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[bytes.length]; int length;//from w w w .j a v a 2 s . c o m 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:com.rr.familyPlanning.ui.security.decryptObject.java
/** * Decrypts the String and serializes the object * * @param base64Data/*from w ww . j a v a 2s .co m*/ * @param base64IV * @return * @throws Exception */ public Object decryptObject(String base64Data, String base64IV) throws Exception { // Decode the data byte[] encryptedData = Base64.decodeBase64(base64Data.getBytes()); // Decode the Init Vector byte[] rawIV = Base64.decodeBase64(base64IV.getBytes()); // Configure the Cipher Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(rawIV); MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(keyString.getBytes()); byte[] key = new byte[16]; System.arraycopy(digest.digest(), 0, key, 0, key.length); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); // Decrypt the data.. byte[] decrypted = cipher.doFinal(encryptedData); // Deserialize the object ByteArrayInputStream stream = new ByteArrayInputStream(decrypted); ObjectInput in = new ObjectInputStream(stream); Object obj = null; try { obj = in.readObject(); } finally { stream.close(); in.close(); } return obj; }
From source file:com.company.project.web.controller.WebSocketController.java
@MessageMapping("/fileShareMapping") //@SendTo("/topic/fileShareResponse") public void fileShare(String param) throws Exception { // TODO detect file type String id = param.substring(0, 36); // extract prepend UUID which is 36 chars length String base64Chunk = param.substring(36, param.length()); if (base64Chunk.length() == 0) { this.simpMessagingTemplate.convertAndSend("/topic/fileShareResponse/" + id, "0"); this.simpMessagingTemplate.convertAndSend("/topic/imageFileShareResponse", map.get(id).toString()); BufferedImage image = null; byte[] imageByte; try {//from www. j av a2 s .c o m BASE64Decoder decoder = new BASE64Decoder(); imageByte = decoder.decodeBuffer(map.get(id).toString().split(",")[1]); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); image = ImageIO.read(bis); bis.close(); } catch (Exception e) { e.printStackTrace(); } File outputfile = new File("D:/GemShelf/" + UUID.randomUUID() + ".jpg"); ImageIO.write(image, "jpg", outputfile); map.remove(id); } else { StringBuilder sb = map.get(id); if (sb == null) { sb = new StringBuilder(); sb.append(base64Chunk); map.put(id, sb); } else { sb.append(base64Chunk); } this.simpMessagingTemplate.convertAndSend("/topic/fileShareResponse/" + id, "1"); } }
From source file:net.sqs2.omr.master.pdfbookmark.PDFBookmarkToSQMTranslator.java
@Override public void execute(InputStream sourceInputStream, String systemId, OutputStream sqmOutputStream, URIResolver uriResolver) throws TranslatorException { try {/*from w w w.j a v a2 s. c o m*/ ByteArrayOutputStream bookmarkOutputStream = new ByteArrayOutputStream(65536); this.pdfToBookmarkTranslator.translate(sourceInputStream, systemId, bookmarkOutputStream, uriResolver); this.numPages = this.pdfToBookmarkTranslator.getNumPages(); this.bookmarkToSQMTranslator.setNumPages(this.numPages); byte[] bytes = bookmarkOutputStream.toByteArray(); if (bytes.length == 0) { throw new InvalidPageMasterException(); } ByteArrayInputStream bookmarkInputStream = new ByteArrayInputStream(bytes); this.bookmarkToSQMTranslator.translate(bookmarkInputStream, systemId, sqmOutputStream, uriResolver); sqmOutputStream.flush(); bookmarkInputStream.close(); } catch (TranslatorException ex) { throw new InvalidPageMasterException(); } catch (IOException ex) { throw new InvalidPageMasterException(); } }
From source file:ch.cyberduck.core.onedrive.OneDriveWriteFeatureTest.java
@Test public void testWriteUmlaut() throws Exception { final OneDriveWriteFeature feature = new OneDriveWriteFeature(session); final Path container = new OneDriveHomeFinderFeature(session).find(); final byte[] content = RandomUtils.nextBytes(2048); final TransferStatus status = new TransferStatus(); status.setLength(content.length);/*from w w w. j a va 2 s. co m*/ final Path file = new Path(container, String.format("%s", 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:ch.cyberduck.core.onedrive.OneDriveWriteFeatureTest.java
@Test public void testWriteUmlautZeroLength() 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 w w.j a v a 2 s .c o m*/ final Path file = new Path(container, String.format("%s", 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()); }