Example usage for java.io DataInput readUnsignedByte

List of usage examples for java.io DataInput readUnsignedByte

Introduction

In this page you can find the example usage for java.io DataInput readUnsignedByte.

Prototype

int readUnsignedByte() throws IOException;

Source Link

Document

Reads one input byte, zero-extends it to type int , and returns the result, which is therefore in the range 0 through 255 .

Usage

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readStreamsInfo(final DataInput header, final Archive archive) throws IOException {
    int nid = header.readUnsignedByte();

    if (nid == NID.kPackInfo) {
        readPackInfo(header, archive);//  w  w w .jav  a 2 s  . c o  m
        nid = header.readUnsignedByte();
    }

    if (nid == NID.kUnpackInfo) {
        readUnpackInfo(header, archive);
        nid = header.readUnsignedByte();
    } else {
        // archive without unpack/coders info
        archive.folders = new Folder[0];
    }

    if (nid == NID.kSubStreamsInfo) {
        readSubStreamsInfo(header, archive);
        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated StreamsInfo");
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readHeader(final DataInput header, final Archive archive) throws IOException {
    int nid = header.readUnsignedByte();

    if (nid == NID.kArchiveProperties) {
        readArchiveProperties(header);/*  www  .j  av a2 s. c om*/
        nid = header.readUnsignedByte();
    }

    if (nid == NID.kAdditionalStreamsInfo) {
        throw new IOException("Additional streams unsupported");
        //nid = header.readUnsignedByte();
    }

    if (nid == NID.kMainStreamsInfo) {
        readStreamsInfo(header, archive);
        nid = header.readUnsignedByte();
    }

    if (nid == NID.kFilesInfo) {
        readFilesInfo(header, archive);
        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated header, found " + nid);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readUnpackInfo(final DataInput header, final Archive archive) throws IOException {
    int nid = header.readUnsignedByte();
    if (nid != NID.kFolder) {
        throw new IOException("Expected kFolder, got " + nid);
    }/*from  ww w .  java 2  s.co  m*/
    final long numFolders = readUint64(header);
    final Folder[] folders = new Folder[(int) numFolders];
    archive.folders = folders;
    final int external = header.readUnsignedByte();
    if (external != 0) {
        throw new IOException("External unsupported");
    }
    for (int i = 0; i < (int) numFolders; i++) {
        folders[i] = readFolder(header);
    }

    nid = header.readUnsignedByte();
    if (nid != NID.kCodersUnpackSize) {
        throw new IOException("Expected kCodersUnpackSize, got " + nid);
    }
    for (final Folder folder : folders) {
        folder.unpackSizes = new long[(int) folder.totalOutputStreams];
        for (int i = 0; i < folder.totalOutputStreams; i++) {
            folder.unpackSizes[i] = readUint64(header);
        }
    }

    nid = header.readUnsignedByte();
    if (nid == NID.kCRC) {
        final BitSet crcsDefined = readAllOrBits(header, (int) numFolders);
        for (int i = 0; i < (int) numFolders; i++) {
            if (crcsDefined.get(i)) {
                folders[i].hasCrc = true;
                folders[i].crc = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            } else {
                folders[i].hasCrc = false;
            }
        }

        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated UnpackInfo");
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readArchiveProperties(final DataInput input) throws IOException {
    // FIXME: the reference implementation just throws them away?
    int nid = input.readUnsignedByte();
    while (nid != NID.kEnd) {
        final long propertySize = readUint64(input);
        final byte[] property = new byte[(int) propertySize];
        input.readFully(property);// w w w.j  a v a2  s  . c  om
        nid = input.readUnsignedByte();
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readPackInfo(final DataInput header, final Archive archive) throws IOException {
    archive.packPos = readUint64(header);
    final long numPackStreams = readUint64(header);
    int nid = header.readUnsignedByte();
    if (nid == NID.kSize) {
        archive.packSizes = new long[(int) numPackStreams];
        for (int i = 0; i < archive.packSizes.length; i++) {
            archive.packSizes[i] = readUint64(header);
        }//ww w .  j a  va  2s  .com
        nid = header.readUnsignedByte();
    }

    if (nid == NID.kCRC) {
        archive.packCrcsDefined = readAllOrBits(header, (int) numPackStreams);
        archive.packCrcs = new long[(int) numPackStreams];
        for (int i = 0; i < (int) numPackStreams; i++) {
            if (archive.packCrcsDefined.get(i)) {
                archive.packCrcs[i] = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            }
        }

        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated PackInfo (" + nid + ")");
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private BitSet readBits(final DataInput header, final int size) throws IOException {
    final BitSet bits = new BitSet(size);
    int mask = 0;
    int cache = 0;
    for (int i = 0; i < size; i++) {
        if (mask == 0) {
            mask = 0x80;/*from  w ww.  java 2s .  co  m*/
            cache = header.readUnsignedByte();
        }
        bits.set(i, (cache & mask) != 0);
        mask >>>= 1;
    }
    return bits;
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readSubStreamsInfo(final DataInput header, final Archive archive) throws IOException {
    for (final Folder folder : archive.folders) {
        folder.numUnpackSubStreams = 1;//from  w  ww .  j a v a2 s  .  c  o  m
    }
    int totalUnpackStreams = archive.folders.length;

    int nid = header.readUnsignedByte();
    if (nid == NID.kNumUnpackStream) {
        totalUnpackStreams = 0;
        for (final Folder folder : archive.folders) {
            final long numStreams = readUint64(header);
            folder.numUnpackSubStreams = (int) numStreams;
            totalUnpackStreams += numStreams;
        }
        nid = header.readUnsignedByte();
    }

    final SubStreamsInfo subStreamsInfo = new SubStreamsInfo();
    subStreamsInfo.unpackSizes = new long[totalUnpackStreams];
    subStreamsInfo.hasCrc = new BitSet(totalUnpackStreams);
    subStreamsInfo.crcs = new long[totalUnpackStreams];

    int nextUnpackStream = 0;
    for (final Folder folder : archive.folders) {
        if (folder.numUnpackSubStreams == 0) {
            continue;
        }
        long sum = 0;
        if (nid == NID.kSize) {
            for (int i = 0; i < folder.numUnpackSubStreams - 1; i++) {
                final long size = readUint64(header);
                subStreamsInfo.unpackSizes[nextUnpackStream++] = size;
                sum += size;
            }
        }
        subStreamsInfo.unpackSizes[nextUnpackStream++] = folder.getUnpackSize() - sum;
    }
    if (nid == NID.kSize) {
        nid = header.readUnsignedByte();
    }

    int numDigests = 0;
    for (final Folder folder : archive.folders) {
        if (folder.numUnpackSubStreams != 1 || !folder.hasCrc) {
            numDigests += folder.numUnpackSubStreams;
        }
    }

    if (nid == NID.kCRC) {
        final BitSet hasMissingCrc = readAllOrBits(header, numDigests);
        final long[] missingCrcs = new long[numDigests];
        for (int i = 0; i < numDigests; i++) {
            if (hasMissingCrc.get(i)) {
                missingCrcs[i] = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            }
        }
        int nextCrc = 0;
        int nextMissingCrc = 0;
        for (final Folder folder : archive.folders) {
            if (folder.numUnpackSubStreams == 1 && folder.hasCrc) {
                subStreamsInfo.hasCrc.set(nextCrc, true);
                subStreamsInfo.crcs[nextCrc] = folder.crc;
                ++nextCrc;
            } else {
                for (int i = 0; i < folder.numUnpackSubStreams; i++) {
                    subStreamsInfo.hasCrc.set(nextCrc, hasMissingCrc.get(nextMissingCrc));
                    subStreamsInfo.crcs[nextCrc] = missingCrcs[nextMissingCrc];
                    ++nextCrc;
                    ++nextMissingCrc;
                }
            }
        }

        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated SubStreamsInfo");
    }

    archive.subStreamsInfo = subStreamsInfo;
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private Folder readFolder(final DataInput header) throws IOException {
    final Folder folder = new Folder();

    final long numCoders = readUint64(header);
    final Coder[] coders = new Coder[(int) numCoders];
    long totalInStreams = 0;
    long totalOutStreams = 0;
    for (int i = 0; i < coders.length; i++) {
        coders[i] = new Coder();
        final int bits = header.readUnsignedByte();
        final int idSize = bits & 0xf;
        final boolean isSimple = (bits & 0x10) == 0;
        final boolean hasAttributes = (bits & 0x20) != 0;
        final boolean moreAlternativeMethods = (bits & 0x80) != 0;

        coders[i].decompressionMethodId = new byte[idSize];
        header.readFully(coders[i].decompressionMethodId);
        if (isSimple) {
            coders[i].numInStreams = 1;/*from   w  w w  .ja  v  a2s.c  o m*/
            coders[i].numOutStreams = 1;
        } else {
            coders[i].numInStreams = readUint64(header);
            coders[i].numOutStreams = readUint64(header);
        }
        totalInStreams += coders[i].numInStreams;
        totalOutStreams += coders[i].numOutStreams;
        if (hasAttributes) {
            final long propertiesSize = readUint64(header);
            coders[i].properties = new byte[(int) propertiesSize];
            header.readFully(coders[i].properties);
        }
        // would need to keep looping as above:
        while (moreAlternativeMethods) {
            throw new IOException("Alternative methods are unsupported, please report. "
                    + "The reference implementation doesn't support them either.");
        }
    }
    folder.coders = coders;
    folder.totalInputStreams = totalInStreams;
    folder.totalOutputStreams = totalOutStreams;

    if (totalOutStreams == 0) {
        throw new IOException("Total output streams can't be 0");
    }
    final long numBindPairs = totalOutStreams - 1;
    final BindPair[] bindPairs = new BindPair[(int) numBindPairs];
    for (int i = 0; i < bindPairs.length; i++) {
        bindPairs[i] = new BindPair();
        bindPairs[i].inIndex = readUint64(header);
        bindPairs[i].outIndex = readUint64(header);
    }
    folder.bindPairs = bindPairs;

    if (totalInStreams < numBindPairs) {
        throw new IOException("Total input streams can't be less than the number of bind pairs");
    }
    final long numPackedStreams = totalInStreams - numBindPairs;
    final long packedStreams[] = new long[(int) numPackedStreams];
    if (numPackedStreams == 1) {
        int i;
        for (i = 0; i < (int) totalInStreams; i++) {
            if (folder.findBindPairForInStream(i) < 0) {
                break;
            }
        }
        if (i == (int) totalInStreams) {
            throw new IOException("Couldn't find stream's bind pair index");
        }
        packedStreams[0] = i;
    } else {
        for (int i = 0; i < (int) numPackedStreams; i++) {
            packedStreams[i] = readUint64(header);
        }
    }
    folder.packedStreams = packedStreams;

    return folder;
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readFilesInfo(final DataInput header, final Archive archive) throws IOException {
    final long numFiles = readUint64(header);
    final SevenZArchiveEntry[] files = new SevenZArchiveEntry[(int) numFiles];
    for (int i = 0; i < files.length; i++) {
        files[i] = new SevenZArchiveEntry();
    }//from w  w  w .  jav  a 2s  .  co m
    BitSet isEmptyStream = null;
    BitSet isEmptyFile = null;
    BitSet isAnti = null;
    while (true) {
        final int propertyType = header.readUnsignedByte();
        if (propertyType == 0) {
            break;
        }
        final long size = readUint64(header);
        switch (propertyType) {
        case NID.kEmptyStream: {
            isEmptyStream = readBits(header, files.length);
            break;
        }
        case NID.kEmptyFile: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kEmptyFile");
            }
            isEmptyFile = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kAnti: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kAnti");
            }
            isAnti = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kName: {
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Not implemented");
            }
            if (((size - 1) & 1) != 0) {
                throw new IOException("File names length invalid");
            }
            final byte[] names = new byte[(int) (size - 1)];
            header.readFully(names);
            int nextFile = 0;
            int nextName = 0;
            for (int i = 0; i < names.length; i += 2) {
                if (names[i] == 0 && names[i + 1] == 0) {
                    files[nextFile].setName(new String(names, nextName, i - nextName, CharsetNames.UTF_16LE));
                    nextName = i + 2;
                    this.mapFilename.put(files[nextFile].getName(), nextFile);
                    nextFile++;
                }
            }
            if (nextName != names.length || nextFile != files.length) {
                throw new IOException("Error parsing file names");
            }
            break;
        }
        case NID.kCTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasCreationDate(timesDefined.get(i));
                if (files[i].getHasCreationDate()) {
                    files[i].setCreationDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kATime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasAccessDate(timesDefined.get(i));
                if (files[i].getHasAccessDate()) {
                    files[i].setAccessDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kMTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasLastModifiedDate(timesDefined.get(i));
                if (files[i].getHasLastModifiedDate()) {
                    files[i].setLastModifiedDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kWinAttributes: {
            final BitSet attributesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasWindowsAttributes(attributesDefined.get(i));
                if (files[i].getHasWindowsAttributes()) {
                    files[i].setWindowsAttributes(Integer.reverseBytes(header.readInt()));
                }
            }
            break;
        }
        case NID.kStartPos: {
            throw new IOException("kStartPos is unsupported, please report");
        }
        case NID.kDummy: {
            // 7z 9.20 asserts the content is all zeros and ignores the property
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287

            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete kDummy property");
            }
            break;
        }

        default: {
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287
            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete property of type " + propertyType);
            }
            break;
        }
        }
    }
    int nonEmptyFileCounter = 0;
    int emptyFileCounter = 0;
    for (int i = 0; i < files.length; i++) {
        files[i].setHasStream(isEmptyStream == null ? true : !isEmptyStream.get(i));
        if (files[i].hasStream()) {
            files[i].setDirectory(false);
            files[i].setAntiItem(false);
            files[i].setHasCrc(archive.subStreamsInfo.hasCrc.get(nonEmptyFileCounter));
            files[i].setCrcValue(archive.subStreamsInfo.crcs[nonEmptyFileCounter]);
            files[i].setSize(archive.subStreamsInfo.unpackSizes[nonEmptyFileCounter]);
            ++nonEmptyFileCounter;
        } else {
            files[i].setDirectory(isEmptyFile == null ? true : !isEmptyFile.get(emptyFileCounter));
            files[i].setAntiItem(isAnti == null ? false : isAnti.get(emptyFileCounter));
            files[i].setHasCrc(false);
            files[i].setSize(0);

            ++emptyFileCounter;
        }
    }
    archive.files = files;
    calculateStreamMap(archive);
}

From source file:dk.statsbiblioteket.util.LineReaderTest.java

public void testSample(String type, DataInput in) throws Exception {
    assertEquals("Int 1 should work for " + type, 12345, in.readInt());
    assertEquals("Int 2 should work for " + type, -87, in.readInt());
    assertEquals("Long should work for " + type, 123456789L, in.readLong());
    assertEquals("String 1 should work for " + type, "Hello World!", in.readLine());
    assertEquals("String 2 should work for " + type, "Another world", in.readLine());
    assertEquals("Float should work for " + type, 0.5f, in.readFloat());
    assertEquals("Boolean 1 should work for " + type, true, in.readBoolean());
    assertEquals("Boolean 2 should work for " + type, false, in.readBoolean());
    assertEquals("Byte 1 should work for " + type, (byte) 12, in.readByte());
    assertEquals("Byte 2 should work for " + type, (byte) -12, in.readByte());
    assertEquals("Unsigned byte should work for " + type, 129, in.readUnsignedByte());
    assertEquals("Short should work for " + type, -4567, in.readShort());
    byte[] loaded = new byte[5];
    byte[] expected = new byte[] { (byte) 'A', (byte) 'S', (byte) 'C', (byte) 'I', (byte) 'I' };
    in.readFully(loaded);/*from   w ww.  ja v  a2s. c  om*/
    for (int i = 0; i < loaded.length; i++) {
        assertEquals("Byte-stored string should be equal at byte " + i + " for " + type, expected[i],
                loaded[i]);
    }
}