Example usage for java.io DataInputStream available

List of usage examples for java.io DataInputStream available

Introduction

In this page you can find the example usage for java.io DataInputStream available.

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Usage

From source file:org.apache.cassandra.db.SuperColumn.java

public IColumn deserialize(DataInputStream dis, String name, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;

    String[] names = RowMutation.getColumnAndColumnFamily(name);
    if (names.length == 1) {
        IColumn superColumn = defreezeSuperColumn(dis);
        if (name.equals(superColumn.name())) {
            /* read the number of columns stored */
            int size = dis.readInt();
            /* read the size of all columns */
            dis.readInt();/*from  ww  w  .j a  va  2  s.  c om*/
            IColumn column = null;
            for (int i = 0; i < size; ++i) {
                column = Column.serializer().deserialize(dis, filter);
                if (column != null) {
                    superColumn.addColumn(column.name(), column);
                    column = null;
                    if (filter.isDone()) {
                        break;
                    }
                }
            }
            return superColumn;
        } else {
            /* read the number of columns stored */
            dis.readInt();
            /* read the size of all columns to skip */
            int size = dis.readInt();
            dis.skip(size);
            return null;
        }
    }

    SuperColumn superColumn = defreezeSuperColumn(dis);
    if (!superColumn.isMarkedForDelete()) {
        int size = dis.readInt();
        /* skip the size of the columns */
        dis.readInt();
        if (size > 0) {
            for (int i = 0; i < size; ++i) {
                IColumn subColumn = Column.serializer().deserialize(dis, names[1], filter);
                if (subColumn != null) {
                    superColumn.addColumn(subColumn.name(), subColumn);
                    break;
                }
            }
        }
    }
    return superColumn;
}

From source file:org.rifidi.emulator.io.comm.serial.SerialCommunicationIncomingMessageHandler.java

public void run() {
    // byte[] message = null;
    DataBuffer<byte[]> newBuffer = host.getReceiveBuffer();
    ArrayList<Byte> receivedData = new ArrayList<Byte>();

    boolean keepRunning = true;

    DataInputStream dataStream = new DataInputStream(serialIn);

    while (keepRunning) {

        boolean eof = false;

        /* Read as fully as we can */
        while (!eof) {

            try {
                /* Attempt to read in a byte */
                receivedData.add(new Byte(dataStream.readByte()));
                if (dataStream.available() <= 0) {
                    eof = true;//from   w  w w.ja  v  a2  s  . c  o m
                }
            } catch (EOFException eofe) {
                /* Indicate end of file */
                eof = true;
            } catch (IOException e) {
                logger.error(e.getMessage());
                eof = true;
            }
        }

        /* Check that we have something to "receive" */
        if (!receivedData.isEmpty()) {
            logger.debug("Back in the if statement");
            /* Construct an array of bytes */
            byte[] receivedBytes = new byte[receivedData.size()];
            for (int i = 0; i < receivedData.size(); i++) {
                receivedBytes[i] = receivedData.get(i).byteValue();
            }
            receivedData.clear();
            if (keepRunning) {
                /* Actually add to the receive buffer */
                try {
                    logger.debug("Adding to buffer: " + receivedBytes);
                    List<byte[]> listOfBytes = this.host.getProtocol().removeProtocol(receivedBytes);
                    for (byte[] b : listOfBytes) {
                        newBuffer.addToBuffer(b);
                    }
                    String x = "";
                    for (byte i : receivedBytes) {
                        x += String.format("%1$02x", i) + " ";
                    }
                    this.host.getConsoleLogger().info("<INPUT> " + x + "</INPUT>");
                } catch (DataBufferInterruptedException e) {
                    logger.error(e.getMessage());
                    keepRunning = false;
                } catch (ProtocolValidationException e) {
                    logger.error(e.getMessage());
                    keepRunning = false;
                }
            }
        }
    }
}

From source file:org.kawanfw.commons.util.Sha1Util.java

/**
 * Gets the a file hash value./* w w  w  .j a va  2s.c o m*/
 * 
 * @param file the file to hash
 *            
 * @return the file hash as bytes
 * 
 * @exception IOException
 *                an I/O error occurred
 * @exception NoSuchAlgorithmException
 *                hash algorithm not found
 * @exception NoSuchProviderException
 *                hash provider not found
 */

public byte[] getFileHash(File file) throws IOException, NoSuchAlgorithmException, NoSuchProviderException {

    FileInputStream fisIn = new FileInputStream(file);
    BufferedInputStream bisIn = new BufferedInputStream(fisIn);

    DataInputStream disIn = null;

    try {
        disIn = new DataInputStream(bisIn);

        // Bytes containing the MD5 or SHA-1 Hash Code
        byte[] bytesHash = new byte[160];

        MessageDigest mdInstance = MessageDigest.getInstance("SHA-1");

        int bytesRead;
        int total = disIn.available();

        if (total == 0) {
            return null;
        }

        if (total > READ_BUFFER_SIZE) {
            total = READ_BUFFER_SIZE;
        }

        byte bRead[] = new byte[total];

        while ((bytesRead = disIn.read(bRead)) > 0) {
            mdInstance.update(bRead, 0, bytesRead);
        }

        bytesHash = mdInstance.digest();

        return bytesHash;
    } finally {
        IOUtils.closeQuietly(disIn);
    }

}

