/** * Reads up to {@code length} bytes from the input stream, and returns the bytes read. * * @param stream The byte stream of data to read. * @param length The maximum length to read, use Integer.MAX_VALUE to read the stream * until EOF. * @return A byte array with the read bytes. * @throws IOException If reading data fails or EOF is encountered too early for the given length. */ public static byte[] toByteArray(InputStream stream, final int length) throws IOException { return toByteArray(stream, length, Integer.MAX_VALUE); }
/** * Reads all the data from the input stream, and returns the bytes read. * * @param stream The byte stream of data to read. * @return A byte array with the read bytes. * @throws IOException If reading data fails or EOF is encountered too early for the given length. */ public static byte[] toByteArray(InputStream stream) throws IOException { return toByteArray(stream, Integer.MAX_VALUE); }
public HeaderBlock(ByteBuffer buffer) throws IOException { this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE)); }
ZipArchiveFakeEntry(ZipArchiveEntry entry, InputStream inp) throws IOException { super(entry.getName()); final long entrySize = entry.getSize(); if (entrySize < -1 || entrySize>=Integer.MAX_VALUE) { throw new IOException("ZIP entry size is too large or invalid"); } // Grab the de-compressed contents for later data = (entrySize == -1) ? IOUtils.toByteArray(inp) : IOUtils.toByteArray(inp, (int)entrySize); }
/** * Convenience method to get the embedded data as byte array. * * @return the embedded data. */ default byte[] getBytes() throws IOException { try (InputStream is = getInputStream()) { return IOUtils.toByteArray(is); } }
public static void main(String[] args) throws IOException { InputStream in = new FileInputStream(args[0]); byte[] b = IOUtils.toByteArray(in); in.close(); System.out.println(HexDump.dump(b, 0, 0)); } }
/** * Adds a picture to the slideshow. * * @param is The stream to read image from * @param format The format of the picture * @return the picture data * @since 3.15 beta 2 */ @Override public XSLFPictureData addPicture(InputStream is, PictureType format) throws IOException { return addPicture(IOUtils.toByteArray(is), format); }
/** * Adds a picture to the document. * * @param is The stream 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()} . * @throws InvalidFormatException If the format of the picture is not known. * @throws IOException If reading the picture-data from the stream fails. */ public String addPictureData(InputStream is, int format) throws InvalidFormatException, IOException { byte[] data = IOUtils.toByteArray(is); return addPictureData(data, format); }
/** * Get the file magic of the supplied {@link File}<p> * * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean, * that the ZIP stream has leading junk bytes * * @param inp a file to be identified */ public static FileMagic valueOf(final File inp) throws IOException { try (FileInputStream fis = new FileInputStream(inp)) { final byte[] data = IOUtils.toByteArray(fis, 8); return FileMagic.valueOf(data); } }
private static void readEntry(POIFSReaderEvent event) { POIFSDocumentPath path = event.getPath(); StringBuilder sb = new StringBuilder(); try (DocumentInputStream istream = event.getStream()) { sb.setLength(0); int pathLength = path.length(); for (int k = 0; k < pathLength; k++) { sb.append("/").append(path.getComponent(k)); } byte[] data = IOUtils.toByteArray(istream); sb.append("/").append(event.getName()).append(": ").append(data.length).append(" bytes read"); System.out.println(sb); } catch (IOException ignored) { } }
public String addPictureData(InputStream is, int format) throws InvalidFormatException { try { byte[] data = IOUtils.toByteArray(is); return addPictureData(data, format); } catch (IOException e) { throw new POIXMLException(e); } }
/** * Gets the picture data as a byte array. * * You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method * * @return the Picture data. */ public byte[] getData() { try { return IOUtils.toByteArray(getInputStream()); } catch (IOException e) { throw new POIXMLException(e); } }
public static void dump(DirectoryEntry root, File parent) throws IOException { for(Iterator<Entry> it = root.getEntries(); it.hasNext();){ Entry entry = it.next(); if(entry instanceof DocumentNode){ DocumentNode node = (DocumentNode)entry; DocumentInputStream is = new DocumentInputStream(node); byte[] bytes = IOUtils.toByteArray(is); is.close(); try (OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()))) { out.write(bytes); } } else if (entry instanceof DirectoryEntry){ DirectoryEntry dir = (DirectoryEntry)entry; File file = new File(parent, entry.getName()); if(!file.exists() && !file.mkdirs()) { throw new IOException("Could not create directory " + file); } dump(dir, file); } else { System.err.println("Skipping unsupported POIFS entry: " + entry); } } } public static void dump(POIFSFileSystem fs, int startBlock, String name, File parent) throws IOException {
public Long getChecksum() { if (this.checksum == null) { byte[] data; try (InputStream is = getPackagePart().getInputStream()) { data = IOUtils.toByteArray(is); } catch (IOException e) { throw new POIXMLException(e); } this.checksum = IOUtils.calculateChecksum(data); } return this.checksum; }
/** * Gets the picture data as a byte array. * <p> * Note, that this call might be expensive since all the picture data is copied into a temporary byte array. * You can grab the picture data directly from the underlying package part as follows: * <br> * <code> * InputStream is = getPackagePart().getInputStream(); * </code> * </p> * * @return the Picture data. */ public byte[] getData() { try { return IOUtils.toByteArray(getPackagePart().getInputStream()); } catch (IOException e) { throw new POIXMLException(e); } }
/** * Gets the picture data as a byte array. * <p> * Note, that this call might be expensive since all the picture data is copied into a temporary byte array. * You can grab the picture data directly from the underlying package part as follows: * <br> * <code> * InputStream is = getPackagePart().getInputStream(); * </code> * </p> * * @return the picture data. */ public byte[] getData() { try { return IOUtils.toByteArray(getPackagePart().getInputStream()); } catch(IOException e) { throw new POIXMLException(e); } }
/** * Creates a {@link PropertySet} instance from an {@link * InputStream} in the Horrible Property Set Format.<p> * * The constructor reads the first few bytes from the stream * and determines whether it is really a property set stream. If * it is, it parses the rest of the stream. If it is not, it * resets the stream to its beginning in order to let other * components mess around with the data and throws an * exception. * * @param stream Holds the data making out the property set * stream. * @throws IOException * if the {@link InputStream} cannot be accessed as needed. * @exception NoPropertySetStreamException * if the input stream does not contain a property set. * @exception UnsupportedEncodingException * if a character encoding is not supported. */ public PropertySet(final InputStream stream) throws NoPropertySetStreamException, IOException { if (!isPropertySetStream(stream)) { throw new NoPropertySetStreamException(); } final byte[] buffer = IOUtils.toByteArray(stream); init(buffer, 0, buffer.length); }
public Long getChecksum() { if (this.checksum == null) { InputStream is = null; byte[] data; try { is = getPackagePart().getInputStream(); data = IOUtils.toByteArray(is); } catch (IOException e) { throw new POIXMLException(e); } finally { IOUtils.closeQuietly(is); } this.checksum = IOUtils.calculateChecksum(data); } return this.checksum; }
byte[] data = IOUtils.toByteArray(is); HexDump.dump(data, 0, System.out, 0); } else {
@Override public EmbeddedData extract(DirectoryNode dn) throws IOException { ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid()); String contentType = null; String ext = null; if (clsId != null) { contentType = clsId.getContentType(); ext = clsId.getFileExtension(); } if (contentType == null || ext == null) { contentType = "application/zip"; ext = ".zip"; } DocumentInputStream dis = dn.createDocumentInputStream("package"); byte data[] = IOUtils.toByteArray(dis); dis.close(); return new EmbeddedData(dn.getName()+ext, data, contentType); } }