Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

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

Prototype

public final void readFully(byte b[], int off, int len) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:fr.noop.subtitle.stl.StlParser.java

private StlTti readTti(DataInputStream dis, StlGsi gsi) throws IOException, InvalidTimeRangeException {
    // Get charset from gsi
    String charset = gsi.getCct().getCharset();

    // Get frame rate from gsi
    int frameRate = gsi.getDfc().getFrameRate();

    // Read and extract metadata from TTI block
    // Each TTI block is 128 bytes long
    StlTti tti = new StlTti();

    // Read Subtitle Group Number (SGN)
    tti.setSgn((short) dis.readUnsignedByte());

    // Read Subtitle Number (SN)
    tti.setSn(Short.reverseBytes(dis.readShort()));

    // Read Extension Block Number (EBN)
    tti.setEbn((short) dis.readUnsignedByte());

    // Read Cumulative Status (CS)
    tti.setCs((short) dis.readUnsignedByte());

    // Read Time Code In (TCI)
    tti.setTci(this.readTimeCode(dis, frameRate));

    // Read Time Code Out (TCO)
    tti.setTco(this.readTimeCode(dis, frameRate));

    // Read Vertical Position (VP)
    tti.setVp((short) dis.readUnsignedByte());

    // Read Justification Code (JC)
    tti.setJc(StlTti.Jc.getEnum(dis.readUnsignedByte()));

    // Read Comment Flag (CF)
    tti.setCf((short) dis.readUnsignedByte());

    // Read TextField (TF)
    byte[] tfBytes = new byte[112];
    dis.readFully(tfBytes, 0, 112);
    tti.setTf(new String(tfBytes, charset));

    // TTI is fully parsed
    return tti;/*  w ww .  j  a va2 s .  c  o  m*/
}

From source file:com.cardio3g.MainActivity.java

/**
 * Go through each of the Expansion APK files and open each as a zip file.
 * Calculate the CRC for each file and return false if any fail to match.
 *
 * @return true if XAPKZipFile is successful
 *///from  w ww  .j a va  2 s  .  c om