From source file:com.facebook.infrastructure.db.SuperColumn.java

public IColumn deserialize(DataInputStream dis, String name, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;

    String[] names = RowMutation.getColumnAndColumnFamily(name);
    if (names.length == 1) {
        IColumn superColumn = defreezeSuperColumn(dis);
        if (name.equals(superColumn.name())) {
            if (!superColumn.isMarkedForDelete()) {
                /* read the number of columns stored */
                int size = dis.readInt();
                /* read the size of all columns */
                dis.readInt();/*ww  w.j  a  va2 s . c o  m*/
                IColumn column = null;
                for (int i = 0; i < size; ++i) {
                    column = Column.serializer().deserialize(dis, filter);
                    if (column != null) {
                        superColumn.addColumn(column.name(), column);
                        column = null;
                        if (filter.isDone()) {
                            break;
                        }
                    }
                }
            }
            return superColumn;
        } else {
            /* read the number of columns stored */
            dis.readInt();
            /* read the size of all columns to skip */
            int size = dis.readInt();
            dis.skip(size);
            return null;
        }
    }

    SuperColumn superColumn = defreezeSuperColumn(dis);
    if (!superColumn.isMarkedForDelete()) {
        int size = dis.readInt();
        /* skip the size of the columns */
        dis.readInt();
        if (size > 0) {
            for (int i = 0; i < size; ++i) {
                IColumn subColumn = Column.serializer().deserialize(dis, names[1], filter);
                if (subColumn != null) {
                    superColumn.addColumn(subColumn.name(), subColumn);
                    break;
                }
            }
        }
    }
    return superColumn;
}

From source file:ubic.gemma.core.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

/**
 * @return A 1 byte character string. A string object is stored as an INT (to store the string length) followed by the CHAR
 * array (to store the string contents).
 *///from w  ww . j  a  v  a 2  s .  co m
private String readString(DataInputStream str) throws IOException {
    int fieldLength = this.readIntBigEndian(str);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        buf.append(new String(new byte[] { str.readByte() }));
    }
    return buf.toString();
}

From source file:ubic.gemma.core.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

/**
 * The data is stored as a int, then the array of bytes.
 *///from w  w w.ja va2 s .  c o m
private byte[] readBytes(DataInputStream str) throws IOException {
    int fieldLength = this.readIntBigEndian(str);

    byte[] result = new byte[fieldLength];
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        result[i] = str.readByte();
    }

    return result;
}

From source file:be.ibridge.kettle.job.entry.filecompare.JobEntryFileCompare.java

/**
 * Check whether 2 files have the same contents.
 *
 * @param file1 first file to compare/*w  ww . j av  a 2  s.c o  m*/
 * @param file2 second file to compare
 * @return true if files are equal, false if they are not
 *
 * @throws IOException upon IO problems
 */
protected boolean equalFileContents(FileObject file1, FileObject file2) throws IOException {
    // Really read the contents and do comparisons

    DataInputStream in1 = new DataInputStream(
            new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file1))));
    DataInputStream in2 = new DataInputStream(
            new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file2))));

    char ch1, ch2;
    while (in1.available() != 0 && in2.available() != 0) {
        ch1 = (char) in1.readByte();
        ch2 = (char) in2.readByte();
        if (ch1 != ch2)
            return false;
    }
    if (in1.available() != in2.available()) {
        return false;
    } else {
        return true;
    }
}

From source file:ZipImploder.java

/** Read all the bytes in a stream */
protected byte[] readAllBytes(DataInputStream is) throws IOException {
    byte[] bytes = new byte[0];
    for (int len = is.available(); len > 0; len = is.available()) {
        byte[] xbytes = new byte[len];
        int count = is.read(xbytes);
        if (count > 0) {
            byte[] nbytes = new byte[bytes.length + count];
            System.arraycopy(bytes, 0, nbytes, 0, bytes.length);
            System.arraycopy(xbytes, 0, nbytes, bytes.length, count);
            bytes = nbytes;//from w  w  w  .ja  v a 2  s  .c o  m
        }
    }
    return bytes;
}

From source file:ClipboardExample.java

public Object nativeToJava(TransferData transferData) {
    if (isSupportedType(transferData)) {

        byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null)
            return null;

        MyType[] myData = new MyType[0];
        try {//w w  w  .  ja  va2 s. c om
            ByteArrayInputStream in = new ByteArrayInputStream(buffer);
            DataInputStream readIn = new DataInputStream(in);
            while (readIn.available() > 20) {
                MyType datum = new MyType();
                int size = readIn.readInt();
                byte[] name = new byte[size];
                readIn.read(name);
                datum.firstName = new String(name);
                size = readIn.readInt();
                name = new byte[size];
                readIn.read(name);
                datum.lastName = new String(name);
                MyType[] newMyData = new MyType[myData.length + 1];
                System.arraycopy(myData, 0, newMyData, 0, myData.length);
                newMyData[myData.length] = datum;
                myData = newMyData;
            }
            readIn.close();
        } catch (IOException ex) {
            return null;
        }
        return myData;
    }

    return null;
}