Example usage for java.io DataInputStream readInt

List of usage examples for java.io DataInputStream readInt

Introduction

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

Prototype

public final int readInt() throws IOException 

Source Link

Document

See the general contract of the readInt method of DataInput.

Usage

From source file:MersenneTwisterFast.java

/** Reads the entire state of the MersenneTwister RNG from the stream
 *  @param stream input stream//from   w ww. j  a v a2  s .  com
 *  @throws IOException exception from stream reading
 */
public void readState(DataInputStream stream) throws IOException {
    int len = mt.length;
    for (int x = 0; x < len; x++)
        mt[x] = stream.readInt();

    len = mag01.length;
    for (int x = 0; x < len; x++)
        mag01[x] = stream.readInt();

    mti = stream.readInt();
    __nextNextGaussian = stream.readDouble();
    __haveNextNextGaussian = stream.readBoolean();
}

From source file:org.openxdata.server.FormsServer.java

/**
 * Called when a new connection has been received. Failures are not handled
 * in this class as different servers (BT,SMS, etc) may want to handle them
 * differently.//w ww  .  java 2  s .  c o  m
 * 
 * @param dis - the stream to read from.
 * @param dos - the stream to write to.
 */
public void processConnection(InputStream disParam, OutputStream dosParam) {

    ZOutputStream gzip = new ZOutputStream(dosParam, JZlib.Z_BEST_COMPRESSION);
    DataOutputStream zdos = new DataOutputStream(gzip);

    byte responseStatus = ResponseStatus.STATUS_ERROR;

    try {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataInputStream dis = new DataInputStream(disParam);

            String name = dis.readUTF();
            String password = dis.readUTF();
            String serializer = dis.readUTF();
            String locale = dis.readUTF();

            byte action = dis.readByte();

            User user = authenticationService.authenticate(name, password);
            if (user == null)
                responseStatus = ResponseStatus.STATUS_ACCESS_DENIED;
            else {
                DataOutputStream dosTemp = new DataOutputStream(baos);

                if (action == ACTION_DOWNLOAD_FORMS)
                    formDownloadService.downloadForms(dosTemp, serializer, locale);
                else if (action == ACTION_UPLOAD_DATA)
                    submitXforms(dis, dosTemp, serializer);
                else if (action == ACTION_DOWNLOAD_USERS)
                    formDownloadService.downloadUsers(dosTemp, serializer);
                else if (action == ACTION_DOWNLOAD_USERS_AND_FORMS)
                    downloadUsersAndForms(dis.readInt(), dosTemp, serializer, locale);
                else if (action == ACTION_DOWNLOAD_STUDY_LIST)
                    formDownloadService.downloadStudies(dosTemp, serializer, locale);
                else if (action == ACTION_DOWNLOAD_LANGUAGES)
                    formDownloadService.downloadLocales(dis, dosTemp, serializer);
                else if (action == ACTION_DOWNLOAD_MENU_TEXT)
                    formDownloadService.downloadMenuText(dis, dosTemp, serializer, locale);
                else if (action == ACTION_DOWNLOAD_STUDY_FORMS)
                    formDownloadService.downloadForms(dis.readInt(), zdos, serializer, locale);
                else if (action == ACTION_DOWNLOAD_USERS_AND_ALL_FORMS)
                    downloadUsersAndAllForms(dosTemp, serializer, locale);

                responseStatus = ResponseStatus.STATUS_SUCCESS;
            }

            zdos.writeByte(responseStatus);

            if (responseStatus == ResponseStatus.STATUS_SUCCESS) {
                zdos.write(baos.toByteArray());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            zdos.writeByte(responseStatus);
        } finally {
            zdos.flush();
            gzip.finish();
        }
    } catch (IOException e) {
        // this is for exceptions occurring in the catch or finally clauses.
        log.error(e.getMessage(), e);
    }
}

From source file:org.carbondata.core.util.CarbonUtil.java

/**
 * Read level metadata file and return cardinality
 *
 * @param levelPath/* w  w w.  j a v a2s.c  om*/
 * @return
 * @throws CarbonUtilException
 */
