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:com.splout.db.benchmark.TCPTest.java

public static void tcpTest(String file, String table)
        throws UnknownHostException, IOException, InterruptedException, JSONSerDeException {
    final TCPServer server = new TCPServer(8888, file, table);

    Thread t = new Thread() {
        @Override//w  ww  .  j  a v  a 2 s .c o  m
        public void run() {
            server.serve();
        }
    };
    t.start();

    while (!server.isServing()) {
        Thread.sleep(100);
    }

    Socket clientSocket = new Socket("localhost", 8888);
    DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));

    try {
        do {
            // Read a record
            inFromServer.readUTF();
            inFromServer.readInt();
            inFromServer.readDouble();
            inFromServer.readUTF();
        } while (true);
    } catch (Throwable th) {
        th.printStackTrace();
    }

    clientSocket.close();
    server.stop();
    t.interrupt();
}

From source file:hudson.console.ConsoleNote.java

/**
 * Reads a note back from {@linkplain #encodeTo(OutputStream) its encoded form}.
 *
 * @param in/*from w  w w . j ava 2s  . c  o m*/
 *      Must point to the beginning of a preamble.
 *
 * @return null if the encoded form is malformed.
 */
public static ConsoleNote readFrom(DataInputStream in) throws IOException, ClassNotFoundException {
    try {
        byte[] preamble = new byte[PREAMBLE.length];
        in.readFully(preamble);
        if (!Arrays.equals(preamble, PREAMBLE))
            return null; // not a valid preamble

        DataInputStream decoded = new DataInputStream(new UnbufferedBase64InputStream(in));
        int sz = decoded.readInt();
        byte[] buf = new byte[sz];
        decoded.readFully(buf);

        byte[] postamble = new byte[POSTAMBLE.length];
        in.readFully(postamble);
        if (!Arrays.equals(postamble, POSTAMBLE))
            return null; // not a valid postamble

        ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(new ByteArrayInputStream(buf)),
                Jenkins.getInstance().pluginManager.uberClassLoader);
        try {
            return (ConsoleNote) ois.readObject();
        } finally {
            ois.close();
        }
    } catch (Error e) {
        // for example, bogus 'sz' can result in OutOfMemoryError.
        // package that up as IOException so that the caller won't fatally die.
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 *
 * Resolve DNS name into IP address/*from  w  w w  . j a  va 2s.c  o m*/
 *
 * @param host DNS name
 * @return IP address in integer format
 * @throws IOException
 */
public static int resolve(String host) throws IOException {
    Socket localSocket = new Socket(ADB_HOST, ADB_PORT);
    DataInputStream dis = new DataInputStream(localSocket.getInputStream());
    OutputStream os = localSocket.getOutputStream();
    int count_read = 0;

    if (localSocket == null || dis == null || os == null)
        return -1;
    String cmd = "dns:" + host;

    if (!sendAdbCmd(dis, os, cmd))
        return -1;

    count_read = dis.readInt();
    localSocket.close();
    return count_read;
}

From source file:PNGDecoder.java

protected static PNGData readChunks(DataInputStream in) throws IOException {
    PNGData chunks = new PNGData();

    boolean trucking = true;
    while (trucking) {
        try {//  w w w .  j a v a 2 s  .co m
            // Read the length.
            int length = in.readInt();
            if (length < 0)
                throw new IOException("Sorry, that file is too long.");
            // Read the type.
            byte[] typeBytes = new byte[4];
            in.readFully(typeBytes);
            // Read the data.
            byte[] data = new byte[length];
            in.readFully(data);
            // Read the CRC.
            long crc = in.readInt() & 0x00000000ffffffffL; // Make it
            // unsigned.
            if (verifyCRC(typeBytes, data, crc) == false)
                throw new IOException("That file appears to be corrupted.");

            PNGChunk chunk = new PNGChunk(typeBytes, data);
            chunks.add(chunk);
        } catch (EOFException eofe) {
            trucking = false;
        }
    }
    return chunks;
}

From source file:org.apache.jackrabbit.core.persistence.util.Serializer.java

/**
 * Deserializes a <code>NodeReferences</code> object from the given
 * binary <code>stream</code>.
 *
 * @param refs   object to deserialize/*w w  w  .  j  av  a 2s .  c  om*/
 * @param stream the stream where the object should be deserialized from
 * @throws Exception if an error occurs during the deserialization
 * @see #serialize(NodeReferences, OutputStream)
 */
public static void deserialize(NodeReferences refs, InputStream stream) throws Exception {
    DataInputStream in = new DataInputStream(stream);

    refs.clearAllReferences();

    // references
    int count = in.readInt(); // count
    for (int i = 0; i < count; i++) {
        refs.addReference(PropertyId.valueOf(in.readUTF())); // propertyId
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.X509ExtensionSet.java

private static Map<String, byte[]> loadExtensions(DataInputStream dis) throws IOException {
    Map<String, byte[]> extensions = new HashMap<String, byte[]>();

    int extensionCnt = dis.readInt();

    for (int i = 0; i < extensionCnt; i++) {
        int oidLen = dis.readInt();

        char[] oidChars = new char[oidLen];

        for (int j = 0; j < oidLen; j++) {
            oidChars[j] = dis.readChar();
        }//  w  w w. j  ava 2 s .  co  m

        String oid = new String(oidChars);

        int valueLen = dis.readInt();
        byte[] value = new byte[valueLen];

        dis.readFully(value);

        extensions.put(oid, value);
    }

    return extensions;
}

From source file:org.apache.jackrabbit.core.persistence.util.Serializer.java

/**
 * Deserializes a <code>PropertyState</code> object from the given binary
 * <code>stream</code>. Binary values are retrieved from the specified
 * <code>BLOBStore</code>.//from  ww w .  j  a  va  2  s  .  co  m
 *
 * @param state     <code>state</code> to deserialize
 * @param stream    the stream where the <code>state</code> should be
 *                  deserialized from
 * @param blobStore handler for BLOB data
 * @throws Exception if an error occurs during the deserialization
 * @see #serialize(PropertyState, OutputStream, BLOBStore)
 */
public static void deserialize(PropertyState state, InputStream stream, BLOBStore blobStore) throws Exception {
    DataInputStream in = new DataInputStream(stream);

    // type
    int type = in.readInt();
    state.setType(type);
    // multiValued
    boolean multiValued = in.readBoolean();
    state.setMultiValued(multiValued);
    // definitionId
    in.readUTF();
    // modCount
    short modCount = in.readShort();
    state.setModCount(modCount);
    // values
    int count = in.readInt(); // count
    InternalValue[] values = new InternalValue[count];
    for (int i = 0; i < count; i++) {
        InternalValue val;
        if (type == PropertyType.BINARY) {
            String s = in.readUTF(); // value (i.e. blobId)
            // special handling required for binary value:
            // the value stores the id of the BLOB data
            // in the BLOB store
            if (blobStore instanceof ResourceBasedBLOBStore) {
                // optimization: if the BLOB store is resource-based
                // retrieve the resource directly rather than having
                // to read the BLOB from an input stream
                FileSystemResource fsRes = ((ResourceBasedBLOBStore) blobStore).getResource(s);
                val = InternalValue.create(fsRes);
            } else {
                InputStream is = blobStore.get(s);
                try {
                    val = InternalValue.create(is);
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        } else {
            /**
             * because writeUTF(String) has a size limit of 65k,
             * Strings are serialized as <length><byte[]>
             */
            //s = in.readUTF();   // value
            int len = in.readInt(); // lenght of byte[]
            byte[] bytes = new byte[len];
            in.readFully(bytes); // byte[]
            String s = new String(bytes, ENCODING);
            val = InternalValue.valueOf(s, type);
        }
        values[i] = val;
    }
    state.setValues(values);
}

From source file:uk.ac.ebi.mdk.io.ReactionMatrixIO.java

public static StoichiometricMatrix readCompressedBasicStoichiometricMatrix(InputStream stream,
        StoichiometricMatrix s) throws IOException {

    DataInputStream in = new DataInputStream(stream);

    int n = in.readInt();
    int m = in.readInt();

    s.ensure(n, m);/* w w w  . jav  a 2 s . co m*/

    for (int j = 0; j < m; j++) {
        s.setReaction(j, in.readUTF());
    }

    for (int i = 0; i < n; i++) {
        s.setMolecule(i, in.readUTF());
    }

    boolean convert = in.readBoolean();
    int size = in.readInt();

    while (--size >= 0) {

        int i = in.readInt();
        int j = in.readInt();
        Object value = convert ? in.readInt() : in.readDouble();
        Double dValue = value instanceof Double ? (Double) value : ((Integer) value).doubleValue();
        s.setValue(i, j, dValue);
    }

    in.close();

    return s;

}

From source file:org.apache.cassandra.service.MigrationManager.java

/**
 * Deserialize migration message considering data compatibility starting from version 1.1
 *
 * @param data The data of the message from coordinator which hold schema mutations to apply
 * @param version The version of the message
 *
 * @return The collection of the row mutations to apply on the node (aka schema)
 *
 * @throws IOException if message is of incompatible version or data is corrupted
 *//*from  ww w  .  j  a va2 s.  c  o m*/
public static Collection<RowMutation> deserializeMigrationMessage(byte[] data, int version) throws IOException {
    Collection<RowMutation> schema = new ArrayList<RowMutation>();
    DataInputStream in = new DataInputStream(new FastByteArrayInputStream(data));

    int count = in.readInt();

    for (int i = 0; i < count; i++)
        schema.add(RowMutation.serializer().deserializeFixingTimestamps(in, version));

    return schema;
}

From source file:PolyGlot.IOHandler.java

/**
 * Tests whether or not a file is a zip archive
 *
 * @param _fileName the file to test//from w  w  w  . j  a  va 2  s  . co  m
 * @return true is passed file is a zip archive
 * @throws java.io.FileNotFoundException
 */
public static boolean isFileZipArchive(String _fileName) throws FileNotFoundException, IOException {
    File file = new File(_fileName);

    // ignore directories and files too small to possibly be archives
    if (file.isDirectory() || file.length() < 4) {
        return false;
    }

    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
    int test = in.readInt();
    in.close();
    return test == 0x504b0304;
}