Example usage for java.io DataInputStream readByte

List of usage examples for java.io DataInputStream readByte

Introduction

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

Prototype

public final byte readByte() throws IOException 

Source Link

Document

See the general contract of the readByte method of DataInput.

Usage

From source file:tudarmstadt.lt.ABSentiment.featureExtractor.util.W2vSpace.java

/**
 * Read a string from the binary model (System default should be UTF-8)
 * @param ds input data stream/*from  www  .  ja  v  a2  s .c o  m*/
 * @return a String from the model
 */
public static String readString(DataInputStream ds) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    while (true) {
        byte byteValue = ds.readByte();
        if ((byteValue != 32) && (byteValue != 10)) {
            byteBuffer.write(byteValue);
        } else if (byteBuffer.size() > 0) {
            break;
        }
    }
    String word = byteBuffer.toString();
    byteBuffer.close();
    return word;
}

From source file:tachyon.io.Utils.java

public static ByteBuffer readByteBuffer(DataInputStream is) throws IOException {
    int len = is.readInt();
    if (len == -1) {
        return null;
    }// www .  j  ava2s  .c  o m

    byte[] arr = new byte[len];
    for (int k = 0; k < len; k++) {
        arr[k] = is.readByte();
    }

    return ByteBuffer.wrap(arr);
}

From source file:Messenger.TorLib.java

/**
 * This method opens a TOR socket, and does an anonymous DNS resolve through it.
 * Since Tor caches things, this is a very fast lookup if we've already connected there
 * The resolve does a gethostbyname() on the exit node.
 * @param targetHostname String containing the hostname to look up.
 * @return String representation of the IP address: "x.x.x.x"
 *//*from w  w  w . j  a  va  2  s.  c o m*/
