Example usage for java.io DataInputStream readLong

List of usage examples for java.io DataInputStream readLong

Introduction

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

Prototype

public final long readLong() throws IOException 

Source Link

Document

See the general contract of the readLong method of DataInput.

Usage

From source file:org.sakaiproject.nakamura.auth.trusted.TokenStore.java

/**
 *
 *//*from w  w  w  .ja  v  a  2s .c  o m*/
private void loadLocalSecretKeys() {
    FileInputStream fin = null;
    DataInputStream keyInputStream = null;
    try {
        fin = new FileInputStream(tokenFile);
        keyInputStream = new DataInputStream(fin);
        int newCurrentToken = keyInputStream.readInt();
        long newNextUpdate = keyInputStream.readLong();
        ExpiringSecretKey[] newKeys = new ExpiringSecretKey[5];
        for (int i = 0; i < newKeys.length; i++) {
            int isNull = keyInputStream.readInt();
            if (isNull == 1) {
                long expires = keyInputStream.readLong();
                String keyServerId = keyInputStream.readUTF();
                int l = keyInputStream.readInt();
                byte[] b = new byte[l];
                if (keyInputStream.read(b) != l) {
                    throw new IOException(
                            "Failed to read Key no " + i + " from Secret Keys, end of file reached ");
                }
                newKeys[i] = new ExpiringSecretKey(b, HMAC_SHA1, expires, keyServerId);
                getServerKeyCache().put(getCacheKey(keyServerId, i), newKeys[i].getSecretKeyData());
                LOG.info("Loaded Key {} from Local Store into {} ", getCacheKey(keyServerId, i),
                        getServerKeyCache());
            } else {
                newKeys[i] = null;
            }
        }
        keyInputStream.close();
        nextUpdate = newNextUpdate;
        secretKeyId = newCurrentToken;
        secretKeyRingBuffer = newKeys;

    } catch (IOException e) {
        LOG.error("Failed to load cookie keys " + e.getMessage());
    } finally {
        try {
            keyInputStream.close();
        } catch (Exception e) {
        }
        try {
            fin.close();
        } catch (Exception e) {
        }
    }
    if (secretKeyRingBuffer == null) {
        secretKeyRingBuffer = new ExpiringSecretKey[5];
        nextUpdate = System.currentTimeMillis();
        secretKeyId = 0;
    }
    if (debugCookies) {
        dumpSecretKeyRingBuffer(secretKeyRingBuffer);
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Reads the metadata and sends the data in one 'DATA_CHUNK'.
 * @param in//from w ww .j  ava  2 s  . c  o  m
 */
void readMetadata(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : readMetadata()");
    Block block = new Block(in.readLong(), 0, in.readLong());
    MetaDataInputStream checksumIn = null;
    DataOutputStream out = null;

    try {

        checksumIn = datanode.data.getMetaDataInputStream(block);

        long fileSize = checksumIn.getLength();

        if (fileSize >= 1L << 31 || fileSize <= 0) {
            throw new IOException("Unexpected size for checksumFile of block" + block);
        }

        byte[] buf = new byte[(int) fileSize];
        IOUtils.readFully(checksumIn, buf, 0, buf.length);

        //out = new DataOutputStream(
        //         NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
        out = new DataOutputStream(s.getOutputStream());

        out.writeByte(DataTransferProtocol.OP_STATUS_SUCCESS);
        out.writeInt(buf.length);
        out.write(buf);

        //last DATA_CHUNK
        out.writeInt(0);
    } finally {
        LOG.debug("Finalizing : readMetadata()");
        IOUtils.closeStream(out);
        IOUtils.closeStream(checksumIn);
    }
}

From source file:com.trigger_context.Main_Service.java

private void recvFile(DataInputStream in, String path) {
    Log.i("recvFile", "Start");
    // path should end with "/"
    String Filename = null;/*  ww  w  .j  a  v  a  2 s. c  om*/
    long size = 0;
    try {
        Filename = in.readUTF();
        size = in.readLong();
    } catch (IOException e1) {
        Log.i(Main_Service.LOG_TAG, "recvFile--error in readins file name and lngth");
    }

    OutputStream outfile = null;
    // noti("path of recv folder:",path);
    try {
        outfile = new FileOutputStream(path + Filename);
    } catch (FileNotFoundException e1) {
        Log.i(Main_Service.LOG_TAG, "recvFile--Error file not found exception");
    }

    byte[] buff = new byte[1024];
    int readbytes;
    try {
        while (size > 0 && (readbytes = in.read(buff, 0, (int) Math.min(buff.length, size))) != -1) {
            try {
                outfile.write(buff, 0, readbytes);
                size -= readbytes;
            } catch (IOException e) {
                Log.i(Main_Service.LOG_TAG, "recvFile--Error file write");
            }
        }
    } catch (IOException e) {
        Log.i(Main_Service.LOG_TAG, "recvFile--Error socket read");
        e.printStackTrace();
    }
    try {
        outfile.close();
    } catch (IOException e) {
        Log.i(Main_Service.LOG_TAG, "recvFile--Erro oufile close");
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Get block checksum (MD5 of CRC32).//from  w w w  . java2s. co m
 * @param in
 */
void getBlockChecksum(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : getBlockChecksum()");
    final Block block = new Block(in.readLong(), 0, in.readLong());

    DataOutputStream out = null;
    final MetaDataInputStream metadataIn = datanode.data.getMetaDataInputStream(block);
    final DataInputStream checksumIn = new DataInputStream(new BufferedInputStream(metadataIn, BUFFER_SIZE));

    try {
        //read metadata file
        final BlockMetadataHeader header = BlockMetadataHeader.readHeader(checksumIn);
        final DataChecksum checksum = header.getChecksum();
        final int bytesPerCRC = checksum.getBytesPerChecksum();
        final long crcPerBlock = (metadataIn.getLength() - BlockMetadataHeader.getHeaderSize())
                / checksum.getChecksumSize();

        //compute block checksum
        final MD5Hash md5 = MD5Hash.digest(checksumIn);

        if (LOG.isDebugEnabled()) {
            LOG.debug("block=" + block + ", bytesPerCRC=" + bytesPerCRC + ", crcPerBlock=" + crcPerBlock
                    + ", md5=" + md5);
        }

        //write reply
        //out = new DataOutputStream(
        //        NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
        out = new DataOutputStream(s.getOutputStream());
        out.writeShort(DataTransferProtocol.OP_STATUS_SUCCESS);
        out.writeInt(bytesPerCRC);
        out.writeLong(crcPerBlock);
        md5.write(out);
        out.flush();
    } finally {
        LOG.debug("Finalizing : getBlockChecksum()");
        IOUtils.closeStream(out);
        IOUtils.closeStream(checksumIn);
        IOUtils.closeStream(metadataIn);
    }
}

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageDecompressor.java

/**
 * Process image file.// w w w  . j  a  v  a  2s  .  c  om
 */
private void go() throws IOException {
    long start = System.currentTimeMillis();
    System.out.println("Decompressing image file: " + inputFile + " to " + outputFile);
    DataInputStream in = null;
    DataOutputStream out = null;

    try {
        // setup in
        PositionTrackingInputStream ptis = new PositionTrackingInputStream(
                new FileInputStream(new File(inputFile)));
        in = new DataInputStream(ptis);

        // read header information
        int imgVersion = in.readInt();
        if (!LayoutVersion.supports(Feature.FSIMAGE_COMPRESSION, imgVersion)) {
            System.out.println("Image is not compressed. No output will be produced.");
            return;
        }
        int namespaceId = in.readInt();
        long numFiles = in.readLong();
        long genstamp = in.readLong();

        long imgTxId = -1;
        if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
            imgTxId = in.readLong();
        }
        FSImageCompression compression = FSImageCompression.readCompressionHeader(new Configuration(), in);
        if (compression.isNoOpCompression()) {
            System.out.println("Image is not compressed. No output will be produced.");
            return;
        }
        in = BufferedByteInputStream.wrapInputStream(compression.unwrapInputStream(in),
                FSImage.LOAD_SAVE_BUFFER_SIZE, FSImage.LOAD_SAVE_CHUNK_SIZE);
        System.out.println("Starting decompression.");

        // setup output
        out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));

        // write back the uncompressed information
        out.writeInt(imgVersion);
        out.writeInt(namespaceId);
        out.writeLong(numFiles);
        out.writeLong(genstamp);
        if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
            out.writeLong(imgTxId);
        }
        // no compression
        out.writeBoolean(false);

        // copy the data
        long size = new File(inputFile).length();
        // read in 1MB chunks
        byte[] block = new byte[1024 * 1024];
        while (true) {
            int bytesRead = in.read(block);
            if (bytesRead <= 0)
                break;
            out.write(block, 0, bytesRead);
            printProgress(ptis.getPos(), size);
        }

        out.close();

        long stop = System.currentTimeMillis();
        System.out.println("Input file : " + inputFile + " size: " + size);
        System.out.println("Output file: " + outputFile + " size: " + new File(outputFile).length());
        System.out.println("Decompression completed in " + (stop - start) + " ms.");
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Read a block from the disk and then sends it to a destination.
 * /*  w ww . j a  v  a2  s.co m*/
 * @param in The stream to read from
 * @throws IOException
 */
private void copyBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : copyBlock()");
    // Read in the header
    long blockId = in.readLong(); // read block id
    Block block = new Block(blockId, 0, in.readLong());

    if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start
        LOG.info("Not able to copy block " + blockId + " to " + s.getRemoteSocketAddress()
                + " because threads quota is exceeded.");
        return;
    }

    BlockSender blockSender = null;
    DataOutputStream reply = null;
    boolean isOpSuccess = true;

    try {
        // check if the block exists or not
        blockSender = new BlockSender(block, 0, -1, false, false, false, datanode);

        // set up response stream
        //OutputStream baseStream = NetUtils.getOutputStream(s, datanode.socketWriteTimeout);
        OutputStream baseStream = s.getOutputStream();

        //reply = new DataOutputStream(new BufferedOutputStream(
        //    baseStream, SMALL_BUFFER_SIZE));
        LOG.debug("Replying to DFS client");
        reply = new DataOutputStream(new BufferedOutputStream(baseStream));

        // send block content to the target
        long read = blockSender.sendBlock(reply, baseStream, dataXceiverServer.balanceThrottler);

        datanode.myMetrics.bytesRead.inc((int) read);
        datanode.myMetrics.blocksRead.inc();

        LOG.info("Copied block " + block + " to " + s.getRemoteSocketAddress());
    } catch (IOException ioe) {
        isOpSuccess = false;
        throw ioe;
    } finally {
        dataXceiverServer.balanceThrottler.release();
        if (isOpSuccess) {
            try {
                // send one last byte to indicate that the resource is cleaned.
                reply.writeChar('d');
            } catch (IOException ignored) {
            }
        }
        LOG.debug("Finalizing : copyBlock()");
        IOUtils.closeStream(reply);
        IOUtils.closeStream(blockSender);
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Read a block from the disk./*from  w ww . j a va 2 s .com*/
 * @param in The stream to read from
 * @throws IOException
 */
private void readBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : readBlock()");
    //
    // Read in the header
    //
    long blockId = in.readLong();
    Block block = new Block(blockId, 0, in.readLong());

    long startOffset = in.readLong();
    long length = in.readLong();
    String clientName = Text.readString(in);
    // send the block
    // OutputStream baseStream = NetUtils.getOutputStream(s, datanode.socketWriteTimeout);
    OutputStream baseStream = s.getOutputStream();
    // DataOutputStream out = new DataOutputStream(
    //             new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE));
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(baseStream));

    BlockSender blockSender = null;
    final String clientTraceFmt = clientName.length() > 0 && ClientTraceLog.isInfoEnabled()
            ? String.format(DN_CLIENTTRACE_FORMAT, localAddress, remoteAddress, "%d", "HDFS_READ", clientName,
                    datanode.dnRegistration.getStorageID(), block)
            : datanode.dnRegistration + " Served block " + block + " to " + s.getInetAddress();
    try {
        try {
            blockSender = new BlockSender(block, startOffset, length, true, true, false, datanode,
                    clientTraceFmt);
        } catch (IOException e) {
            out.writeShort(DataTransferProtocol.OP_STATUS_ERROR);
            throw e;
        }

        out.writeShort(DataTransferProtocol.OP_STATUS_SUCCESS); // send op status
        long read = blockSender.sendBlock(out, baseStream, null); // send data

        if (blockSender.isBlockReadFully()) {
            // See if client verification succeeded. 
            // This is an optional response from client.
            try {
                if (in.readShort() == DataTransferProtocol.OP_STATUS_CHECKSUM_OK
                        && datanode.blockScanner != null) {
                    datanode.blockScanner.verifiedByClient(block);
                }
            } catch (IOException ignored) {
            }
        }

        datanode.myMetrics.bytesRead.inc((int) read);
        datanode.myMetrics.blocksRead.inc();
    } catch (SocketException ignored) {
        // Its ok for remote side to close the connection anytime.
        datanode.myMetrics.blocksRead.inc();
    } catch (IOException ioe) {
        /* What exactly should we do here?
         * Earlier version shutdown() datanode if there is disk error.
         */
        LOG.warn(datanode.dnRegistration + ":Got exception while serving " + block + " to " + s.getInetAddress()
                + ":\n" + StringUtils.stringifyException(ioe));
        throw ioe;
    } finally {
        LOG.debug("Finalizing : readBlock()");
        IOUtils.closeStream(out);
        IOUtils.closeStream(blockSender);
    }
}

