Example usage for java.io DataInput readUnsignedByte

List of usage examples for java.io DataInput readUnsignedByte

Introduction

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

Prototype

int readUnsignedByte() throws IOException;

Source Link

Document

Reads one input byte, zero-extends it to type int , and returns the result, which is therefore in the range 0 through 255 .

Usage

From source file:org.apache.flink.types.StringValue.java

public static String readString(DataInput in) throws IOException {
    // the length we read is offset by one, because a length of zero indicates a null value
    int len = in.readUnsignedByte();

    if (len == 0) {
        return null;
    }//  ww w .  ja  v  a2s  . com

    if (len >= HIGH_BIT) {
        int shift = 7;
        int curr;
        len = len & 0x7f;
        while ((curr = in.readUnsignedByte()) >= HIGH_BIT) {
            len |= (curr & 0x7f) << shift;
            shift += 7;
        }
        len |= curr << shift;
    }

    // subtract one for the null length
    len -= 1;

    final char[] data = new char[len];

    for (int i = 0; i < len; i++) {
        int c = in.readUnsignedByte();
        if (c < HIGH_BIT) {
            data[i] = (char) c;
        } else {
            int shift = 7;
            int curr;
            c = c & 0x7f;
            while ((curr = in.readUnsignedByte()) >= HIGH_BIT) {
                c |= (curr & 0x7f) << shift;
                shift += 7;
            }
            c |= curr << shift;
            data[i] = (char) c;
        }
    }

    return new String(data, 0, len);
}

From source file:org.apache.geode.internal.cache.DiskInitFile.java

