Example usage for java.io DataInput readByte

List of usage examples for java.io DataInput readByte

Introduction

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

Prototype

byte readByte() throws IOException;

Source Link

Document

Reads and returns one input byte.

Usage

From source file:Main.java

public static byte[] readByteBuffer(DataInput in) throws Exception {
    int b = in.readByte();
    if (b == 1) {
        b = in.readInt();//w w w . j  av a2 s.  c  om
        byte[] buf = new byte[b];
        in.readFully(buf, 0, buf.length);
        return buf;
    }
    return null;
}

From source file:com.smartitengineering.cms.api.impl.Utils.java

public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException {
    int allocationBlockSize = 2000;
    int capacity = allocationBlockSize;
    int length = 0;
    ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize);
    boolean notEof = true;
    while (notEof) {
        try {/*from   w  ww  .  j  av  a  2 s  . com*/
            buffer.put(in.readByte());
            if (++length >= capacity) {
                capacity += allocationBlockSize;
                buffer.limit(capacity);
            }
        } catch (EOFException ex) {
            notEof = false;
        }
    }
    String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length));
    return string;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

/** @return An unsigned short in an integer. */
private static int readShortLength(DataInput in) throws IOException {
    int length = (in.readByte() & 0xFF) << 8;
    return length | (in.readByte() & 0xFF);
}

From source file:net.darkmist.alib.io.Slurp.java

public static byte[] slurp(DataInput din) throws IOException {
    ByteArrayOutputStream baos;//  ww  w .  ja v a2 s .c  o m
    int b;

    if (din instanceof InputStream)
        return IOUtils.toByteArray((InputStream) din);
    baos = new ByteArrayOutputStream();
    while (true) {
        try {
            b = din.readByte();
        } catch (EOFException e) {
            return baos.toByteArray();
        }
        baos.write(b);
    }
}

From source file:com.aliyun.odps.io.TupleReaderWriter.java

/**
 * ????Tuple/*from   ww w .  j a  va  2s . c o m*/
 *
 * @param in
 *     ??Tuplefield
 * @param t
 *     ????Tuple
 * @throws IOException
 *     ???Tuple??
 */
public static void readTuple(DataInput in, Tuple t) throws IOException {
    // Make sure it's a tuple.
    byte b = in.readByte();
    if (b != TUPLE) {
        String msg = "Unexpected data while reading tuple from binary file.";
        throw new IOException(msg);
    }

    // Read the number of fields
    int sz = in.readInt();
    for (int i = 0; i < sz; i++) {
        byte type = in.readByte();
        t.append(readDatum(in, type));
    }
}

From source file:hivemall.fm.FFMPredictionModel.java

@Nonnull
private static void readEntry(@Nonnull final DataInput in, final int factors, @Nonnull final float[] Vf,
        @Nonnull Entry dst) throws IOException {
    final byte type = in.readByte();
    switch (type) {
    case HALF_FLOAT_ENTRY: {
        float W = HalfFloat.halfFloatToFloat(in.readShort());
        dst.setW(W);// w w  w . ja  va2  s  .c  o m
        for (int i = 0; i < factors; i++) {
            Vf[i] = HalfFloat.halfFloatToFloat(in.readShort());
        }
        dst.setV(Vf);
        break;
    }
    case W_ONLY_HALF_FLOAT_ENTRY: {
        float W = HalfFloat.halfFloatToFloat(in.readShort());
        dst.setW(W);
        break;
    }
    case FLOAT_ENTRY: {
        float W = in.readFloat();
        dst.setW(W);
        IOUtils.readFloats(in, Vf);
        dst.setV(Vf);
        break;
    }
    case W_ONLY_FLOAT_ENTRY: {
        float W = in.readFloat();
        dst.setW(W);
        break;
    }
    default:
        throw new IOException("Unexpected Entry type: " + type);
    }
}

From source file:com.aliyun.odps.io.TupleReaderWriter.java

@SuppressWarnings("unchecked")
private static Writable readDatum(DataInput in, byte type) throws IOException {
    switch (type) {
    case TUPLE:/*from  www. j a v a 2  s. c  o m*/
        int sz = in.readInt();
        // if sz == 0, we construct an "empty" tuple -
        // presumably the writer wrote an empty tuple!
        if (sz < 0) {
            throw new IOException("Invalid size " + sz + " for a tuple");
        }
        Tuple tp = new Tuple(sz);
        for (int i = 0; i < sz; i++) {
            byte b = in.readByte();
            tp.set(i, readDatum(in, b));
        }

        return tp;

    case NULL:
        return null;

    case INTWRITABLE:
        IntWritable iw = new IntWritable();
        iw.readFields(in);
        return iw;

    case LONGWRITABLE:
        LongWritable lw = new LongWritable();
        lw.readFields(in);
        return lw;

    case DATETIMEWRITABLE:
        DatetimeWritable dtw = new DatetimeWritable();
        dtw.readFields(in);
        return dtw;

    case DOUBLEWRITABLE:
        DoubleWritable dw = new DoubleWritable();
        dw.readFields(in);
        return dw;

    case BOOLEANWRITABLE:
        BooleanWritable bw = new BooleanWritable();
        bw.readFields(in);
        return bw;

    case BYTESWRITABLE:
        BytesWritable bsw = new BytesWritable();
        bsw.readFields(in);
        return bsw;

    case TEXT:
        Text t = new Text();
        t.readFields(in);
        return t;

    case NULLWRITABLE:
        NullWritable nw = NullWritable.get();
        nw.readFields(in);
        return nw;

    case UNKNOWN:
        String clsName = in.readUTF();
        try {
            Class<? extends Writable> cls = (Class<? extends Writable>) Class.forName(clsName);
            Writable w = (Writable) ReflectionUtils.newInstance(cls, null);
            w.readFields(in);
            return w;
        } catch (RuntimeException re) {
            LOG.info(re.getMessage());
            throw new IOException(re);
        } catch (ClassNotFoundException cnfe) {
            throw new IOException(cnfe);
        }

    default:
        throw new RuntimeException("Unexpected data type " + type + " found in stream.");
    }
}

From source file:com.iflytek.spider.parse.ParseText.java

public void readFields(DataInput in) throws IOException {
    byte version = in.readByte();
    switch (version) {
    case 1://from w  ww .  j  a v  a2 s .  c o m
        text = WritableUtils.readCompressedString(in);
        break;
    case VERSION:
        text = Text.readString(in);
        break;
    default:
        throw new VersionMismatchException(VERSION, version);
    }
}

From source file:com.aliyun.openservices.tablestore.hadoop.MultiCriteria.java

@Override
public void readFields(DataInput in) throws IOException {
    byte tag = in.readByte();
    if (tag != WritableConsts.MULTI_CRITERIA) {
        throw new IOException("broken input stream");
    }/*  ww w . j  av a 2 s.c  om*/
    List<RangeRowQueryCriteria> newCriteria = new ArrayList<RangeRowQueryCriteria>();
    int sz = in.readInt();
    for (int i = 0; i < sz; ++i) {
        newCriteria.add(RangeRowQueryCriteriaWritable.read(in).getRangeRowQueryCriteria());
    }
    criteria = newCriteria;
}

From source file:com.aliyun.openservices.tablestore.hadoop.Endpoint.java

@Override
public void readFields(DataInput in) throws IOException {
    byte tag = in.readByte();
    if (tag != WritableConsts.ENDPOINT) {
        throw new IOException("broken input stream");
    }//from  w  w  w  .  ja  va 2s .co m
    endpoint = null;
    instance = null;
    endpoint = in.readUTF();
    instance = in.readUTF();
}