Refine search
@Override public void close() { // some cases require this close here if(toClose != null) { IOUtils.closeQuietly(toClose); toClose = null; } }
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)); } }
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(); } }
private String readUnicodeString(RLEDecompressingInputStream in, int unicodeNameRecordLength) throws IOException { byte[] buffer = IOUtils.safelyAllocate(unicodeNameRecordLength, MAX_STRING_LENGTH); int bytesRead = IOUtils.readFully(in, buffer); if (bytesRead != unicodeNameRecordLength) { throw new EOFException(); } return new String(buffer, StringUtil.UTF16LE); }
/** * Write a POI Document ({@link org.apache.poi.ss.usermodel.Workbook}, {@link org.apache.poi.sl.usermodel.SlideShow}, etc) to an output stream and close the output stream. * This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. * This will also attempt to close the document, even if an error occurred while writing the document or closing the output stream. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable and closeable document to write to the output stream, then close * @param out the output stream that the document is written to * @throws IOException thrown on errors writing to the stream * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void writeAndClose(POIDocument doc, OutputStream out) throws IOException { try { write(doc, out); } finally { closeQuietly(doc); } }
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; }
IOUtils.readFully(stream, header); byte[] date = new byte[12]; IOUtils.readFully(stream, date); "-" + dateStr.substring(6,8) + "T" + dateStr.substring(8,10) + ":" + dateStr.substring(10, 12) + ":00"; metadata.set(TikaCoreProperties.CREATED, formattedDate); metadata.set(TikaCoreProperties.CREATED, formattedDate); metadata.set(Metadata.CONTENT_TYPE, PRT_MIME_TYPE); IOUtils.readFully(stream, desc); String description = extractText(desc, true); if(description.length() > 0) { while( (read = stream.read()) > -1) { if(read == 0xe0 || read == 0xe3 || read == 0xf0) { int nread = stream.read(); if(nread == 0x3f || nread == 0xbf) {
int propIdx = EndianUtils.readUShortLE(stream); int length = EndianUtils.readUShortLE(stream); int valueType = stream.read(); IOUtils.readFully(stream, value); if(valueType == 0x1e) { metadata.add(HEADER_2000_PROPERTIES_ENTRIES[propIdx], val); xhtml.element("p", val); } else if(propIdx == 0x012c) { String propName = val.substring(0, splitAt); String propVal = val.substring(splitAt+1); metadata.add(propName, propVal);
DocumentInputStream dis = root.createDocumentInputStream(es); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(dis, bos); dis.close(); CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray()); InputStream is = new BoundedInputStream(sbis, entry.streamSize); fsOut.createDocument(is, entry.streamName); is.close(); IOUtils.closeQuietly(fsOut); if (e instanceof GeneralSecurityException) { throw (GeneralSecurityException)e; IOUtils.closeQuietly(leis); IOUtils.closeQuietly(sbis);
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 { try { is.close(); } catch (IOException e) { throw new POIXMLException(e); } } this.checksum = IOUtils.calculateChecksum(data); } return this.checksum; }
IOUtils.readFully(stream, signature); if (signature[0] == (byte) 'B' && signature[1] == (byte) 'P' && signature[2] == (byte) 'G' && signature[3] == (byte) 0xfb) { int pdf = stream.read(); metadata.set(TIFF.BITS_PER_SAMPLE, Integer.toString(bitDepth)); int cer = stream.read(); switch (colourSpace) { case 0: metadata.set(Photoshop.COLOR_MODE, "YCbCr Colour"); break; case 1: metadata.set(Photoshop.COLOR_MODE, "RGB Colour"); break; case 2: break; default: stream.skip(extensionLength);
/** * 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; }
try { long skippedBytes = compressed.skip(module.offset); if (skippedBytes != module.offset) { throw new IOException("tried to skip " + module.offset + " bytes, but actually skipped " + skippedBytes + " bytes"); } catch (IllegalArgumentException | IllegalStateException e) { } finally { IOUtils.closeQuietly(compressed); IOUtils.closeQuietly(decompressed); decompressedBytes = findCompressedStreamWBruteForce(compressed); } finally { IOUtils.closeQuietly(compressed);
IOUtils.readFully(stream, signature); if (signature[0] == (byte) '8' && signature[1] == (byte) 'B' && signature[2] == (byte) 'P' && signature[3] == (byte) 'S') { IOUtils.readFully(stream, new byte[6]); metadata.set(TIFF.IMAGE_LENGTH, height); metadata.set(TIFF.IMAGE_WIDTH, width); metadata.set(TIFF.BITS_PER_SAMPLE, Integer.toString(depth)); stream.skip(colorModeSectionSize);
IOUtils.copy(stream, out); ret = out.toByteArray(); } else { is.reset(); IOUtils.copy(is, out); ret = out.toByteArray(); metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, "file_" + unknownFilenameCount.getAndIncrement() + "." + type.getExtension()); metadata.set(Metadata.CONTENT_TYPE, type.getType().toString());
/** * Get the file magic of the supplied InputStream (which MUST * support mark and reset).<p> * * If unsure if your InputStream does support mark / reset, * use {@link #prepareToCheckMagic(InputStream)} to wrap it and make * sure to always use that, and not the original!<p> * * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean, * that the ZIP stream has leading junk bytes * * @param inp An InputStream which supports either mark/reset */ public static FileMagic valueOf(InputStream inp) throws IOException { if (!inp.markSupported()) { throw new IOException("getFileMagic() only operates on streams which support mark(int)"); } // Grab the first 8 bytes byte[] data = IOUtils.peekFirst8Bytes(inp); return FileMagic.valueOf(data); }