Example usage for org.apache.commons.vfs2 RandomAccessContent readFully

List of usage examples for org.apache.commons.vfs2 RandomAccessContent readFully

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 RandomAccessContent readFully.

Prototype

void readFully(byte b[]) throws IOException;

Source Link

Document

Reads some bytes from an input stream and stores them into the buffer array b .

Usage

From source file:org.freedesktop.mime.MagicEntry.java

public boolean match(FileObject file) throws IOException {
    RandomAccessContent s = file.getContent().getRandomAccessContent(RandomAccessMode.READ);
    try {/* w w w .j  a  v a 2  s . c om*/
        for (Pattern p : this) {

            byte[] val = p.getValue();
            byte[] mask = p.getMask();

            // Read in portion of file
            ByteBuffer buf = ByteBuffer.allocate(p.getRangeLength() + val.length);
            s.seek(p.getOffset());
            byte[] bufArr = buf.array();
            s.readFully(bufArr);

            int valIdx = 0;
            for (int i = 0; i < bufArr.length; i++) {
                if ((bufArr[i] & mask[valIdx]) == (val[valIdx] & mask[valIdx])) {
                    valIdx++;
                    if (valIdx == val.length) {
                        return true;
                    }
                } else {
                    valIdx = 0;
                }
            }
        }
    } finally {
        s.close();
    }
    return false;
}