/** * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt> */ public static int readFully(InputStream in, byte[] b) throws IOException { return readFully(in, b, 0, b.length); }
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); }
private static byte[] readFirst512(InputStream stream) throws IOException { // Grab the first 512 bytes // (For 4096 sized blocks, the remaining 3584 bytes are zero) byte[] data = new byte[512]; int bsCount = IOUtils.readFully(stream, data); if(bsCount != 512) { throw alertShortRead(bsCount); } return data; }
/** * Read <tt>length</tt> bytes of MBCS (multi-byte character set) characters from the stream * * @param stream the inputstream to read from * @param length number of bytes to read from stream * @param charset the character set encoding of the bytes in the stream * @return a java String in the supplied character set * @throws IOException If reading from the stream fails */ private static String readString(InputStream stream, int length, Charset charset) throws IOException { byte[] buffer = IOUtils.safelyAllocate(length, MAX_STRING_LENGTH); int bytesRead = IOUtils.readFully(stream, buffer); if (bytesRead != length) { throw new IOException("Tried to read: "+length + ", but could only read: "+bytesRead); } return new String(buffer, 0, length, charset); }
int worked = IOUtils.readFully(channel, dst); if(worked == -1) { throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
/** * Adds a picture to the presentation. * * @param pict The file containing the image to add * @param format The format of the picture. * @return the picture data * @since 3.15 beta 2 */ @Override public XSLFPictureData addPicture(File pict, PictureType format) throws IOException { int length = (int) pict.length(); byte[] data = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); try (InputStream is = new FileInputStream(pict)) { IOUtils.readFully(is, data); } return addPicture(data, format); }
IOUtils.readFully(channel, headerBuffer);
/** * create a new HeaderBlockReader from an InputStream * * @param stream the source InputStream * * @exception IOException on errors or bad data */ public HeaderBlock(InputStream stream) throws IOException { // Grab the first 512 bytes // (For 4096 sized blocks, the remaining 3584 bytes are zero) // Then, process the contents this(readFirst512(stream)); // Fetch the rest of the block if needed if(bigBlockSize.getBigBlockSize() != 512) { int rest = bigBlockSize.getBigBlockSize() - 512; byte[] tmp = IOUtils.safelyAllocate(rest, MAX_RECORD_LENGTH); IOUtils.readFully(stream, tmp); } }
public void parseRawExif(InputStream stream, int length, boolean needsExifHeader) throws IOException, SAXException, TikaException { byte[] exif; if (needsExifHeader) { exif = new byte[length + 6]; exif[0] = (byte) 'E'; exif[1] = (byte) 'x'; exif[2] = (byte) 'i'; exif[3] = (byte) 'f'; IOUtils.readFully(stream, exif, 6, length); } else { exif = new byte[length]; IOUtils.readFully(stream, exif, 0, length); } parseRawExif(exif); }
protected void handleXMP(InputStream stream, int xmpLength, ImageMetadataExtractor extractor) throws IOException, TikaException, SAXException { byte[] xmp = new byte[xmpLength]; IOUtils.readFully(stream, xmp); extractor.parseRawXMP(xmp); } }
private byte[] readBytes(InputStream is, long len) throws IOException, TikaException { //initByteArray tests for "reading of too many bytes" byte[] bytes = initByteArray(len); IOUtils.readFully(is, bytes); return bytes; }
private String read2007and2010String(InputStream stream) throws IOException, TikaException { int stringLen = EndianUtils.readUShortLE(stream); byte[] stringData = new byte[stringLen * 2]; IOUtils.readFully(stream, stringData); String value = StringUtil.getFromUnicodeLE(stringData); // Some strings are null terminated if(value.charAt(value.length()-1) == 0) { value = value.substring(0, value.length()-1); } return value; }
int totalBytes = 0; while (totalBytes < buf.length) { IOUtils.readFully(plain, tmp, 0, 4); final int sid = LittleEndian.getUShort(tmp, 0); final int len = LittleEndian.getUShort(tmp, 2);
/** * Grab the offset, then skip there */ private boolean skipToPropertyInfoSection(InputStream stream, byte[] header) throws IOException, TikaException { // The offset is stored in the header from 0x20 onwards long offsetToSection = EndianUtils.getLongLE(header, 0x20); // Sanity check the offset. Some files seem to use a different format, // and the offset isn't available at 0x20. Until we can work out how // to find the offset in those files, skip them if detected if (offsetToSection > 0xa00000l) { // Header should never be more than 10mb into the file, something is wrong offsetToSection = 0; } // Work out how far to skip, and sanity check long toSkip = offsetToSection - header.length; if(offsetToSection == 0){ return false; } while (toSkip > 0) { byte[] skip = new byte[Math.min((int) toSkip, 0x4000)]; IOUtils.readFully(stream, skip); toSkip -= skip.length; } return true; }
private String read2004String(InputStream stream) throws IOException, TikaException { int stringLen = EndianUtils.readUShortLE(stream); byte[] stringData = new byte[stringLen]; IOUtils.readFully(stream, stringData); // Often but not always null terminated if (stringData[stringLen-1] == 0) { stringLen--; } String value = StringUtil.getFromCompressedUnicode(stringData, 0, stringLen); return value; }
private void handleText(int length, InputStream stream, XHTMLContentHandler xhtml) throws IOException, SAXException, TikaException { byte[] str = new byte[length]; IOUtils.readFully(stream, str); if(str[length-1] != 0) { // Not properly null terminated, must be wrong return; } String text = extractText(str, false); xhtml.startElement("p"); xhtml.characters(text); xhtml.endElement("p"); }
public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { // We only do metadata, for now XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); // What kind is it? byte[] first4 = new byte[4]; IOUtils.readFully(stream, first4); if (first4[0] == (byte)'M' && first4[1] == (byte)'Z') { parsePE(xhtml, metadata, stream, first4); } else if (first4[0] == (byte)0x7f && first4[1] == (byte)'E' && first4[2] == (byte)'L' && first4[3] == (byte)'F') { parseELF(xhtml, metadata, stream, first4); } // Finish everything xhtml.endDocument(); }
IOUtils.readFully(stream, header); String version = new String(header, 0, 6, "US-ASCII");