public static int[] getCardinalityFromLevelMetadataFile(String levelPath) throws CarbonUtilException {
    DataInputStream dataInputStream = null;
    int[] cardinality = null;

    try {
        if (FileFactory.isFileExist(levelPath, FileFactory.getFileType(levelPath))) {
            dataInputStream = FileFactory.getDataInputStream(levelPath, FileFactory.getFileType(levelPath));

            cardinality = new int[dataInputStream.readInt()];

            for (int i = 0; i < cardinality.length; i++) {
                cardinality[i] = dataInputStream.readInt();
            }
        }
    } catch (FileNotFoundException e) {
        throw new CarbonUtilException("Problem while getting the file", e);
    } catch (IOException e) {
        throw new CarbonUtilException("Problem while reading the file", e);
    } finally {
        closeStreams(dataInputStream);
    }

    return cardinality;
}

From source file:IndexService.IndexServer.java

public boolean setindexstatus(String indexloc, int status) {
    if (!testmode) {
        Path indexpath = new Path(indexloc);
        try {/*from  w ww  .  ja va2  s  .  c o m*/
            String dbname = indexpath.getParent().getParent().getName();
            if (dbname.endsWith(".db")) {
                dbname = dbname.substring(0, dbname.lastIndexOf(".db"));
            }
            return db.setIndexStatus(dbname, indexpath.getParent().getName(), indexpath.getName(), status);
        } catch (HiveException e1) {
            e1.printStackTrace();
            return false;
        }
    } else {
        try {
            ArrayList<IndexItemStatus> statuss = new ArrayList<IndexItemStatus>();
            File file = new File("indexconf");
            boolean exist = false;
            if (file.exists()) {
                DataInputStream dis = new DataInputStream(new FileInputStream(file));
                int num = dis.readInt();
                for (int i = 0; i < num; i++) {
                    IndexItemStatus itemstatus = new IndexItemStatus();
                    itemstatus.read(dis);
                    if (itemstatus.indexlocation.equals(indexloc)) {
                        itemstatus.status = status;
                        exist = true;
                    }
                    statuss.add(itemstatus);
                }
                dis.close();

                DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
                dos.writeInt(statuss.size());
                for (IndexItemStatus indexItemStatus : statuss) {
                    indexItemStatus.write(dos);
                }
                dos.close();
            }
            if (exist)
                return true;
            return false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

From source file:org.hyperic.hq.bizapp.agent.client.AgentClient.java

private void verifyAgentRunning(ServerSocket startupSock) throws AgentInvokeException {
    try {/* w  w w  .java2s.  co  m*/

        Socket conn = startupSock.accept();

        DataInputStream dIs = new DataInputStream(conn.getInputStream());

        if (dIs.readInt() != 1) {
            throw new AgentInvokeException("Agent reported an error " + "while starting up");
        }

    } catch (InterruptedIOException exc) {
        throw new AgentInvokeException("Timed out waiting for Agent " + "to report startup success");
    } catch (IOException exc) {
        throw new AgentInvokeException("Agent failure while starting");
    } finally {
        try {
            startupSock.close();
        } catch (IOException exc) {
        }
    }

    try {
        this.agtCommands.ping();
    } catch (Exception exc) {
        throw new AgentInvokeException("Unable to ping agent: " + exc.getMessage());
    }
}

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

/**
 * Write a block to disk./*  w  w w  . ja va 2  s .c o m*/
 * 
 * @param in The stream to read from
 * @throws IOException
 */
private void writeBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : writeBlock()");
    DatanodeInfo srcDataNode = null;
    LOG.debug("writeBlock receive buf size " + s.getReceiveBufferSize() + " tcp no delay " + s.getTcpNoDelay());
    //
    // Read in the header
    //
    Block block = new Block(in.readLong(), dataXceiverServer.estimateBlockSize, in.readLong());
    LOG.info("Receiving block " + block + " src: " + remoteAddress + " dest: " + localAddress);

    int pipelineSize = in.readInt(); // num of datanodes in entire pipeline
    boolean isRecovery = in.readBoolean(); // is this part of recovery?
    String client = Text.readString(in); // working on behalf of this client

    boolean hasSrcDataNode = in.readBoolean(); // is src node info present
    if (hasSrcDataNode) {
        srcDataNode = new DatanodeInfo();
        srcDataNode.readFields(in);
    }

    int numTargets = in.readInt();
    if (numTargets < 0) {
        throw new IOException("Mislabelled incoming datastream.");
    }

    DatanodeInfo targets[] = new DatanodeInfo[numTargets];
    for (int i = 0; i < targets.length; i++) {
        DatanodeInfo tmp = new DatanodeInfo();
        tmp.readFields(in);
        targets[i] = tmp;
    }

    DataOutputStream mirrorOut = null; // stream to next target
    DataInputStream mirrorIn = null; // reply from next target
    DataOutputStream replyOut = null; // stream to prev target
    JxtaSocket mirrorSock = null; // socket to next target
    BlockReceiver blockReceiver = null; // responsible for data handling
    String mirrorNode = null; // the name:port of next target
    String firstBadLink = ""; // first datanode that failed in connection setup

    try {
        // open a block receiver and check if the block does not exist
        /*blockReceiver = new BlockReceiver(block, in, 
            s.getRemoteSocketAddress().toString(),
            s.getLocalSocketAddress().toString(),
            isRecovery, client, srcDataNode, datanode);*/
        blockReceiver = new BlockReceiver(block, in,
                ((JxtaSocketAddress) s.getRemoteSocketAddress()).getPeerId().toString(),
                ((JxtaSocketAddress) s.getLocalSocketAddress()).getPeerId().toString(), isRecovery, client,
                srcDataNode, datanode);

        // get a connection back to the previous target
        //replyOut = new DataOutputStream(
        //     NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
        ReliableOutputStream replyOutRos = (ReliableOutputStream) s.getOutputStream();
        replyOut = new DataOutputStream(replyOutRos);

        //
        // Open network conn to backup machine, if 
        // appropriate
        //
        if (targets.length > 0) {
            // JxtaSocketAddress mirrorTarget = null;
            // Connect to backup machine
            mirrorNode = targets[0].getPeerId();
            // mirrorTarget = NetUtils.createSocketAddr(mirrorNode);
            // mirrorSock = datanode.newSocket();

            try {
                //int timeoutValue = numTargets * datanode.socketTimeout;
                //int writeTimeout = datanode.socketWriteTimeout + 
                //                   (HdfsConstants.WRITE_TIMEOUT_EXTENSION * numTargets);
                // NetUtils.connect(mirrorSock, mirrorTarget, timeoutValue);
                mirrorSock = datanode.getDnPeer().getInfoSocket(mirrorNode.toString());
                if (mirrorSock == null)
                    throw new IOException("Failed to get a mirror socket");
                //mirrorSock.setSoTimeout(timeoutValue);
                //mirrorSock.setTcpNoDelay(true);
                //mirrorSock.setSoTimeout(Integer.parseInt(datanode.getConf().get("hadoop.p2p.info.timeout")));
                //mirrorSock.setSendBufferSize(DEFAULT_DATA_SOCKET_SIZE);
                /*mirrorOut = new DataOutputStream(
                   new BufferedOutputStream(
                         NetUtils.getOutputStream(mirrorSock, writeTimeout),
                 SMALL_BUFFER_SIZE));
                mirrorIn = new DataInputStream(NetUtils.getInputStream(mirrorSock));
                */
                mirrorOut = new DataOutputStream((ReliableOutputStream) mirrorSock.getOutputStream());
                mirrorIn = new DataInputStream((ReliableInputStream) mirrorSock.getInputStream());

                // Write header: Copied from DFSClient.java!
                mirrorOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION);
                mirrorOut.write(DataTransferProtocol.OP_WRITE_BLOCK);
                mirrorOut.writeLong(block.getBlockId());
                mirrorOut.writeLong(block.getGenerationStamp());
                mirrorOut.writeInt(pipelineSize);
                mirrorOut.writeBoolean(isRecovery);
                Text.writeString(mirrorOut, client);
                mirrorOut.writeBoolean(hasSrcDataNode);

                if (hasSrcDataNode) { // pass src node information
                    srcDataNode.write(mirrorOut);
                }

                mirrorOut.writeInt(targets.length - 1);
                for (int i = 1; i < targets.length; i++) {
                    targets[i].write(mirrorOut);
                }

                blockReceiver.writeChecksumHeader(mirrorOut);
                mirrorOut.flush();

                // read connect ack (only for clients, not for replication req)
                if (client.length() != 0) {
                    firstBadLink = Text.readString(mirrorIn);
                    if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                        LOG.info("Datanode " + targets.length + " got response for connect ack "
                                + " from downstream datanode with firstbadlink as " + firstBadLink);
                    }
                }

            } catch (SocketTimeoutException ste) {
                LOG.debug("Time out while receiving data on DataXceiver");
                LOG.debug(ste);
                ste.printStackTrace();
            } catch (IOException e) {
                LOG.debug("IOException occurred : " + e.getMessage());
                if (client.length() != 0) {
                    Text.writeString(replyOut, mirrorNode);
                    replyOut.flush();
                }
                IOUtils.closeStream(mirrorOut);
                mirrorOut = null;
                IOUtils.closeStream(mirrorIn);
                mirrorIn = null;
                if (mirrorSock != null) {
                    IOUtils.closeSocket(mirrorSock);
                    mirrorSock = null;
                }
                if (client.length() > 0) {
                    throw e;
                } else {
                    LOG.info(datanode.dnRegistration + ":Exception transfering block " + block + " to mirror "
                            + mirrorNode + ". continuing without the mirror.\n"
                            + StringUtils.stringifyException(e));
                }
            }
        }

        // send connect ack back to source (only for clients)
        if (client.length() != 0) {
            if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                LOG.info("Datanode " + targets.length + " forwarding connect ack to upstream firstbadlink is "
                        + firstBadLink);
            }
            Text.writeString(replyOut, firstBadLink);
            replyOut.flush();
        }

        // receive the block and mirror to the next target
        String mirrorAddr = (mirrorSock == null) ? null : mirrorNode;
        blockReceiver.receiveBlock(mirrorOut, mirrorIn, replyOut, mirrorAddr, null, targets.length);

        // if this write is for a replication request (and not
        // from a client), then confirm block. For client-writes,
        // the block is finalized in the PacketResponder.
        if (client.length() == 0) {
            datanode.notifyNamenodeReceivedBlock(block, DataNode.EMPTY_DEL_HINT);
            LOG.info("Received block " + block + " src: " + remoteAddress + " dest: " + localAddress
                    + " of size " + block.getNumBytes());
        }

        if (datanode.blockScanner != null) {
            datanode.blockScanner.addBlock(block);
        }

    } catch (IOException ioe) {
        LOG.info("writeBlock " + block + " received exception " + ioe);
        throw ioe;
    } catch (Exception e) {
        LOG.warn("Exception occurred in writting block : " + e.getMessage());
    } finally {
        // close all opened streams

        LOG.debug("Finalizing : writeBlock()");
        IOUtils.closeStream(mirrorOut);
        IOUtils.closeStream(mirrorIn);
        IOUtils.closeStream(replyOut);
        IOUtils.closeSocket(mirrorSock);
        IOUtils.closeStream(blockReceiver);
    }
}

