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:Main.java

public static Object[] readSettings(String file) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from  w  ww  .j  a  v a  2s .c om*/
        Object[] res = new Object[in.readInt()];
        for (int i = 0; i < res.length; i++) {
            char cl = in.readChar();
            switch (cl) {
            case 'S':
                res[i] = in.readUTF();
                break;
            case 'F':
                res[i] = in.readFloat();
                break;
            case 'D':
                res[i] = in.readDouble();
                break;
            case 'I':
                res[i] = in.readInt();
                break;
            case 'L':
                res[i] = in.readLong();
                break;
            case 'B':
                res[i] = in.readBoolean();
                break;
            case 'Y':
                res[i] = in.readByte();
                break;
            default:
                throw new IllegalStateException("cannot read type " + cl + " from " + file);
            }
        }
        return res;
    } finally {
        in.close();
    }
}

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");
        }/*from  www  . j  a v  a 2s  . co  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:gobblin.metrics.reporter.util.SchemaRegistryVersionWriter.java

@Override
public Schema readSchemaVersioningInformation(DataInputStream inputStream) throws IOException {
    if (inputStream.readByte() != KafkaAvroSchemaRegistry.MAGIC_BYTE) {
        throw new IOException("MAGIC_BYTE not found in Avro message.");
    }//from  w  ww .j  a  va2  s  . c  o  m

    byte[] byteKey = new byte[schemaIdLengthBytes];
    int bytesRead = inputStream.read(byteKey, 0, schemaIdLengthBytes);
    if (bytesRead != schemaIdLengthBytes) {
        throw new IOException(
                String.format("Could not read enough bytes for schema id. Expected: %d, found: %d.",
                        schemaIdLengthBytes, bytesRead));
    }
    String hexKey = Hex.encodeHexString(byteKey);

    try {
        return this.registry.getSchemaByKey(hexKey);
    } catch (SchemaRegistryException sre) {
        throw new IOException("Failed to retrieve schema for key " + hexKey, sre);
    }
}

From source file:org.structr.core.graph.SyncCommand.java

public static Object deserialize(final DataInputStream inputStream) throws IOException {

    Object serializedObject = null;
    final byte type = inputStream.readByte();
    Class clazz = classMap.get(type);

    if (clazz != null) {

        if (clazz.isArray()) {

            // len is the length of the underlying array
            final int len = inputStream.readInt();
            final Object[] array = (Object[]) Array.newInstance(clazz.getComponentType(), len);

            for (int i = 0; i < len; i++) {

                array[i] = deserialize(inputStream);
            }/*from  w ww. ja v  a 2  s . c  o  m*/

            // set array
            serializedObject = array;

        } else {

            serializedObject = readObject(inputStream, type);
        }

    } else if (type != 127) {

        logger.log(Level.WARNING, "Unsupported type \"{0}\" in input", type);
    }

    return serializedObject;
}

From source file:org.apache.lucene.replicator.http.HttpReplicator.java

@Override
public SessionToken checkForUpdate(String currVersion) throws IOException {
    String[] params = null;// w  w w  .j  ava 2s .  co  m
    if (currVersion != null) {
        params = new String[] { ReplicationService.REPLICATE_VERSION_PARAM, currVersion };
    }
    final HttpResponse response = executeGET(ReplicationAction.UPDATE.name(), params);
    return doAction(response, new Callable<SessionToken>() {
        @Override
        public SessionToken call() throws Exception {
            final DataInputStream dis = new DataInputStream(responseInputStream(response));
            try {
                if (dis.readByte() == 0) {
                    return null;
                } else {
                    return new SessionToken(dis);
                }
            } finally {
                dis.close();
            }
        }
    });
}

From source file:SafeUTF.java