void validateXAPKZipFiles() {
    AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {

        @Override
        protected void onPreExecute() {
            mDownloadViewGroup.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Object... params) {
            for (XAPKFile xf : xAPKS) {
                String fileName = Helpers.getExpansionAPKFileName(MainActivity.this, xf.mIsMain,
                        xf.mFileVersion);
                if (!Helpers.doesFileExist(MainActivity.this, fileName, xf.mFileSize, false))
                    return false;
                fileName = Helpers.generateSaveFileName(MainActivity.this, fileName);
                ZipResourceFile zrf;
                byte[] buf = new byte[1024 * 256];
                try {
                    zrf = new ZipResourceFile(fileName);
                    ZipResourceFile.ZipEntryRO[] entries = zrf.getAllEntries();
                    /**
                     * First calculate the total compressed length
                     */
                    long totalCompressedLength = 0;
                    for (ZipResourceFile.ZipEntryRO entry : entries) {
                        totalCompressedLength += entry.mCompressedLength;
                    }
                    float averageVerifySpeed = 0;
                    long totalBytesRemaining = totalCompressedLength;
                    long timeRemaining;
                    /**
                     * Then calculate a CRC for every file in the Zip file,
                     * comparing it to what is stored in the Zip directory.
                     * Note that for compressed Zip files we must extract
                     * the contents to do this comparison.
                     */
                    for (ZipResourceFile.ZipEntryRO entry : entries) {
                        if (-1 != entry.mCRC32) {
                            long length = entry.mUncompressedLength;
                            CRC32 crc = new CRC32();
                            DataInputStream dis = null;
                            try {
                                dis = new DataInputStream(zrf.getInputStream(entry.mFileName));

                                long startTime = SystemClock.uptimeMillis();
                                while (length > 0) {
                                    int seek = (int) (length > buf.length ? buf.length : length);
                                    dis.readFully(buf, 0, seek);
                                    crc.update(buf, 0, seek);
                                    length -= seek;
                                    long currentTime = SystemClock.uptimeMillis();
                                    long timePassed = currentTime - startTime;
                                    if (timePassed > 0) {
                                        float currentSpeedSample = (float) seek / (float) timePassed;
                                        if (0 != averageVerifySpeed) {
                                            averageVerifySpeed = SMOOTHING_FACTOR * currentSpeedSample
                                                    + (1 - SMOOTHING_FACTOR) * averageVerifySpeed;
                                        } else {
                                            averageVerifySpeed = currentSpeedSample;
                                        }
                                        totalBytesRemaining -= seek;
                                        timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
                                        this.publishProgress(new DownloadProgressInfo(totalCompressedLength,
                                                totalCompressedLength - totalBytesRemaining, timeRemaining,
                                                averageVerifySpeed));
                                    }
                                    startTime = currentTime;
                                    if (mCancelValidation)
                                        return true;
                                }
                                if (crc.getValue() != entry.mCRC32) {
                                    Log.e("EXPANSION ERROR",
                                            "CRC does not match for entry: " + entry.mFileName);
                                    Log.e("EXPANSION ERROR", "In file: " + entry.getZipFileName());
                                    return false;
                                }
                            } finally {
                                if (null != dis) {
                                    dis.close();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            return true;
        }

        @Override
        protected void onProgressUpdate(DownloadProgressInfo... values) {
            onDownloadProgress(values[0]);
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                mDownloadViewGroup.setVisibility(View.GONE);
            } else {
                mDownloadViewGroup.setVisibility(View.VISIBLE);
            }
            super.onPostExecute(result);
        }

    };
    validationTask.execute(new Object());
}

From source file:de.tum.frm2.nicos_android.nicos.NicosClient.java

public TupleOfTwo<Byte, Object> _read() throws ProtocolError {
    // receive first byte + (possibly) length
    DataInputStream din = new DataInputStream(socketIn);
    byte start;/*  www.  j  a  va2 s.  co m*/
    try {
        start = din.readByte();
    } catch (IOException e) {
        throw new ProtocolError("connection broken");
    }
    if (start == daemon.ACK) {
        // ACK == executed ok, no more information follows
        return new TupleOfTwo<>(start, null);
    }

    if (start != daemon.NAK && start != daemon.STX) {
        // Server respondend with neither NAK (error) nor STX (ok)
        throw new ProtocolError("invalid response " + String.valueOf(start));
    }

    // it has a length...
    int length;
    try {
        length = din.readInt();
    } catch (IOException e) {
        throw new ProtocolError("connection broken");
    }
    // Cannot concat these two try blocks: We need length before allocating msg.
    // And msg needs to be declared outside of try block to be accessible afterwards.
    try {
        byte[] msg = new byte[length];
        try {
            din.readFully(msg, 0, length);
        } catch (IOException e) {
            throw new ProtocolError("connection broken");
        }

        Unpickler unpickler = new Unpickler();
        Object result = null;
        try {
            result = unpickler.loads(msg);
        } catch (Exception e) {
            // result stays at null.
            handle_error(e);
        }
        return new TupleOfTwo<>(start, result);
    } catch (OutOfMemoryError e) {
        throw new ProtocolError("bad response");
    }
}

From source file:de.tum.frm2.nicos_android.nicos.NicosClient.java

public void event_handler() {
    DataInputStream din = new DataInputStream(eventSocketIn);
    while (true) {
        try {/*from  ww w.j a va  2  s  . c  om*/
            // receive STX (1 byte) + eventcode (2) + length (4)
            byte[] start = new byte[7];
            try {
                //noinspection ResultOfMethodCallIgnored
                din.read(start);
            } catch (IOException e) {
                if (!disconnecting) {
                    signal("broken", "Server connection broken.");
                    _close();
                }
                return;
            }
            if (start[0] != daemon.STX) {
                // Every event starts with STX. Else, something's wrong.
                if (!disconnecting) {
                    signal("broken", "Server connection broken.");
                    _close();
                }
                return;
            }
            byte[] slice = Arrays.copyOfRange(start, 3, 7);
            ByteBuffer bb = ByteBuffer.wrap(slice);
            bb.order(ByteOrder.BIG_ENDIAN);

            // Get length, allocate byte buffer.
            int length = bb.getInt();
            byte[] buf = new byte[length];

            // Read length bytes and store them in buf.
            din.readFully(buf, 0, length);

            boolean should_signal = true;
            String event = null;
            Object data = null;
            try {
                // Stackoverflow magic to convert 2 bytes to int which can be compared in
                // daemon.command2event().
                int eventcode = ((start[1] & 0xff) << 8) | (start[2] & 0x00ff);
                event = daemon.command2event(eventcode);
                // serialized or raw data?
                if (daemon.eventNeedsUnserialize(event)) {
                    Unpickler unpickler = new Unpickler();
                    data = unpickler.loads(buf);
                } else {
                    data = buf;
                }
            } catch (Exception e) {
                // Garbled event
                should_signal = false;
            }
            if (should_signal) {
                signal(event, data);
            }
        } catch (Exception e) {
            if (!disconnecting) {
                signal("broken", "Server connection broken.");
                _close();
            }
            return;
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.BlockPoolSlice.java

/**
 * Find out the number of bytes in the block that match its crc.
 * <p/>//from  ww w.  ja v  a  2 s  .  c  om
 * This algorithm assumes that data corruption caused by unexpected
 * datanode shutdown occurs only in the last crc chunk. So it checks
 * only the last chunk.
 *
 * @param blockFile
 *     the block file
 * @param genStamp
 *     generation stamp of the block
 * @return the number of valid bytes
 */
private long validateIntegrityAndSetLength(File blockFile, long genStamp) {
    DataInputStream checksumIn = null;
    InputStream blockIn = null;
    try {
        final File metaFile = FsDatasetUtil.getMetaFile(blockFile, genStamp);
        long blockFileLen = blockFile.length();
        long metaFileLen = metaFile.length();
        int crcHeaderLen = DataChecksum.getChecksumHeaderSize();
        if (!blockFile.exists() || blockFileLen == 0 || !metaFile.exists() || metaFileLen < crcHeaderLen) {
            return 0;
        }
        checksumIn = new DataInputStream(
                new BufferedInputStream(new FileInputStream(metaFile), HdfsConstants.IO_FILE_BUFFER_SIZE));

        // read and handle the common header here. For now just a version
        BlockMetadataHeader header = BlockMetadataHeader.readHeader(checksumIn);
        short version = header.getVersion();
        if (version != BlockMetadataHeader.VERSION) {
            FsDatasetImpl.LOG
                    .warn("Wrong version (" + version + ") for metadata file " + metaFile + " ignoring ...");
        }
        DataChecksum checksum = header.getChecksum();
        int bytesPerChecksum = checksum.getBytesPerChecksum();
        int checksumSize = checksum.getChecksumSize();
        long numChunks = Math.min((blockFileLen + bytesPerChecksum - 1) / bytesPerChecksum,
                (metaFileLen - crcHeaderLen) / checksumSize);
        if (numChunks == 0) {
            return 0;
        }
        IOUtils.skipFully(checksumIn, (numChunks - 1) * checksumSize);
        blockIn = new FileInputStream(blockFile);
        long lastChunkStartPos = (numChunks - 1) * bytesPerChecksum;
        IOUtils.skipFully(blockIn, lastChunkStartPos);
        int lastChunkSize = (int) Math.min(bytesPerChecksum, blockFileLen - lastChunkStartPos);
        byte[] buf = new byte[lastChunkSize + checksumSize];
        checksumIn.readFully(buf, lastChunkSize, checksumSize);
        IOUtils.readFully(blockIn, buf, 0, lastChunkSize);

        checksum.update(buf, 0, lastChunkSize);
        long validFileLength;
        if (checksum.compare(buf, lastChunkSize)) { // last chunk matches crc
            validFileLength = lastChunkStartPos + lastChunkSize;
        } else { // last chunck is corrupt
            validFileLength = lastChunkStartPos;
        }

        // truncate if extra bytes are present without CRC
        if (blockFile.length() > validFileLength) {
            RandomAccessFile blockRAF = new RandomAccessFile(blockFile, "rw");
            try {
                // truncate blockFile
                blockRAF.setLength(validFileLength);
            } finally {
                blockRAF.close();
            }
        }

        return validFileLength;
    } catch (IOException e) {
        FsDatasetImpl.LOG.warn(e);
        return 0;
    } finally {
        IOUtils.closeStream(checksumIn);
        IOUtils.closeStream(blockIn);
    }
}