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:org.apache.geode.internal.cache.Oplog.java

/**
 * @throws DiskAccessException if this file does not belong to our parent
 *///  www.  j  av  a 2s  . c  o m
private void readGemfireVersionRecord(DataInput dis, File f) throws IOException {
    Version recoveredGFVersion = readProductVersionRecord(dis, f);
    final boolean hasDataVersion;
    if ((hasDataVersion = (recoveredGFVersion == Version.TOKEN))) {
        // actual GFE version will be the next record in this case
        byte opCode = dis.readByte();
        if (opCode != OPLOG_GEMFIRE_VERSION) {
            throw new DiskAccessException(LocalizedStrings.Oplog_UNKNOWN_OPCODE_0_FOUND_IN_DISK_OPERATION_LOG
                    .toLocalizedString(opCode), getParent());
        }
        recoveredGFVersion = readProductVersionRecord(dis, f);
    }
    if (this.gfversion == null) {
        this.gfversion = recoveredGFVersion;
    } else {
        assert this.gfversion == recoveredGFVersion;
    }
    if (hasDataVersion) {
        byte opCode = dis.readByte();
        if (opCode != OPLOG_GEMFIRE_VERSION) {
            throw new DiskAccessException(LocalizedStrings.Oplog_UNKNOWN_OPCODE_0_FOUND_IN_DISK_OPERATION_LOG
                    .toLocalizedString(opCode), getParent());
        }
        recoveredGFVersion = readProductVersionRecord(dis, f);
        if (this.dataVersion == null) {
            this.dataVersion = recoveredGFVersion;
        } else {
            assert this.dataVersion == recoveredGFVersion;
        }
    }
}

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

private void readEndOfRecord(DataInput di) throws IOException {
    int b = di.readByte();
    if (b != END_OF_RECORD_ID) {
        if (b == 0) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.Oplog_PARTIAL_RECORD));

            // this is expected if this is the last record and we died while writing
            // it.
            throw new EOFException("found partial last record");
        } else {//from   w  ww.j  a  v a2s.c  om
            // Our implementation currently relies on all unwritten bytes having
            // a value of 0. So throw this exception if we find one we didn't
            // expect.
            throw new IllegalStateException(
                    "expected end of record (byte==" + END_OF_RECORD_ID + ") or zero but found " + b);
        }
    }
}

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

/**
 * Reads a {@code TimeUnit} from a {@code DataInput}.
 *
 * @throws IOException A problem occurs while writing to {@code out}
 *//*from w w  w . ja  va  2s .  c  o m*/
private static TimeUnit readTimeUnit(DataInput in) throws IOException {
    InternalDataSerializer.checkIn(in);

    byte type = in.readByte();

    TimeUnit unit;
    switch (type) {
    case TIME_UNIT_NANOSECONDS:
        unit = TimeUnit.NANOSECONDS;
        break;
    case TIME_UNIT_MICROSECONDS:
        unit = TimeUnit.MICROSECONDS;
        break;
    case TIME_UNIT_MILLISECONDS:
        unit = TimeUnit.MILLISECONDS;
        break;
    case TIME_UNIT_SECONDS:
        unit = TimeUnit.SECONDS;
        break;
    default:
        throw new IOException(LocalizedStrings.DataSerializer_UNKNOWN_TIMEUNIT_TYPE_0.toLocalizedString(type));
    }

    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "Read TimeUnit: {}", unit);
    }

    return unit;
}

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

/**
 * @throws IOException since 6.6.2/* ww  w  .  j a  va 2 s  .  c  o m*/
 */
private static Object readPdxEnum(DataInput in) throws IOException {
    int dsId = in.readByte();
    int tmp = readArrayLength(in);
    int enumId = dsId << 24 | tmp & 0xFFFFFF;
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "read PdxEnum id={}", enumId);
    }
    InternalCache internalCache = GemFireCacheImpl
            .getForPdx("PDX registry is unavailable because the Cache has been closed.");
    TypeRegistry tr = internalCache.getPdxRegistry();

    Object result = tr.getEnumById(enumId);
    if (result instanceof PdxInstance) {
        getDMStats(internalCache).incPdxInstanceCreations();
    }
    return result;
}

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

public static int readArrayLength(DataInput in) throws IOException {
    byte code = in.readByte();
    if (code == NULL_ARRAY) {
        return -1;
    } else {//from  w  w  w .  j  a  v  a 2  s. co m
        int result = ubyteToInt(code);
        if (result > MAX_BYTE_ARRAY_LEN) {
            if (code == SHORT_ARRAY_LEN) {
                result = in.readUnsignedShort();
            } else if (code == INT_ARRAY_LEN) {
                result = in.readInt();
            } else {
                throw new IllegalStateException("unexpected array length code=" + code);
            }
        }
        return result;
    }
}

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

public static Object readDSFID(final DataInput in) throws IOException, ClassNotFoundException {
    checkIn(in);// w w  w  . ja va2  s  .c o m
    byte header = in.readByte();
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "readDSFID: header={}", header);
    }
    if (header == DS_FIXED_ID_BYTE) {
        return DSFIDFactory.create(in.readByte(), in);
    } else if (header == DS_FIXED_ID_SHORT) {
        return DSFIDFactory.create(in.readShort(), in);
    } else if (header == DS_NO_FIXED_ID) {
        return readDataSerializableFixedID(in);
    } else if (header == DS_FIXED_ID_INT) {
        return DSFIDFactory.create(in.readInt(), in);
    } else {
        throw new IllegalStateException("unexpected byte: " + header + " while reading dsfid");
    }
}

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

public static int readDSFIDHeader(final DataInput in) throws IOException {
    checkIn(in);//ww  w  .  ja  v  a  2  s. c  o  m
    byte header = in.readByte();
    if (header == DS_FIXED_ID_BYTE) {
        return in.readByte();
    } else if (header == DS_FIXED_ID_SHORT) {
        return in.readShort();
    } else if (header == DS_NO_FIXED_ID) {
        // is that correct??
        return Integer.MAX_VALUE;
    } else if (header == DS_FIXED_ID_INT) {
        return in.readInt();
    } else {
        throw new IllegalStateException("unexpected byte: " + header + " while reading dsfid");
    }
}

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 .j  av a 2  s  . c o m

    // 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.geode.internal.InternalDataSerializer.java

/**
 * Write a variable length long the old way (pre 7.0). Use this only in contexts where you might
 * need to communicate with pre 7.0 members or files.
 *///from w  w  w .  java2 s .c o  m
public static long readVLOld(DataInput in) throws IOException {
    byte code = in.readByte();
    long result;
    if (code < 0) {
        // mask off sign bit
        result = code & 0x7F;
        result <<= 8;
        result |= in.readByte() & 0xFF;
    } else if (code <= MAX_BYTE_VL) {
        result = code;
    } else if (code == INT_VL) {
        result = in.readInt();
    } else if (code == LONG_VL) {
        result = in.readLong();
    } else {
        throw new IllegalStateException("unexpected variable length code=" + code);
    }
    return result;
}

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

/**
 * Decode a long as a variable length array.
 * //w  w  w. j  av  a  2s . c o  m
 * This is taken from the varint encoding in protobufs (BSD licensed). See
 * https://developers.google.com/protocol-buffers/docs/encoding
 */
public static long readUnsignedVL(DataInput in) throws IOException {
    int shift = 0;
    long result = 0;
    while (shift < 64) {
        final byte b = in.readByte();
        result |= (long) (b & 0x7F) << shift;
        if ((b & 0x80) == 0) {
            return result;
        }
        shift += 7;
    }
    throw new GemFireIOException("Malformed variable length integer");
}