From source file:com.joey.software.MoorFLSI.RepeatImageTextReader.java

public void loadData(File f, boolean clearData) throws IOException {
    BufferedInputStream inReader = new BufferedInputStream(new FileInputStream(f), 1000000);// RandomAccessFile in = new
    // RandomAccessFile(f, "r");
    DataInputStream in = new DataInputStream(inReader);
    int tot = in.readInt();
    wide = in.readInt();/* w w  w.  j  ava  2 s. c o m*/
    high = in.readInt();

    short[][][] holder = new short[tot][wide][high];

    byte[] inputHolder = new byte[high * 2];
    for (int i = 0; i < tot; i++) {
        Date d = new Date(in.readLong());
        for (int x = 0; x < wide; x++) {
            in.read(inputHolder);
            for (int y = 0; y < high; y++) {
                holder[i][wide - 1 - x][high - y - 1] = BinaryToolkit.readShort(inputHolder, y * 2);
            }
            // for (int y = 0; y < high; y++)
            // {
            // holder[i][x][y] = in.readShort();
            // }
        }
        addData(d, holder[i]);
    }
    ((SpinnerNumberModel) currentValue.getModel()).setMaximum(imageData.size() - 1);
    in.close();
    setCurrentImage(0);
    image.updateMaxMin();
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Receive a block and write it to disk, it then notifies the namenode to
 * remove the copy from the source./*  w  w  w.j a va 2 s. c  o  m*/
 * 
 * @param in The stream to read from
 * @throws IOException
 */
private void replaceBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : replaceBlock()");
    /* read header */
    long blockId = in.readLong();
    Block block = new Block(blockId, dataXceiverServer.estimateBlockSize, in.readLong()); // block id & generation stamp
    String sourceID = Text.readString(in); // read del hint
    DatanodeInfo proxySource = new DatanodeInfo(); // read proxy source
    proxySource.readFields(in);

    if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start
        LOG.warn("Not able to receive block " + blockId + " from " + s.getRemoteSocketAddress()
                + " because threads quota is exceeded.");
        sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR, datanode.socketWriteTimeout);
        return;
    }

    JxtaSocket proxySock = null;
    DataOutputStream proxyOut = null;
    short opStatus = DataTransferProtocol.OP_STATUS_SUCCESS;
    BlockReceiver blockReceiver = null;
    DataInputStream proxyReply = null;
    ReliableOutputStream baseStream = null;
    ReliableInputStream replyStream = null;

    try {
        // get the output stream to the proxy
        //InetSocketAddress proxyAddr = NetUtils.createSocketAddr(
        //    proxySource.getName());
        //proxySock = datanode.newSocket();
        proxySock = datanode.getDnPeer().getInfoSocket(proxySource.getPeerId().toString());

        // NetUtils.connect(proxySock, proxyAddr, datanode.socketTimeout);
        // proxySock.setSoTimeout(datanode.socketTimeout);

        /*OutputStream baseStream = NetUtils.getOutputStream(proxySock, 
            datanode.socketWriteTimeout);
        proxyOut = new DataOutputStream(
             new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE));
        */
        baseStream = (ReliableOutputStream) proxySock.getOutputStream();
        proxyOut = new DataOutputStream(new BufferedOutputStream(baseStream));

        /* send request to the proxy */
        proxyOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION); // transfer version
        proxyOut.writeByte(DataTransferProtocol.OP_COPY_BLOCK); // op code
        proxyOut.writeLong(block.getBlockId()); // block id
        proxyOut.writeLong(block.getGenerationStamp()); // block id
        proxyOut.flush();

        // receive the response from the proxy
        //proxyReply = new DataInputStream(new BufferedInputStream(
        //    NetUtils.getInputStream(proxySock), BUFFER_SIZE));
        replyStream = (ReliableInputStream) proxySock.getInputStream();
        proxyReply = new DataInputStream(new BufferedInputStream(replyStream));
        // open a block receiver and check if the block does not exist
        blockReceiver = new BlockReceiver(block, proxyReply, proxySock.getRemoteSocketAddress().toString(),
                proxySock.getLocalSocketAddress().toString(), false, "", null, datanode);

        // receive a block
        blockReceiver.receiveBlock(null, null, null, null, dataXceiverServer.balanceThrottler, -1);

        // notify name node
        datanode.notifyNamenodeReceivedBlock(block, sourceID);

        LOG.info("Moved block " + block + " from " + s.getRemoteSocketAddress());

    } catch (IOException ioe) {
        opStatus = DataTransferProtocol.OP_STATUS_ERROR;
        throw ioe;
    } finally {
        // receive the last byte that indicates the proxy released its thread resource
        if (opStatus == DataTransferProtocol.OP_STATUS_SUCCESS) {
            try {
                proxyReply.readChar();
            } catch (IOException ignored) {
            }
        }

        // now release the thread resource
        dataXceiverServer.balanceThrottler.release();

        // send response back
        try {
            sendResponse(s, opStatus, datanode.socketWriteTimeout);
        } catch (IOException ioe) {
            LOG.warn("Error writing reply back to " + s.getRemoteSocketAddress());
        }

        LOG.debug("Finalizing : replaceBlock()");
        LOG.debug("baseStream queue empty : " + baseStream.isQueueEmpty());
        IOUtils.closeStream(proxyOut);
        IOUtils.closeStream(blockReceiver);
        IOUtils.closeStream(proxyReply);
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java

public static long hashPublicKey(byte[] data) {
    try {/*from  w w  w. ja  v  a  2s . c o  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        ByteArrayInputStream bais = new ByteArrayInputStream(m.digest(data));
        DataInputStream dis = new DataInputStream(bais);
        return dis.readLong();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}