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:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

public static Object deserializeObject(InputStream in) throws IOException {
    if (in == null) {
        return null;
    }//from w w w .j  a  v a2 s. c  o m
    if (in.available() == 0) {
        throw new EOFException();
    }
    DataInputStream dataIn = new DataInputStream(in);
    int size = dataIn.readInt();
    byte[] buff = new byte[size];
    dataIn.readFully(buff);
    Kryo kryo = kryoTL.get();
    try (Input input = new Input(buff)) {
        return kryo.readClassAndObject(input);
    }
}

From source file:com.momock.http.HttpSession.java

public static DownloadInfo getDownloadInfo(File file) {
    if (file.exists()) {
        return new DownloadInfo(file.length(), file.length());
    }//from w  ww .  j  ava2  s  . c  o m
    DownloadInfo di = null;
    File fileData = new File(file.getPath() + ".data");
    File fileInfo = new File(file.getPath() + ".info");
    if (fileData.exists() && fileInfo.exists()) {
        long downloadedLength = fileData.length();
        long contentLength = -1;
        DataInputStream din;
        if (fileInfo.length() == 0)
            return new DownloadInfo(0, 0);
        try {
            din = new DataInputStream(new FileInputStream(fileInfo));

            int headerCount = din.readInt();
            for (int i = 0; i < headerCount; i++) {
                String key = din.readUTF();
                int count = din.readInt();
                List<String> vals = new ArrayList<String>();
                for (int j = 0; j < count; j++) {
                    String val = din.readUTF();
                    vals.add(val);
                }
                if ("Content-Length".equals(key)) {
                    if (contentLength == -1)
                        contentLength = Convert.toInteger(vals.get(0));
                } else if ("Content-Range".equals(key)) {
                    int pos = vals.get(0).indexOf('/');
                    contentLength = Convert.toInteger(vals.get(0).substring(pos + 1));
                }
            }
            din.close();
            di = new DownloadInfo(downloadedLength, contentLength);
        } catch (Exception e) {
            Logger.error(e);
        }
    }
    return di;
}

From source file:org.apache.hadoop.hdfs.qjournal.client.URLLogInputStream.java

/**
 * Read the header of fsedit log//from w  ww. java 2  s . co  m
 * @param in fsedit stream
 * @return the edit log version number
 * @throws IOException if error occurs
 */
static int readLogVersion(DataInputStream in) throws IOException, LogHeaderCorruptException {
    int logVersion = 0;
    // Read log file version. Could be missing.
    in.mark(4);
    // If edits log is greater than 2G, available method will return negative
    // numbers, so we avoid having to call available
    boolean available = true;
    try {
        logVersion = in.readByte();
    } catch (EOFException e) {
        available = false;
    }

    if (available) {
        in.reset();
        logVersion = in.readInt();
        if (logVersion < FSConstants.LAYOUT_VERSION) { // future version
            throw new LogHeaderCorruptException("Unexpected version of the file system log file: " + logVersion
                    + ". Current version = " + FSConstants.LAYOUT_VERSION + ".");
        }
    }
    return logVersion;
}

From source file:org.hyperic.hq.measurement.agent.server.SenderThread.java

private static Record decodeRecord(String val) throws IOException {
    ByteArrayInputStream bIs;/*from   ww  w.  ja  v  a  2 s.c  o  m*/
    DataInputStream dIs;
    MetricValue measVal;
    boolean isAvail;
    int derivedID, dsnID;
    long retTime;

    bIs = new ByteArrayInputStream(Base64.decode(val));
    dIs = new DataInputStream(bIs);

    derivedID = dIs.readInt();
    retTime = dIs.readLong();
    dsnID = dIs.readInt();

    measVal = new MetricValue(dIs.readDouble(), retTime);
    return new Record(dsnID, measVal, derivedID);
}

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

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

    // primaryType
    String s = in.readUTF();
    state.setNodeTypeName(NameFactoryImpl.getInstance().create(s));
    // parentUUID (may be null)
    byte[] uuidBytes = new byte[NodeId.UUID_BYTE_LENGTH];
    in.readFully(uuidBytes);
    if (!Arrays.equals(uuidBytes, NULL_UUID_PLACEHOLDER_BYTES)) {
        state.setParentId(new NodeId(uuidBytes));
    }
    // definitionId
    in.readUTF();
    // mixin types
    int count = in.readInt(); // count
    Set<Name> set = new HashSet<Name>(count);
    for (int i = 0; i < count; i++) {
        set.add(NameFactoryImpl.getInstance().create(in.readUTF()));
    }
    if (set.size() > 0) {
        state.setMixinTypeNames(set);
    }
    // modCount
    short modCount = in.readShort();
    state.setModCount(modCount);
    // properties (names)
    count = in.readInt(); // count
    for (int i = 0; i < count; i++) {
        state.addPropertyName(NameFactoryImpl.getInstance().create(in.readUTF())); // name
    }
    // child nodes (list of name/uuid pairs)
    count = in.readInt(); // count
    for (int i = 0; i < count; i++) {
        Name name = NameFactoryImpl.getInstance().create(in.readUTF()); // name
        // uuid
        in.readFully(uuidBytes);
        state.addChildNodeEntry(name, new NodeId(uuidBytes));
    }
}

