void read(InputStream in) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out); out.close(); buf = out.toByteArray(); } public String getContent() {
private static byte[] tryToDecompress(InputStream is) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { IOUtils.copy(new RLEDecompressingInputStream(is), bos); } catch (IllegalArgumentException | IOException | IllegalStateException e){ return null; } return bos.toByteArray(); } }
public static byte[] decompress(byte[] compressed, int offset, int length) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream instream = new ByteArrayInputStream(compressed, offset, length); InputStream stream = new RLEDecompressingInputStream(instream); IOUtils.copy(stream, out); stream.close(); out.close(); return out.toByteArray(); } }
/** * Copy the contents of the stream to a new file. * * @param srcStream The {@link InputStream} which provides the data * @param destFile The file where the data should be stored * @return the amount of bytes copied * * @throws IOException If the target directory does not exist and cannot be created * or if copying the data fails. */ public static long copy(InputStream srcStream, File destFile) throws IOException { File destDirectory = destFile.getParentFile(); if (!(destDirectory.exists() || destDirectory.mkdirs())) { throw new RuntimeException("Can't create destination directory: "+destDirectory); } try (OutputStream destStream = new FileOutputStream(destFile)) { return IOUtils.copy(srcStream, destStream); } }
@Override public boolean load(InputStream ios) throws InvalidFormatException { // Grab the data ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copy(ios, baos); } catch(IOException e) { throw new InvalidFormatException(e.getMessage()); } // Save it data = baos.toByteArray(); // All done return true; }
@Override public byte[] getObjectData() throws IOException { InputStream is = getObjectPart().getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(is, bos); is.close(); return bos.toByteArray(); }
@Override public EmbeddedData extract(DirectoryNode dn) throws IOException { try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = dn.createDocumentInputStream("CONTENTS")) { IOUtils.copy(is, bos); return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF); } }
/** * Peeks at the first N bytes of the stream. Returns those bytes, but * with the stream unaffected. Requires a stream that supports mark/reset, * or a PushbackInputStream. If the stream has >0 but <N bytes, * remaining bytes will be zero. * @throws EmptyFileException if the stream is empty */ public static byte[] peekFirstNBytes(InputStream stream, int limit) throws IOException, EmptyFileException { stream.mark(limit); ByteArrayOutputStream bos = new ByteArrayOutputStream(limit); copy(new BoundedInputStream(stream, limit), bos); int readBytes = bos.size(); if (readBytes == 0) { throw new EmptyFileException(); } if (readBytes < limit) { bos.write(new byte[limit-readBytes]); } byte peekedBytes[] = bos.toByteArray(); if(stream instanceof PushbackInputStream) { PushbackInputStream pin = (PushbackInputStream)stream; pin.unread(peekedBytes, 0, readBytes); } else { stream.reset(); } return peekedBytes; }
IOUtils.copy(is, bos); byte[] compressed = bos.toByteArray(); byte[] decompressed = null;
@Override public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { try (OutputStream os = event.getStream(); FileInputStream fis = new FileInputStream(fileOut)) { // StreamSize (8 bytes): An unsigned integer that specifies the number of bytes used by data // encrypted within the EncryptedData field, not including the size of the StreamSize field. // Note that the actual size of the \EncryptedPackage stream (1) can be larger than this // value, depending on the block size of the chosen encryption algorithm byte buf[] = new byte[LittleEndianConsts.LONG_SIZE]; LittleEndian.putLong(buf, 0, pos); os.write(buf); IOUtils.copy(fis, os); } if (!fileOut.delete()) { LOG.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut); } } catch (IOException e) { throw new EncryptedDocumentException(e); } } }
private boolean copyFile( String src, String dest ) throws KettleFileException, IOException { FileObject srcFile = getFileObjectFromKettleVFS( src ); FileObject destFile = getFileObjectFromKettleVFS( dest ); try ( InputStream in = KettleVFS.getInputStream( srcFile ); OutputStream out = KettleVFS.getOutputStream( destFile, false ) ) { IOUtils.copy( in, out ); } return true; }
@Override public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { LittleEndianOutputStream leos = new LittleEndianOutputStream(event.getStream()); // StreamSize (8 bytes): An unsigned integer that specifies the number of bytes used by data // encrypted within the EncryptedData field, not including the size of the StreamSize field. // Note that the actual size of the \EncryptedPackage stream (1) can be larger than this // value, depending on the block size of the chosen encryption algorithm leos.writeLong(countBytes); FileInputStream fis = new FileInputStream(fileOut); try { IOUtils.copy(fis, leos); } finally { fis.close(); } if (!fileOut.delete()) { logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut); } leos.close(); } catch (IOException e) { throw new EncryptedDocumentException(e); } } }
/** * Adds a picture to the workbook. * * @param is The sream to read image from * @param format The format of the picture. * * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} . * @see Workbook#PICTURE_TYPE_EMF * @see Workbook#PICTURE_TYPE_WMF * @see Workbook#PICTURE_TYPE_PICT * @see Workbook#PICTURE_TYPE_JPEG * @see Workbook#PICTURE_TYPE_PNG * @see Workbook#PICTURE_TYPE_DIB * @see #getAllPictures() */ public int addPicture(InputStream is, int format) throws IOException { int imageNumber = getAllPictures().size() + 1; XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], this.xssfFactory, imageNumber, true).getDocumentPart(); try (OutputStream out = img.getPackagePart().getOutputStream()) { IOUtils.copy(is, out); } pictures.add(img); return imageNumber - 1; }
length = IOUtils.copy(bis, os);
/** * Import a package part into this sheet. */ void importPart(PackageRelationship srcRel, PackagePart srcPafrt) { PackagePart destPP = getPackagePart(); PackagePartName srcPPName = srcPafrt.getPartName(); OPCPackage pkg = destPP.getPackage(); if(pkg.containPart(srcPPName)){ // already exists return; } destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType()); PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType()); try { OutputStream out = part.getOutputStream(); InputStream is = srcPafrt.getInputStream(); IOUtils.copy(is, out); is.close(); out.close(); } catch (IOException e){ throw new POIXMLException(e); } }
}; CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc); IOUtils.copy(zis, cos); cos.close(); fos2.close();
IOUtils.copy(part.getInputStream(), out); out.close();
IOUtils.copy(is, zos);
IOUtils.copy(vbaProjectStream, outputStream); } finally { IOUtils.closeQuietly(outputStream);
/** * Recursively copy package parts to the destination package */ private static void copy(OPCPackage pkg, PackagePart part, OPCPackage tgt, PackagePart part_tgt) throws OpenXML4JException, IOException { PackageRelationshipCollection rels = part.getRelationships(); if(rels != null) for (PackageRelationship rel : rels) { PackagePart p; if(rel.getTargetMode() == TargetMode.EXTERNAL){ part_tgt.addExternalRelationship(rel.getTargetURI().toString(), rel.getRelationshipType(), rel.getId()); //external relations don't have associated package parts continue; } URI uri = rel.getTargetURI(); if(uri.getRawFragment() != null) { part_tgt.addRelationship(uri, rel.getTargetMode(), rel.getRelationshipType(), rel.getId()); continue; } PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); p = pkg.getPart(relName); part_tgt.addRelationship(p.getPartName(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId()); PackagePart dest; if(!tgt.containPart(p.getPartName())){ dest = tgt.createPart(p.getPartName(), p.getContentType()); OutputStream out = dest.getOutputStream(); IOUtils.copy(p.getInputStream(), out); out.close(); copy(pkg, p, tgt, dest); } } }