static long readDiskRegionID(DataInput dis) throws IOException {
    int bytesToRead = dis.readUnsignedByte();
    if (bytesToRead <= DiskStoreImpl.MAX_RESERVED_DRID && bytesToRead >= DiskStoreImpl.MIN_RESERVED_DRID) {
        long result = dis.readByte(); // we want to sign extend this first byte
        bytesToRead--;//from w  ww. j  a v a2s.  co  m
        while (bytesToRead > 0) {
            result <<= 8;
            result |= dis.readUnsignedByte(); // no sign extension
            bytesToRead--;
        }
        return result;
    } else {
        return bytesToRead;
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static Object basicReadObject(final DataInput in) throws IOException, ClassNotFoundException {
    checkIn(in);/*from   w  w  w .ja v a  2s  .  com*/

    // Read the header byte
    byte header = in.readByte();
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "basicReadObject: header={}", header);
    }
    switch (header) {
    case DS_FIXED_ID_BYTE:
        return DSFIDFactory.create(in.readByte(), in);
    case DS_FIXED_ID_SHORT:
        return DSFIDFactory.create(in.readShort(), in);
    case DS_FIXED_ID_INT:
        return DSFIDFactory.create(in.readInt(), in);
    case DS_NO_FIXED_ID:
        return readDataSerializableFixedID(in);
    case NULL:
        return null;
    case NULL_STRING:
    case STRING:
    case HUGE_STRING:
    case STRING_BYTES:
    case HUGE_STRING_BYTES:
        return readString(in, header);
    case CLASS:
        return readClass(in);
    case DATE:
        return readDate(in);
    case FILE:
        return readFile(in);
    case INET_ADDRESS:
        return readInetAddress(in);
    case BOOLEAN:
        return readBoolean(in);
    case CHARACTER:
        return readCharacter(in);
    case BYTE:
        return readByte(in);
    case SHORT:
        return readShort(in);
    case INTEGER:
        return readInteger(in);
    case LONG:
        return readLong(in);
    case FLOAT:
        return readFloat(in);
    case DOUBLE:
        return readDouble(in);
    case BYTE_ARRAY:
        return readByteArray(in);
    case ARRAY_OF_BYTE_ARRAYS:
        return readArrayOfByteArrays(in);
    case SHORT_ARRAY:
        return readShortArray(in);
    case STRING_ARRAY:
        return readStringArray(in);
    case INT_ARRAY:
        return readIntArray(in);
    case LONG_ARRAY:
        return readLongArray(in);
    case FLOAT_ARRAY:
        return readFloatArray(in);
    case DOUBLE_ARRAY:
        return readDoubleArray(in);
    case BOOLEAN_ARRAY:
        return readBooleanArray(in);
    case CHAR_ARRAY:
        return readCharArray(in);
    case OBJECT_ARRAY:
        return readObjectArray(in);
    case ARRAY_LIST:
        return readArrayList(in);
    case LINKED_LIST:
        return readLinkedList(in);
    case HASH_SET:
        return readHashSet(in);
    case LINKED_HASH_SET:
        return readLinkedHashSet(in);
    case HASH_MAP:
        return readHashMap(in);
    case IDENTITY_HASH_MAP:
        return readIdentityHashMap(in);
    case HASH_TABLE:
        return readHashtable(in);
    case CONCURRENT_HASH_MAP:
        return readConcurrentHashMap(in);
    case PROPERTIES:
        return readProperties(in);
    case TIME_UNIT:
        return readTimeUnit(in);
    case USER_CLASS:
        return readUserObject(in, in.readByte());
    case USER_CLASS_2:
        return readUserObject(in, in.readShort());
    case USER_CLASS_4:
        return readUserObject(in, in.readInt());
    case VECTOR:
        return readVector(in);
    case STACK:
        return readStack(in);
    case TREE_MAP:
        return readTreeMap(in);
    case TREE_SET:
        return readTreeSet(in);
    case BOOLEAN_TYPE:
        return Boolean.TYPE;
    case CHARACTER_TYPE:
        return Character.TYPE;
    case BYTE_TYPE:
        return Byte.TYPE;
    case SHORT_TYPE:
        return Short.TYPE;
    case INTEGER_TYPE:
        return Integer.TYPE;
    case LONG_TYPE:
        return Long.TYPE;
    case FLOAT_TYPE:
        return Float.TYPE;
    case DOUBLE_TYPE:
        return Double.TYPE;
    case VOID_TYPE:
        return Void.TYPE;

    case USER_DATA_SERIALIZABLE:
        return readUserDataSerializable(in, in.readByte());
    case USER_DATA_SERIALIZABLE_2:
        return readUserDataSerializable(in, in.readShort());
    case USER_DATA_SERIALIZABLE_4:
        return readUserDataSerializable(in, in.readInt());

    case DATA_SERIALIZABLE:
        return readDataSerializable(in);

    case SERIALIZABLE: {
        final boolean isDebugEnabled_SERIALIZER = logger.isTraceEnabled(LogMarker.SERIALIZER);
        Object serializableResult;
        if (in instanceof DSObjectInputStream) {
            serializableResult = ((DSObjectInputStream) in).readObject();
        } else {
            InputStream stream;
            if (in instanceof InputStream) {
                stream = (InputStream) in;
            } else {
                stream = new InputStream() {
                    @Override
                    public int read() throws IOException {
                        try {
                            return in.readUnsignedByte(); // fix for bug 47249
                        } catch (EOFException ignored) {
                            return -1;
                        }
                    }

                };
            }

            ObjectInput ois = new DSObjectInputStream(stream);
            if (stream instanceof VersionedDataStream) {
                Version v = ((VersionedDataStream) stream).getVersion();
                if (v != null && v != Version.CURRENT) {
                    ois = new VersionedObjectInput(ois, v);
                }
            }

            serializableResult = ois.readObject();

            if (isDebugEnabled_SERIALIZER) {
                logger.trace(LogMarker.SERIALIZER, "Read Serializable object: {}", serializableResult);
            }
        }
        if (isDebugEnabled_SERIALIZER) {
            logger.trace(LogMarker.SERIALIZER, "deserialized instanceof {}", serializableResult.getClass());
        }
        return serializableResult;
    }
    case PDX:
        return readPdxSerializable(in);
    case PDX_ENUM:
        return readPdxEnum(in);
    case GEMFIRE_ENUM:
        return readGemFireEnum(in);
    case PDX_INLINE_ENUM:
        return readPdxInlineEnum(in);
    case BIG_INTEGER:
        return readBigInteger(in);
    case BIG_DECIMAL:
        return readBigDecimal(in);
    case UUID:
        return readUUID(in);
    case TIMESTAMP:
        return readTimestamp(in);
    default:
        String s = "Unknown header byte: " + header;
        throw new IOException(s);
    }
}