static String TorResolve(String targetHostname) {
    int targetPort = 0; // we dont need a port to resolve

    try {
        Socket s = TorSocketPre(targetHostname, targetPort, TOR_RESOLVE);
        DataInputStream is = new DataInputStream(s.getInputStream());

        byte version = is.readByte();
        byte status = is.readByte();
        if (status != (byte) 90) {
            //failed for some reason, return useful exception
            throw (new IOException(ParseSOCKSStatus(status)));
        }
        int port = is.readShort();
        byte[] ipAddrBytes = new byte[4];
        is.read(ipAddrBytes);
        InetAddress ia = InetAddress.getByAddress(ipAddrBytes);
        //System.out.println("Resolved into:"+ia);
        is.close();
        String addr = ia.toString().substring(1); // clip off the "/"
        return (addr);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (null);
}

From source file:com.zack6849.alphabot.api.Utils.java

public static String checkServerStatus(InetAddress i, int port) {
    String returns = "Error.";
    try {/* ww  w . java  2 s.  c o  m*/
        //wow...i never actually used the port argument?
        Socket s = new Socket(i, port);
        DataInputStream SS_BF = new DataInputStream(s.getInputStream());
        DataOutputStream d = new DataOutputStream(s.getOutputStream());
        d.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
        SS_BF.readByte();
        short length = SS_BF.readShort();
        StringBuilder sb = new StringBuilder();
        for (int in = 0; in < length; in++) {
            char ch = SS_BF.readChar();
            sb.append(ch);
        }
        String all = sb.toString().trim();
        System.out.println(all);
        String[] args1 = all.split("\u0000");
        if (args1[3].contains("")) {
            returns = "MOTD: " + args1[3].replaceAll("[a-m]", "").replaceAll("[1234567890]", "")
                    + "   players: [" + args1[4] + "/" + args1[5] + "]";
        } else {
            returns = "MOTD: " + args1[3] + "   players: [" + args1[4] + "/" + args1[5] + "]";
        }
    } catch (UnknownHostException e1) {
        returns = "the host you specified is unknown. check your settings.";
    } catch (IOException e1) {
        returns = "sorry, we couldn't reach this server, make sure that the server is up and has query enabled.";
    }
    return returns;
}

From source file:Messenger.TorLib.java

/**
 * This method creates a socket to the target host and port using TorSocketPre, then reads
 * the SOCKS information.// ww w  .  j  av  a  2s  .co m
 * @param targetHostname Hostname of destination host.
 * @param targetPort Port on remote destination host.
 * @return Fully initialized TCP Socket that tunnels to the target Host/Port via the Tor Proxy host/port.
 * @throws IOException when Socket and Read/Write exceptions occur.
 */
static Socket TorSocket(String targetHostname, int targetPort) throws IOException {
    Socket s = TorSocketPre(targetHostname, targetPort, TOR_CONNECT);
    DataInputStream is = new DataInputStream(s.getInputStream());

    // only the status is useful on a TOR CONNECT
    byte version = is.readByte();
    byte status = is.readByte();
    if (status != (byte) 90) {
        //failed for some reason, return useful exception
        throw (new IOException(ParseSOCKSStatus(status)));
    }
    //      System.out.println("status: "+ParseSOCKSStatus(status));
    int port = is.readShort();
    int ipAddr = is.readInt();
    return (s);
}

From source file:org.apache.flink.runtime.metrics.dump.MetricDumpSerialization.java

private static QueryScopeInfo deserializeMetricInfo(DataInputStream dis) throws IOException {
    String jobID;//from www  . j ava 2 s.c  om
    String vertexID;
    int subtaskIndex;

    String scope = deserializeString(dis);
    byte cat = dis.readByte();
    switch (cat) {
    case INFO_CATEGORY_JM:
        return new QueryScopeInfo.JobManagerQueryScopeInfo(scope);
    case INFO_CATEGORY_TM:
        String tmID = deserializeString(dis);
        return new QueryScopeInfo.TaskManagerQueryScopeInfo(tmID, scope);
    case INFO_CATEGORY_JOB:
        jobID = deserializeString(dis);
        return new QueryScopeInfo.JobQueryScopeInfo(jobID, scope);
    case INFO_CATEGORY_TASK:
        jobID = deserializeString(dis);
        vertexID = deserializeString(dis);
        subtaskIndex = dis.readInt();
        return new QueryScopeInfo.TaskQueryScopeInfo(jobID, vertexID, subtaskIndex, scope);
    case INFO_CATEGORY_OPERATOR:
        jobID = deserializeString(dis);
        vertexID = deserializeString(dis);
        subtaskIndex = dis.readInt();
        String operatorName = deserializeString(dis);
        return new QueryScopeInfo.OperatorQueryScopeInfo(jobID, vertexID, subtaskIndex, operatorName, scope);
    default:
        throw new IOException("sup");
    }
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java

/**
 * @return 8 bit signed integral number//from w  ww.  j av  a2  s . c om
 */
private static int readByte(DataInputStream dis) throws IOException {
    return dis.readByte();
}

From source file:org.skife.muckery.mappy.ByteUtils.java

public static long readVarNumber(DataInputStream input) throws IOException {
    int b = 0xFF & input.readByte();
    if ((b & MASK_10000000) == 0) {
        // one byte value, mask off the size bit and return
        return MASK_01111111 & b;
    } else if ((b & MASK_11000000) == MASK_10000000) {
        // two byte value, mask off first two bits
        long val = (b & MASK_00111111) << Byte.SIZE;
        val |= 0xFF & input.readByte();
        return val;
    } else if ((b & MASK_11100000) == MASK_11000000) {
        // four byte value, mask off three bits
        long val = (b & MASK_00011111);
        for (int i = 0; i < 3; i++) {
            val <<= Byte.SIZE;
            val |= 0xFF & input.readByte();
        }/*from   w  ww .  j  a  va2  s  .  com*/
        return val;
    } else if ((b & MASK_11100000) == MASK_11100000) {
        // eight byte value, mask off three bits
        long val = (b & MASK_00011111);
        for (int i = 0; i < 7; i++) {
            val <<= Byte.SIZE;
            val |= 0xFF & input.readByte();
        }
        return val;
    } else {
        throw new IllegalArgumentException("Unknown prefix!");
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.bookkeeper.BookKeeperEditLogInputStream.java

/**
 * Safely reads the log version from the stream. Logic is exactly the same
 * as in the equivalent {@link EditLogFileInputStream} method.
 * @see EditLogFileInputStream#readLogVersion(DataInputStream)
 * @return The log version or 0 if stream is empty
 *//* w  w w.  j  av a 2  s . c o  m*/
private static int readLogVersion(DataInputStream in) throws IOException {
    int logVersion = 0;
    in.mark(4);
    // See comments in EditLogFileInputStream as to why readLogVersion is
    // implemented in this way
    boolean available = true;
    try {
        logVersion = in.readByte();
    } catch (EOFException e) {
        available = false;
    }

    if (available) {
        in.reset();
        logVersion = in.readInt();
        if (logVersion < FSConstants.LAYOUT_VERSION) {
            throw new LedgerHeaderCorruptException("Unexpected version of the log segment in the ledger: "
                    + logVersion + ". Current version is " + FSConstants.LAYOUT_VERSION + ".");
        }
    }
    return logVersion;
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java

private static int readIntBigEndian(DataInputStream dis) throws IOException {
    byte[] buf = new byte[4];

    for (int i = 0; i < buf.length; i++) {
        buf[i] = dis.readByte();
    }/*from   w  w w .j  av  a  2s.  c  o m*/

    return ByteBuffer.wrap(buf).order(ByteOrder.BIG_ENDIAN).getInt();
}