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:Main.java

public static int[] readInts(String file) throws IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024));
    try {//from   w  w  w .j  ava 2 s . c  o m
        int len = in.readInt();
        int[] ints = new int[len];
        for (int i = 0; i < len; i++) {
            ints[i] = in.readInt();
        }
        return ints;
    } finally {
        in.close();
    }
}

From source file:Main.java

public static float[] readFloats(String file) throws IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024));
    try {/*from  w  w  w . j a v a  2 s .c  o  m*/
        int len = in.readInt();
        float[] floats = new float[len];
        for (int i = 0; i < len; i++) {
            floats[i] = in.readFloat();
        }
        return floats;
    } finally {
        in.close();
    }
}

From source file:org.apache.reef.io.data.loading.impl.DistributedDataSetPartitionSerializer.java

public static DistributedDataSetPartition deserialize(final String serializedPartition) {
    try (ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(serializedPartition))) {
        final DataInputStream dais = new DataInputStream(bais);
        return new DistributedDataSetPartition(dais.readUTF(), dais.readUTF(), dais.readInt());
    } catch (final IOException e) {
        throw new RuntimeException("Unable to de-serialize distributed data partition", e);
    }// w  w w  .ja v a2  s .  co m
}

From source file:com.microsoft.tfs.client.eclipse.resourcedata.ResourceData.java

/**
 * Deserializes a {@link ResourceData} object from a byte array.
 *
 * @param value//w  w  w  .  ja  v a  2  s  . c o  m
 *        the byte array (not null)
 * @return the deserialized {@link ResourceData} or <code>null</code> if
 *         there was a problem deserializing
 */
public static ResourceData fromByteArray(final byte[] value) {
    Check.notNull(value, "value"); //$NON-NLS-1$

    try {
        final ByteArrayInputStream is = new ByteArrayInputStream(value);
        final DataInputStream dis = new DataInputStream(is);

        return new ResourceData(dis.readUTF(), dis.readInt());
    } catch (final IOException e) {
        log.error("Error deserializing", e); //$NON-NLS-1$
        return null;
    }
}

From source file:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

public static int getSizeOfTimeBytes(final byte[] timesAndSamples) {
    final DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(timesAndSamples));
    try {//from  w  w w .j  av a2  s.co  m
        return inputStream.readInt();
    } catch (IOException e) {
        throw new IllegalStateException(String.format(
                "Exception reading timeByteCount in TimelineChunkMapper.map() for timesAndSamples %s",
                Hex.encodeHex(timesAndSamples)), e);
    }
}

From source file:org.apache.pulsar.io.file.utils.ZipFiles.java

/**
 * Returns true if the given file is a gzip file.
 *//*from  w w  w .  jav a2s  . c o m*/
@SuppressWarnings("deprecation")
public static boolean isZip(File f) {

    InputStream input = null;
    try {
        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
        int test = in.readInt();
        in.close();
        return test == 0x504b0304;
    } catch (final Exception e) {
        return false;
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:org.mrgeo.utils.StringUtils.java

public static String read(DataInputStream stream) throws IOException {
    int len = stream.readInt();
    if (len == -1) {
        return null;
    } else {/*from w  ww.  j av  a 2s .c o m*/
        byte[] data = new byte[len];
        stream.readFully(data);

        return new String(data, "UTF-8");
    }
}

From source file:PipedBytes.java

public static void readStuff(InputStream rawIn) {
    try {// w w w .j ava2 s . c  om
        DataInputStream in = new DataInputStream(new BufferedInputStream(rawIn));

        boolean eof = false;
        while (!eof) {
            try {
                int i = in.readInt();
                System.out.println("just read: " + i);
            } catch (EOFException eofx) {
                eof = true;
            }
        }

        System.out.println("Read all data from the pipe");
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static Object[] readSettings(String file) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from ww  w  . ja v  a  2  s. com*/
        Object[] res = new Object[in.readInt()];
        for (int i = 0; i < res.length; i++) {
            char cl = in.readChar();
            switch (cl) {
            case 'S':
                res[i] = in.readUTF();
                break;
            case 'F':
                res[i] = in.readFloat();
                break;
            case 'D':
                res[i] = in.readDouble();
                break;
            case 'I':
                res[i] = in.readInt();
                break;
            case 'L':
                res[i] = in.readLong();
                break;
            case 'B':
                res[i] = in.readBoolean();
                break;
            case 'Y':
                res[i] = in.readByte();
                break;
            default:
                throw new IllegalStateException("cannot read type " + cl + " from " + file);
            }
        }
        return res;
    } finally {
        in.close();
    }
}

From source file:org.jboss.capedwarf.tools.BulkLoader.java

private static byte[] readArray(DataInputStream in) {
    try {/*from ww  w .j av  a  2 s  .  co m*/
        int arraySize;
        try {
            arraySize = in.readInt();
        } catch (EOFException e) {
            return null;
        }
        byte[] array = new byte[arraySize];
        in.readFully(array);
        return array;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}