From source file:org.apache.pig.data.BinInterSedes.java

private Tuple readSchemaTuple(DataInput in, byte type) throws IOException {
    int id;//  w w w.j a v  a  2s.  c  om
    switch (type) {
    case (SCHEMA_TUPLE_BYTE_INDEX):
        id = in.readUnsignedByte();
        break;
    case (SCHEMA_TUPLE_SHORT_INDEX):
        id = in.readUnsignedShort();
        break;
    case (SCHEMA_TUPLE):
        id = in.readInt();
        break;
    default:
        throw new RuntimeException("Invalid type given to readSchemaTuple: " + type);
    }

    Tuple st = SchemaTupleFactory.getInstance(id).newTuple();
    st.readFields(in);

    return st;
}

From source file:org.apache.pig.data.BinInterSedes.java

public int getTupleSize(DataInput in, byte type) throws IOException {
    int sz;/*from w  w w .  j a  va 2 s  . c o m*/
    switch (type) {
    case TUPLE_0:
        return 0;
    case TUPLE_1:
        return 1;
    case TUPLE_2:
        return 2;
    case TUPLE_3:
        return 3;
    case TUPLE_4:
        return 4;
    case TUPLE_5:
        return 5;
    case TUPLE_6:
        return 6;
    case TUPLE_7:
        return 7;
    case TUPLE_8:
        return 8;
    case TUPLE_9:
        return 9;
    case TINYTUPLE:
        sz = in.readUnsignedByte();
        break;
    case SMALLTUPLE:
        sz = in.readUnsignedShort();
        break;
    case TUPLE:
        sz = in.readInt();
        break;
    default: {
        int errCode = 2112;
        String msg = "Unexpected datatype " + type + " while reading tuple" + "from binary file.";
        throw new ExecException(msg, errCode, PigException.BUG);
    }
    }
    // 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");
    }
    return sz;
}

From source file:org.apache.pig.data.BinInterSedes.java

private DataBag readBag(DataInput in, byte type) throws IOException {
    DataBag bag = mBagFactory.newDefaultBag();
    long size;//from   ww  w.  j av a  2  s .  c om
    // determine size of bag
    switch (type) {
    case TINYBAG:
        size = in.readUnsignedByte();
        break;
    case SMALLBAG:
        size = in.readUnsignedShort();
        break;
    case BAG:
        size = in.readLong();
        break;
    default:
        int errCode = 2219;
        String msg = "Unexpected data while reading bag " + "from binary file.";
        throw new ExecException(msg, errCode, PigException.BUG);
    }

    for (long i = 0; i < size; i++) {
        try {
            Object o = readDatum(in);
            bag.add((Tuple) o);
        } catch (ExecException ee) {
            throw ee;
        }
    }
    return bag;
}

From source file:org.apache.pig.data.BinInterSedes.java

private Map<String, Object> readMap(DataInput in, byte type) throws IOException {
    int size;/*from  w w  w .ja va 2  s. c o  m*/
    switch (type) {
    case TINYMAP:
        size = in.readUnsignedByte();
        break;
    case SMALLMAP:
        size = in.readUnsignedShort();
        break;
    case MAP:
        size = in.readInt();
        break;
    default: {
        int errCode = 2220;
        String msg = "Unexpected data while reading map" + "from binary file.";
        throw new ExecException(msg, errCode, PigException.BUG);
    }
    }
    Map<String, Object> m = new HashMap<String, Object>(size);
    for (int i = 0; i < size; i++) {
        String key = (String) readDatum(in);
        m.put(key, readDatum(in));
    }
    return m;
}

From source file:org.apache.pig.data.Tuple.java

static int decodeInt(DataInput is) throws IOException {
    int i = 0;/*from  w  w  w. ja v a2s  .  c  o  m*/
    int c;
    while (true) {
        c = is.readUnsignedByte();
        if (c == -1)
            break;
        i <<= 7;
        i += c & 0x7f;
        if ((c & 0x80) != 0)
            break;
    }
    return i;
}