From source file:marytts.util.io.FileUtils.java

public static int[] readFromBinaryFile(String filename) throws IOException {
    DataInputStream d = null;
    try {//from www.j  a v a 2 s  . co  m
        d = new DataInputStream(new FileInputStream(new File(filename)));
        int[] x = null;
        int len = d.readInt();

        if (len > 0) {
            x = new int[len];

            for (int i = 0; i < len; i++) {
                x[i] = d.readInt();
            }
        }
        return x;
    } finally {
        close(d);
    }
}

From source file:com.iitb.cse.ConnectionInfo.java

public static String readFromStream(Socket socket, DataInputStream din, DataOutputStream dos)
        throws IOException {

    if (socket != null) {
        System.out.println("\nTrying to read from socket");
        //            synchronized (socket) {
        System.out.println("\nRead from socket");
        String data = "";
        int length = din.readInt();
        System.out.println("\nR Json length : " + length);
        for (int i = 0; i < length; ++i) {
            data += (char) din.readByte();
            //     System.out.println("\nR Read: Json length : "+(i+1)*8);
            //     System.out.println("\nSuccess : Json byte");
        }//w ww . j  ava2s .  c  o  m
        System.out.println("\nR Success : Json byte Complete");
        //         dos.writeInt(200);
        //         System.out.println("\nR Success : Json Write 200");
        //        dos.flush();
        System.out.println("\nRead from Socket Success!!!");
        /* try {
        //                if (din.available() > 0) {
            int length = din.readInt();
            System.out.println("\nR Json length : " + length);
            for (int i = 0; i < length; ++i) {
                data += (char) din.readByte();
                //     System.out.println("\nR Read: Json length : "+(i+1)*8);
                //     System.out.println("\nSuccess : Json byte");
            }
            System.out.println("\nR Success : Json byte Complete");
            //         dos.writeInt(200);
            //         System.out.println("\nR Success : Json Write 200");
            //        dos.flush();
            System.out.println("\nRead from Socket Success!!!");
        //               }
        } catch (IOException ex) {
        System.out.println("\n[1] IOEx :" + ex.toString() + "-->" + socket);
        } catch (Exception ex) {
        System.out.println("\n[2] Ex :" + ex.toString() + "-->" + socket);
        }*/
        return data;
        //            }
    } else {
        return null;
    }
}

From source file:org.nuras.mcpha.Client.java

/**
 * Return the status of the oscilloscope.
 * /*from w w  w.j a  v a2  s.  com*/
 * @return the status of the oscilloscope
 * @throws java.io.IOException 
 */
synchronized public static int mcphaReadOscilloscopeStatus() throws IOException {
    sendCommand(MCPHA_COMMAND_READ_OSCILLOSCOPE_STATUS, 0L, 4L);

    // read response
    DataInputStream in = new DataInputStream(deviceSocket.getInputStream());

    return Integer.reverseBytes(in.readInt());
}

From source file:com.android.hierarchyviewerlib.device.DeviceBridge.java

private static boolean readLayer(DataInputStream in, PsdFile psd) {
    try {//from w  w  w. jav  a  2s .  c o  m
        if (in.read() == 2) {
            return false;
        }
        String name = in.readUTF();
        boolean visible = in.read() == 1;
        int x = in.readInt();
        int y = in.readInt();
        int dataSize = in.readInt();

        byte[] data = new byte[dataSize];
        int read = 0;
        while (read < dataSize) {
            read += in.read(data, read, dataSize - read);
        }

        ByteArrayInputStream arrayIn = new ByteArrayInputStream(data);
        BufferedImage chunk = ImageIO.read(arrayIn);

        // Ensure the image is in the right format
        BufferedImage image = new BufferedImage(chunk.getWidth(), chunk.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(chunk, null, 0, 0);
        g.dispose();

        psd.addLayer(name, image, new Point(x, y), visible);

        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.carbondata.query.util.CacheUtil.java

public static int[] getGlobalSurrogateMapping(String filesLocaton) {
    int[] globalMapping = new int[0];
    if (null == filesLocaton) {
        return globalMapping;
    }/* w w w.  j  a  v  a 2s . c om*/
    DataInputStream fileChannel = null;
    try {
        if (!FileFactory.isFileExist(filesLocaton, FileFactory.getFileType(filesLocaton))) {
            return null;
        }
        fileChannel = FileFactory.getDataInputStream(filesLocaton, FileFactory.getFileType(filesLocaton));
        fileChannel.readInt();
        int minValue = fileChannel.readInt();
        int numberOfEntries = fileChannel.readInt();
        globalMapping = new int[numberOfEntries];
        int counter = 0;
        int index = 0;
        while (counter < numberOfEntries) {
            index = fileChannel.readInt();
            globalMapping[index - minValue] = fileChannel.readInt();
            counter++;
        }
    } catch (IOException e) {
        //            e.printStackTrace();
        LOGGER.error(e, e.getMessage());
    } finally {
        CarbonUtil.closeStreams(fileChannel);
    }
    return globalMapping;
}