From source file:org.ejbca.core.protocol.cmp.CmpTestCase.java

/**
 * /*from  w ww .  j  a v a2s  . com*/
 * @param message
 * @param type set to 5 when sending a PKI request, 3 when sending a PKIConf
 * @return
 * @throws IOException
 * @throws NoSuchProviderException
 */
protected byte[] sendCmpTcp(byte[] message, int type) throws IOException, NoSuchProviderException {
    final String host = getProperty("tcpCmpProxyIP", this.CMP_HOST);
    final int port = getProperty("tcpCmpProxyPort", PORT_NUMBER);
    try {
        final Socket socket = new Socket(host, port);

        final byte[] msg = createTcpMessage(message);
        try {
            final BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
            os.write(msg);
            os.flush();

            DataInputStream dis = new DataInputStream(socket.getInputStream());

            // Read the length, 32 bits
            final int len = dis.readInt();
            log.info("Got a message claiming to be of length: " + len);
            // Read the version, 8 bits. Version should be 10 (protocol draft nr
            // 5)
            final int ver = dis.readByte();
            log.info("Got a message with version: " + ver);
            assertEquals(ver, 10);

            // Read flags, 8 bits for version 10
            final byte flags = dis.readByte();
            log.info("Got a message with flags (1 means close): " + flags);
            // Check if the client wants us to close the connection (LSB is 1 in
            // that case according to spec)

            // Read message type, 8 bits
            final int msgType = dis.readByte();
            log.info("Got a message of type: " + msgType);
            assertEquals(msgType, type);

            // Read message
            final ByteArrayOutputStream baos = new ByteArrayOutputStream(3072);
            while (dis.available() > 0) {
                baos.write(dis.read());
            }

            log.info("Read " + baos.size() + " bytes");
            final byte[] respBytes = baos.toByteArray();
            assertNotNull(respBytes);
            assertTrue(respBytes.length > 0);
            return respBytes;
        } finally {
            socket.close();
        }
    } catch (ConnectException e) {
        assertTrue("This test requires a CMP TCP listener to be configured on " + host + ":" + port
                + ". Edit conf/cmptcp.properties and redeploy.", false);
    } catch (EOFException e) {
        assertTrue("Response was malformed.", false);
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
    return null;
}

From source file:org.apache.giraph.graph.BspServiceWorker.java

@Override
public void loadCheckpoint(long superstep) {
    // Algorithm:
    // Examine all the partition owners and load the ones
    // that match my hostname and id from the master designated checkpoint
    // prefixes./* www  .j  a va  2 s  .  c  om*/
    long startPos = 0;
    int loadedPartitions = 0;
    for (PartitionOwner partitionOwner : workerGraphPartitioner.getPartitionOwners()) {
        if (partitionOwner.getWorkerInfo().equals(getWorkerInfo())) {
            String metadataFile = partitionOwner.getCheckpointFilesPrefix() + CHECKPOINT_METADATA_POSTFIX;
            String partitionsFile = partitionOwner.getCheckpointFilesPrefix() + CHECKPOINT_VERTICES_POSTFIX;
            try {
                int partitionId = -1;
                DataInputStream metadataStream = getFs().open(new Path(metadataFile));
                int partitions = metadataStream.readInt();
                for (int i = 0; i < partitions; ++i) {
                    startPos = metadataStream.readLong();
                    partitionId = metadataStream.readInt();
                    if (partitionId == partitionOwner.getPartitionId()) {
                        break;
                    }
                }
                if (partitionId != partitionOwner.getPartitionId()) {
                    throw new IllegalStateException("loadCheckpoint: " + partitionOwner + " not found!");
                }
                metadataStream.close();
                Partition<I, V, E, M> partition = new Partition<I, V, E, M>(getConfiguration(), partitionId);
                DataInputStream partitionsStream = getFs().open(new Path(partitionsFile));
                if (partitionsStream.skip(startPos) != startPos) {
                    throw new IllegalStateException(
                            "loadCheckpoint: Failed to skip " + startPos + " on " + partitionsFile);
                }
                partition.readFields(partitionsStream);
                partitionsStream.close();
                if (LOG.isInfoEnabled()) {
                    LOG.info("loadCheckpoint: Loaded partition " + partition);
                }
                if (getPartitionMap().put(partitionId, partition) != null) {
                    throw new IllegalStateException(
                            "loadCheckpoint: Already has partition owner " + partitionOwner);
                }
                ++loadedPartitions;
            } catch (IOException e) {
                throw new RuntimeException("loadCheckpoing: Failed to get partition owner " + partitionOwner,
                        e);
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("loadCheckpoint: Loaded " + loadedPartitions + " partitions of out "
                + workerGraphPartitioner.getPartitionOwners().size() + " total.");
    }
    // Communication service needs to setup the connections prior to
    // processing vertices
    commService.setup();
}

From source file:org.nuxeo.ecm.core.blob.binary.AESBinaryManager.java

/**
 * Decrypts the given input stream into the given output stream.
 *//*w w  w  .j  av  a 2s.co  m*/
protected void decrypt(InputStream in, OutputStream out) throws IOException {
    byte[] magic = new byte[FILE_MAGIC.length];
    IOUtils.read(in, magic);
    if (!Arrays.equals(magic, FILE_MAGIC)) {
        throw new IOException("Invalid file (bad magic)");
    }
    DataInputStream data = new DataInputStream(in);
    byte magicvers = data.readByte();
    if (magicvers != FILE_VERSION_1) {
        throw new IOException("Invalid file (bad version)");
    }

    byte usepb = data.readByte();
    if (usepb == USE_PBKDF2) {
        if (!usePBKDF2) {
            throw new NuxeoException("File requires PBKDF2 password");
        }
    } else if (usepb == USE_KEYSTORE) {
        if (usePBKDF2) {
            throw new NuxeoException("File requires keystore");
        }
    } else {
        throw new IOException("Invalid file (bad use)");
    }

    try {
        // secret key
        Key secret;
        if (usePBKDF2) {
            // read salt first
            int saltLen = data.readInt();
            if (saltLen <= 0 || saltLen > MAX_SALT_LEN) {
                throw new NuxeoException("Invalid salt length: " + saltLen);
            }
            byte[] salt = new byte[saltLen];
            data.read(salt, 0, saltLen);
            secret = generateSecretKey(salt);
        } else {
            secret = getSecretKey();
        }

        // read IV
        int ivLen = data.readInt();
        if (ivLen <= 0 || ivLen > MAX_IV_LEN) {
            throw new NuxeoException("Invalid IV length: " + ivLen);
        }
        byte[] iv = new byte[ivLen];
        data.read(iv, 0, ivLen);

        // cipher
        Cipher cipher;
        cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));

        // read the encrypted data
        try (InputStream cipherIn = new CipherInputStream(in, cipher)) {
            IOUtils.copy(cipherIn, out);
        } catch (IOException e) {
            Throwable cause = e.getCause();
            if (cause != null && cause instanceof BadPaddingException) {
                throw new NuxeoException(cause.getMessage(), e);
            }
        }
    } catch (GeneralSecurityException e) {
        throw new NuxeoException(e);
    }
}

From source file:org.neo4j.ogm.metadata.ClassInfo.java

public ClassInfo(InputStream inputStream) throws IOException {

    DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream, 1024));

    // Magic//from  w ww.ja  v  a 2 s  .  c o  m
    if (dataInputStream.readInt() != 0xCAFEBABE) {
        return;
    }

    dataInputStream.readUnsignedShort(); //minor version
    dataInputStream.readUnsignedShort(); // major version

    ConstantPool constantPool = new ConstantPool(dataInputStream);

    // Access flags
    int flags = dataInputStream.readUnsignedShort();

    isInterface = (flags & 0x0200) != 0;
    isAbstract = (flags & 0x0400) != 0;
    isEnum = (flags & 0x4000) != 0;

    className = constantPool.lookup(dataInputStream.readUnsignedShort()).replace('/', '.');
    String sce = constantPool.lookup(dataInputStream.readUnsignedShort());
    if (sce != null) {
        directSuperclassName = sce.replace('/', '.');
    }
    interfacesInfo = new InterfacesInfo(dataInputStream, constantPool);
    fieldsInfo = new FieldsInfo(dataInputStream, constantPool);
    methodsInfo = new MethodsInfo(dataInputStream, constantPool);
    annotationsInfo = new AnnotationsInfo(dataInputStream, constantPool);
    new ClassValidator(this).validate();
}