public String safeReadUTF(DataInputStream in) throws IOException {
    boolean isNull = in.readByte() == NULL;

    if (isNull) {
        return null;
    }/*  w ww.  j a  va 2 s.  c  om*/

    short numChunks = in.readShort();

    int bufferSize = chunkSize * numChunks;

    // special handling for single chunk
    if (numChunks == 1) {
        // The text size is likely to be much smaller than the chunkSize
        // so set bufferSize to the min of the input stream available
        // and the maximum buffer size. Since the input stream
        // available() can be <= 0 we check for that and default to
        // a small msg size of 256 bytes.

        int inSize = in.available();

        if (inSize <= 0) {
            inSize = 256;
        }

        bufferSize = Math.min(inSize, bufferSize);

        lastReadBufferSize = bufferSize;
    }

    StringBuffer buff = new StringBuffer(bufferSize);

    for (int i = 0; i < numChunks; i++) {
        String s = in.readUTF();

        buff.append(s);
    }

    return buff.toString();
}

From source file:gobblin.metrics.reporter.KafkaAvroEventReporterWithSchemaRegistryTest.java

@Test
public void test() throws Exception {

    MetricContext context = MetricContext.builder("context").build();

    MockKafkaPusher pusher = new MockKafkaPusher();
    KafkaAvroSchemaRegistry registry = Mockito.mock(KafkaAvroSchemaRegistry.class);
    Mockito.when(registry.register(Mockito.any(Schema.class))).thenAnswer(new Answer<String>() {
        @Override//from w ww.java 2 s.  c  o m
        public String answer(InvocationOnMock invocation) throws Throwable {
            return register((Schema) invocation.getArguments()[0]);
        }
    });
    Mockito.when(registry.register(Mockito.any(Schema.class), Mockito.anyString()))
            .thenAnswer(new Answer<String>() {
                @Override
                public String answer(InvocationOnMock invocation) throws Throwable {
                    return register((Schema) invocation.getArguments()[0]);
                }
            });
    KafkaEventReporter kafkaReporter = KafkaAvroEventReporter.forContext(context).withKafkaPusher(pusher)
            .withSchemaRegistry(registry).build("localhost:0000", "topic");

    GobblinTrackingEvent event = new GobblinTrackingEvent(0l, "namespace", "name",
            Maps.<String, String>newHashMap());

    context.submitEvent(event);

    try {
        Thread.sleep(100);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    kafkaReporter.report();

    try {
        Thread.sleep(100);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    byte[] nextMessage = pusher.messageIterator().next();
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(nextMessage));
    Assert.assertEquals(is.readByte(), KafkaAvroSchemaRegistry.MAGIC_BYTE);
    byte[] readId = new byte[20];
    Assert.assertEquals(is.read(readId), 20);
    String readStringId = Hex.encodeHexString(readId);
    Assert.assertTrue(this.schemas.containsKey(readStringId));

    Schema schema = this.schemas.get(readStringId);
    Assert.assertFalse(schema.toString().contains("avro.java.string"));

    is.close();
}

From source file:org.openxdata.server.serializer.JavaRosaXformSerializer.java

/**
 * Deserializes forms./*from ww w .  j a  va2s.com*/
 * 
 * @return returns an List<String> of deserialized forms
 */
@Override
public List<String> deSerialize(InputStream in, Map<Integer, String> map) {
    List<String> forms = new ArrayList<String>();
    DataInputStream dis = new DataInputStream(in);
    try {
        int len = dis.readByte();
        for (int i = 0; i < len; i++)
            forms.add(dis.readUTF());
    } catch (IOException e) {
        throw new UnexpectedException(e);
    }

    return forms;
}

From source file:org.structr.core.graph.SyncCommand.java

private static Object readObject(final DataInputStream inputStream, final byte type) throws IOException {

    switch (type) {

    case 0:/*from   w  ww .j a v a 2 s.  c om*/
    case 1:
        return inputStream.readByte();

    case 2:
    case 3:
        return inputStream.readShort();

    case 4:
    case 5:
        return inputStream.readInt();

    case 6:
    case 7:
        return inputStream.readLong();

    case 8:
    case 9:
        return inputStream.readFloat();

    case 10:
    case 11:
        return inputStream.readDouble();

    case 12:
    case 13:
        return inputStream.readChar();

    case 14:
    case 15:
        return new String(deserializeData(inputStream), "UTF-8");

    // this doesn't work with very long strings
    //return inputStream.readUTF();

    case 16:
    case 17:
        return inputStream.readBoolean();
    }

    return null;
}

From source file:ubic.gemma.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

private int readByteLittleEndian(DataInputStream dis) throws IOException {
    return dis.readByte();
}