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:clueweb09.WarcRecord.java

private static byte[] readNextRecord(DataInputStream in, StringBuffer headerBuffer) throws IOException {
    if (in == null) {
        return null;
    }//from w w  w  . j  a  v  a  2s  .  c o m
    if (headerBuffer == null) {
        return null;
    }

    String line = null;
    boolean foundMark = false;
    byte[] retContent = null;

    // cannot be using a buffered reader here!!!!
    // just read the header
    // first - find our WARC header
    while ((!foundMark) && ((line = readLineFromInputStream(in)) != null)) {
        if (line.startsWith(WARC_VERSION)) {
            WARC_VERSION_LINE = line;
            foundMark = true;
        }
    }

    // no WARC mark?
    if (!foundMark) {
        return null;
    }

    // LOG.info("Found WARC_VERSION");

    int contentLength = -1;
    // read until we see contentLength then an empty line
    // (to handle malformed ClueWeb09 headers that have blank lines)
    // get the content length and set our retContent
    for (line = readLineFromInputStream(in).trim(); line.length() > 0
            || contentLength < 0; line = readLineFromInputStream(in).trim()) {

        if (line.length() > 0) {
            headerBuffer.append(line);
            headerBuffer.append(LINE_ENDING);

            // find the content length designated by Content-Length: <length>
            String[] parts = line.split(":", 2);
            if (parts.length == 2 && parts[0].equals("Content-Length")) {
                try {
                    contentLength = Integer.parseInt(parts[1].trim());
                    // LOG.info("WARC record content length: " + contentLength);
                } catch (NumberFormatException nfEx) {
                    contentLength = -1;
                }
            }
        }
    }

    // now read the bytes of the content
    retContent = new byte[contentLength];
    int totalWant = contentLength;
    int totalRead = 0;
    //
    // LOOP TO REMOVE LEADING CR * LF 
    // To prevent last few characters from being cut off of the content
    // when reading
    //
    while ((totalRead == 0) && (totalRead < contentLength)) {
        byte CR = in.readByte();
        byte LF = in.readByte();
        if ((CR != 13) && (LF != 10)) {
            retContent[0] = CR;
            retContent[1] = LF;
            totalRead = 2;
            totalWant = contentLength - totalRead;
        }
    }
    //
    //
    //
    while (totalRead < contentLength) {
        try {
            int numRead = in.read(retContent, totalRead, totalWant);
            if (numRead < 0) {
                return null;
            } else {
                totalRead += numRead;
                totalWant = contentLength - totalRead;
            } // end if (numRead < 0) / else
        } catch (EOFException eofEx) {
            // resize to what we have
            if (totalRead > 0) {
                byte[] newReturn = new byte[totalRead];
                System.arraycopy(retContent, 0, newReturn, 0, totalRead);
                return newReturn;
            } else {
                return null;
            }
        } // end try/catch (EOFException)
    } // end while (totalRead < contentLength)

    return retContent;
}

From source file:edu.cmu.lemurproject.WarcRecord.java

private static byte[] readNextRecord(DataInputStream in, StringBuffer headerBuffer) throws IOException {
    if (in == null) {
        return null;
    }/*from w  w w .j  a va 2s  .com*/
    if (headerBuffer == null) {
        return null;
    }

    String line = null;
    boolean foundMark = false;
    byte[] retContent = null;

    // cannot be using a buffered reader here!!!!
    // just read the header
    // first - find our WARC header
    while ((!foundMark) && ((line = readLineFromInputStream(in)) != null)) {
        if (line.startsWith(WARC_VERSION)) {
            WARC_VERSION_LINE = line;
            foundMark = true;
        }
    }

    // no WARC mark?
    if (!foundMark) {
        return null;
    }

    //LOG.info("Found WARC_VERSION");

    int contentLength = -1;
    // read until we see contentLength then an empty line
    // (to handle malformed ClueWeb09 headers that have blank lines)
    // get the content length and set our retContent
    for (line = readLineFromInputStream(in).trim(); line.length() > 0
            || contentLength < 0; line = readLineFromInputStream(in).trim()) {

        if (line.length() > 0) {
            headerBuffer.append(line);
            headerBuffer.append(LINE_ENDING);

            // find the content length designated by Content-Length: <length>
            String[] parts = line.split(":", 2);
            if (parts.length == 2 && parts[0].equals("Content-Length")) {
                try {
                    contentLength = Integer.parseInt(parts[1].trim());
                    //LOG.info("WARC record content length: " + contentLength);
                } catch (NumberFormatException nfEx) {
                    contentLength = -1;
                }
            }
        }
    }

    // now read the bytes of the content
    retContent = new byte[contentLength];
    int totalWant = contentLength;
    int totalRead = 0;
    //
    // LOOP TO REMOVE LEADING CR * LF 
    // To prevent last few characters from being cut off of the content
    // when reading
    //
    while ((totalRead == 0) && (totalRead < contentLength)) {
        byte CR = in.readByte();
        byte LF = in.readByte();
        if ((CR != 13) && (LF != 10)) {
            retContent[0] = CR;

            // Minor change to process Common Crawl WET files.
            // Handle conversion records with single character line endings.
            if (retContent.length > 1) {
                retContent[1] = LF;
                totalRead = 2;
            } else
                totalRead = 1;

            totalWant = contentLength - totalRead;
        }
    }
    //
    //
    //
    while (totalRead < contentLength) {
        try {
            int numRead = in.read(retContent, totalRead, totalWant);
            if (numRead < 0) {
                return null;
            } else {
                totalRead += numRead;
                totalWant = contentLength - totalRead;
            } // end if (numRead < 0) / else
        } catch (EOFException eofEx) {
            // resize to what we have
            if (totalRead > 0) {
                byte[] newReturn = new byte[totalRead];
                System.arraycopy(retContent, 0, newReturn, 0, totalRead);
                return newReturn;
            } else {
                return null;
            }
        } // end try/catch (EOFException)
    } // end while (totalRead < contentLength)

    return retContent;
}

From source file:cwru.mjq.WarcRecord.java

private static byte[] readNextRecord(DataInputStream in, StringBuilder headerBuffer) throws IOException {
    if (in == null) {
        return null;
    }//from w  w w  .j  av  a2s .c o m
    if (headerBuffer == null) {
        return null;
    }

    String line;
    boolean foundMark = false;
    byte[] retContent;

    // cannot be using a buffered reader here!!!!
    // just read the header
    // first - find our WARC header
    while ((!foundMark) && ((line = readLineFromInputStream(in)) != null)) {
        if (line.startsWith(WARC_VERSION)) {
            WARC_VERSION_LINE = line;
            foundMark = true;
        }
    }

    // no WARC mark?
    if (!foundMark) {
        return null;
    }

    // LOG.info("Found WARC_VERSION");

    int contentLength = -1;
    // read until we see contentLength then an empty line
    // (to handle malformed ClueWeb09 headers that have blank lines)
    // get the content length and set our retContent
    for (line = readLineFromInputStream(in).trim(); line.length() > 0
            || contentLength < 0; line = readLineFromInputStream(in).trim()) {

        if (line.length() > 0) {
            headerBuffer.append(line);
            headerBuffer.append(LINE_ENDING);

            // find the content length designated by Content-Length:
            // <length>
            String[] parts = line.split(":", 2);
            if (parts.length == 2 && parts[0].equals(CONTENT_LENGTH)) {
                try {
                    contentLength = Integer.parseInt(parts[1].trim());
                    // LOG.info("WARC record content length: " +
                    // contentLength);
                } catch (NumberFormatException nfEx) {
                    contentLength = -1;
                }
            }
        }
    }

    // now read the bytes of the content
    retContent = new byte[contentLength];
    int totalWant = contentLength;
    int totalRead = 0;
    //
    // LOOP TO REMOVE LEADING CR * LF
    // To prevent last few characters from being cut off of the content
    // when reading
    //
    while ((totalRead == 0) && (totalRead < contentLength)) {
        byte CR = in.readByte();
        byte LF = in.readByte();
        if ((CR != 13) && (LF != 10)) {
            retContent[0] = CR;
            retContent[1] = LF;
            totalRead = 2;
            totalWant = contentLength - totalRead;
        }
    }
    //
    //
    //
    while (totalRead < contentLength) {
        try {
            int numRead = in.read(retContent, totalRead, totalWant);
            if (numRead < 0) {
                return null;
            } else {
                totalRead += numRead;
                totalWant = contentLength - totalRead;
            } // end if (numRead < 0) / else
        } catch (EOFException eofEx) {
            // resize to what we have
            if (totalRead > 0) {
                byte[] newReturn = new byte[totalRead];
                System.arraycopy(retContent, 0, newReturn, 0, totalRead);
                return newReturn;
            } else {
                return null;
            }
        } // end try/catch (EOFException)
    } // end while (totalRead < contentLength)

    return retContent;
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Read/write data from/to the DataXceiveServer.
 *//*from w  ww . j  a v  a2 s.  c o  m*/
public void run() {
    DataInputStream in = null;
    LOG.debug("DataXceiver starts processing new incoming data");

    try {
        //in = new DataInputStream(
        //    new BufferedInputStream(NetUtils.getInputStream(s), 
        //                            SMALL_BUFFER_SIZE)); 
        LOG.debug("Reading version from stream");

        LOG.debug("DataXceiver socket connected : " + s.isConnected());
        LOG.debug("DataXceiver socket closed : " + s.isClosed());

        ReliableInputStream ris = (ReliableInputStream) s.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(ris);

        in = new DataInputStream(bis);

        short version = in.readShort();
        LOG.debug("Version read : " + version);
        if (version != DataTransferProtocol.DATA_TRANSFER_VERSION) {
            throw new IOException("Version Mismatch");
        }

        //boolean local = s.getInetAddress().equals(s.getLocalAddress());
        boolean local = false; /** TODO A modifier proprement **/

        LOG.debug("Reading op type from stream");
        byte op = in.readByte();

        LOG.debug("op type read : " + op);

        // Make sure the xciver count is not exceeded
        int curXceiverCount = datanode.getXceiverCount();
        if (curXceiverCount > dataXceiverServer.maxXceiverCount) {
            throw new IOException("xceiverCount " + curXceiverCount
                    + " exceeds the limit of concurrent xcievers " + dataXceiverServer.maxXceiverCount);
        }

        long startTime = DataNode.now();

        switch (op) {
        case DataTransferProtocol.OP_READ_BLOCK:
            LOG.debug("Received a OP_READ_BLOCK op");
            readBlock(in);
            datanode.myMetrics.readBlockOp.inc(DataNode.now() - startTime);
            if (local)
                datanode.myMetrics.readsFromLocalClient.inc();
            else
                datanode.myMetrics.readsFromRemoteClient.inc();
            break;
        case DataTransferProtocol.OP_WRITE_BLOCK:
            LOG.debug("Received a OP_WRITE_BLOCK op");
            writeBlock(in);
            datanode.myMetrics.writeBlockOp.inc(DataNode.now() - startTime);
            if (local)
                datanode.myMetrics.writesFromLocalClient.inc();
            else
                datanode.myMetrics.writesFromRemoteClient.inc();
            break;
        case DataTransferProtocol.OP_READ_METADATA:
            LOG.debug("Received a OP_READ_METADATA op");
            readMetadata(in);
            datanode.myMetrics.readMetadataOp.inc(DataNode.now() - startTime);
            break;
        case DataTransferProtocol.OP_REPLACE_BLOCK: // for balancing purpose; send to a destination
            LOG.debug("Received a OP_REPLACE_BLOCK op");
            replaceBlock(in);
            datanode.myMetrics.replaceBlockOp.inc(DataNode.now() - startTime);
            break;
        case DataTransferProtocol.OP_COPY_BLOCK:
            // for balancing purpose; send to a proxy source
            LOG.debug("Received a OP_COPY_BLOCK op");
            copyBlock(in);
            datanode.myMetrics.copyBlockOp.inc(DataNode.now() - startTime);
            break;
        case DataTransferProtocol.OP_BLOCK_CHECKSUM: //get the checksum of a block
            LOG.debug("Received a OP_BLOCK_CHECKSUM op");
            getBlockChecksum(in);
            datanode.myMetrics.blockChecksumOp.inc(DataNode.now() - startTime);
            break;
        default:
            LOG.debug("Unknown op code");
            throw new IOException("Unknown opcode " + op + " in data stream");
        }

        IOUtils.closeStream(in);
        IOUtils.closeSocket(s);
    } catch (SocketTimeoutException ste) {
        LOG.debug("Time out while receiving data on DataXceiver");
        LOG.debug(ste);
        ste.printStackTrace();
    } catch (Exception t) {
        LOG.error(datanode.dnRegistration + ":DataXceiver FAILED", t);
        t.printStackTrace();
    } finally {
        LOG.debug(datanode.dnRegistration + ":Number of active connections is: " + datanode.getXceiverCount());
        // IOUtils.closeStream(in);
        // IOUtils.closeSocket(s);

        dataXceiverServer.childSockets.remove(s);
        s = null;
    }
}

From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java

public Properties readCredential(DataInputStream dis, DataOutputStream dos, DistributedSystem system)
        throws GemFireSecurityException, IOException {

    Properties credentials = null;
    boolean requireAuthentication = securityService.isClientSecurityRequired();
    try {/*from   w w w . j  a  va 2 s .  c o  m*/
        byte secureMode = dis.readByte();
        throwIfMissingRequiredCredentials(requireAuthentication, secureMode != CREDENTIALS_NONE);
        if (secureMode == CREDENTIALS_NORMAL) {
            this.appSecureMode = CREDENTIALS_NORMAL;
            /*
             * if (requireAuthentication) { credentials = DataSerializer.readProperties(dis); } else {
             * DataSerializer.readProperties(dis); // ignore the credentials }
             */
        } else if (secureMode == CREDENTIALS_DHENCRYPT) {
            this.appSecureMode = CREDENTIALS_DHENCRYPT;
            boolean sendAuthentication = dis.readBoolean();
            InternalLogWriter securityLogWriter = (InternalLogWriter) system.getSecurityLogWriter();
            // Get the symmetric encryption algorithm to be used
            // String skAlgo = DataSerializer.readString(dis);
            this.clientSKAlgo = DataSerializer.readString(dis);
            // Get the public key of the other side
            byte[] keyBytes = DataSerializer.readByteArray(dis);
            byte[] challenge = null;
            // PublicKey pubKey = null;
            if (requireAuthentication) {
                // Generate PublicKey from encoded form
                X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
                KeyFactory keyFact = KeyFactory.getInstance("DH");
                this.clientPublicKey = keyFact.generatePublic(x509KeySpec);

                // Send the public key to other side
                keyBytes = dhPublicKey.getEncoded();
                challenge = new byte[64];
                random.nextBytes(challenge);

                // If the server has to also authenticate itself then
                // sign the challenge from client.
                if (sendAuthentication) {
                    // Get the challenge string from client
                    byte[] clientChallenge = DataSerializer.readByteArray(dis);
                    if (privateKeyEncrypt == null) {
                        throw new AuthenticationFailedException(
                                LocalizedStrings.HandShake_SERVER_PRIVATE_KEY_NOT_AVAILABLE_FOR_CREATING_SIGNATURE
                                        .toLocalizedString());
                    }
                    // Sign the challenge from client and send it to the client
                    Signature sig = Signature.getInstance(privateKeySignAlgo);
                    sig.initSign(privateKeyEncrypt);
                    sig.update(clientChallenge);
                    byte[] signedBytes = sig.sign();
                    dos.writeByte(REPLY_OK);
                    DataSerializer.writeByteArray(keyBytes, dos);
                    // DataSerializer.writeString(privateKeyAlias, dos);
                    DataSerializer.writeString(privateKeySubject, dos);
                    DataSerializer.writeByteArray(signedBytes, dos);
                    securityLogWriter.fine("HandShake: sent the signed client challenge");
                } else {
                    // These two lines should not be moved before the if{} statement in
                    // a common block for both if...then...else parts. This is to handle
                    // the case when an AuthenticationFailedException is thrown by the
                    // if...then part when sending the signature.
                    dos.writeByte(REPLY_OK);
                    DataSerializer.writeByteArray(keyBytes, dos);
                }
                // Now send the server challenge
                DataSerializer.writeByteArray(challenge, dos);
                securityLogWriter.fine("HandShake: sent the public key and challenge");
                dos.flush();

                // Read and decrypt the credentials
                byte[] encBytes = DataSerializer.readByteArray(dis);
                Cipher c = getDecryptCipher(this.clientSKAlgo, this.clientPublicKey);
                byte[] credentialBytes = decryptBytes(encBytes, c);
                ByteArrayInputStream bis = new ByteArrayInputStream(credentialBytes);
                DataInputStream dinp = new DataInputStream(bis);
                // credentials = DataSerializer.readProperties(dinp);//Hitesh: we don't send in handshake
                // now
                byte[] challengeRes = DataSerializer.readByteArray(dinp);
                // Check the challenge string
                if (!Arrays.equals(challenge, challengeRes)) {
                    throw new AuthenticationFailedException(
                            LocalizedStrings.HandShake_MISMATCH_IN_CHALLENGE_BYTES_MALICIOUS_CLIENT
                                    .toLocalizedString());
                }
                dinp.close();
            } else {
                if (sendAuthentication) {
                    // Read and ignore the client challenge
                    DataSerializer.readByteArray(dis);
                }
                dos.writeByte(REPLY_AUTH_NOT_REQUIRED);
                dos.flush();
            }
        }
    } catch (IOException ex) {
        throw ex;
    } catch (GemFireSecurityException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AuthenticationFailedException(
                LocalizedStrings.HandShake_FAILURE_IN_READING_CREDENTIALS.toLocalizedString(), ex);
    }
    return credentials;
}

From source file:com.alphabetbloc.accessmrs.services.SyncManager.java

private void insertPatientForms(final DataInputStream zdis) throws Exception {
    long start = System.currentTimeMillis();

    SimpleDateFormat output = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");

    SQLiteDatabase db = DbProvider.getDb();
    InsertHelper ih = new InsertHelper(db, DataModel.OBSERVATIONS_TABLE);

    int ptIdIndex = ih.getColumnIndex(DataModel.KEY_PATIENT_ID);
    int obsTextIndex = ih.getColumnIndex(DataModel.KEY_VALUE_TEXT);
    int obsNumIndex = ih.getColumnIndex(DataModel.KEY_VALUE_NUMERIC);
    int obsDateIndex = ih.getColumnIndex(DataModel.KEY_VALUE_DATE);
    int obsIntIndex = ih.getColumnIndex(DataModel.KEY_VALUE_INT);
    int obsFieldIndex = ih.getColumnIndex(DataModel.KEY_FIELD_NAME);
    int obsTypeIndex = ih.getColumnIndex(DataModel.KEY_DATA_TYPE);
    int obsEncDateIndex = ih.getColumnIndex(DataModel.KEY_ENCOUNTER_DATE);

    db.beginTransaction();/*from  ww  w.j av  a2 s.  co  m*/
    sLoopProgress.set(0);
    try {
        sLoopCount.set(zdis.readInt());
        if (App.DEBUG)
            Log.v(TAG, "insertPatientForms icount: " + sLoopCount);
        for (int i = 1; i < sLoopCount.get() + 1; i++) {

            ih.prepareForInsert();
            ih.bind(ptIdIndex, zdis.readInt());
            ih.bind(obsFieldIndex, zdis.readUTF());
            byte dataType = zdis.readByte();
            if (dataType == DataModel.TYPE_STRING) {
                ih.bind(obsTextIndex, zdis.readUTF());
            } else if (dataType == DataModel.TYPE_INT) {
                ih.bind(obsIntIndex, zdis.readInt());
            } else if (dataType == DataModel.TYPE_DOUBLE) {
                ih.bind(obsNumIndex, zdis.readDouble());
            } else if (dataType == DataModel.TYPE_DATE) {
                ih.bind(obsDateIndex, parseDate(input, output, zdis.readUTF()));
            }
            ih.bind(obsTypeIndex, dataType);
            ih.bind(obsEncDateIndex, parseDate(input, output, zdis.readUTF()));
            ih.execute();

            sLoopProgress.getAndIncrement();
        }
        db.setTransactionSuccessful();
    } finally {
        ih.close();
        db.endTransaction();
    }

    long end = System.currentTimeMillis();
    if (App.DEBUG)
        Log.v("SYNC BENCHMARK",
                String.format("Total Patient-Forms: \n%d\nTotal Time: \n%d\nRecords per second: \n%.2f",
                        sLoopCount.get(), (int) (end - start),
                        1000 * (double) sLoopCount.get() / (double) (end - start)));
}

From source file:com.alphabetbloc.accessmrs.services.SyncManager.java

private void insertObservations(DataInputStream zdis) throws Exception {
    long start = System.currentTimeMillis();

    SimpleDateFormat output = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");

    SQLiteDatabase db = DbProvider.getDb();
    InsertHelper ih = new InsertHelper(db, DataModel.OBSERVATIONS_TABLE);
    int ptIdIndex = ih.getColumnIndex(DataModel.KEY_PATIENT_ID);
    int obsTextIndex = ih.getColumnIndex(DataModel.KEY_VALUE_TEXT);
    int obsNumIndex = ih.getColumnIndex(DataModel.KEY_VALUE_NUMERIC);
    int obsDateIndex = ih.getColumnIndex(DataModel.KEY_VALUE_DATE);
    int obsIntIndex = ih.getColumnIndex(DataModel.KEY_VALUE_INT);
    int obsFieldIndex = ih.getColumnIndex(DataModel.KEY_FIELD_NAME);
    int obsTypeIndex = ih.getColumnIndex(DataModel.KEY_DATA_TYPE);
    int obsEncDateIndex = ih.getColumnIndex(DataModel.KEY_ENCOUNTER_DATE);

    db.beginTransaction();//from   w  w  w.  j  ava 2  s. co  m
    sLoopProgress.set(0);
    int count = 0;
    try {
        sLoopCount.set(zdis.readInt());
        if (App.DEBUG)
            Log.v(TAG, "insertObservations icount: " + sLoopCount);
        for (int i = 1; i < sLoopCount.get() + 1; i++) {

            ih.prepareForInsert();
            ih.bind(ptIdIndex, zdis.readInt());
            ih.bind(obsFieldIndex, zdis.readUTF());
            byte dataType = zdis.readByte();
            if (dataType == DataModel.TYPE_STRING) {
                ih.bind(obsTextIndex, zdis.readUTF());
            } else if (dataType == DataModel.TYPE_INT) {
                ih.bind(obsIntIndex, zdis.readInt());
            } else if (dataType == DataModel.TYPE_DOUBLE) {
                ih.bind(obsNumIndex, zdis.readDouble());
            } else if (dataType == DataModel.TYPE_DATE) {
                ih.bind(obsDateIndex, parseDate(input, output, zdis.readUTF()));
            }

            ih.bind(obsTypeIndex, dataType);
            ih.bind(obsEncDateIndex, parseDate(input, output, zdis.readUTF()));
            ih.execute();

            count++;
            sLoopProgress.set(count * 2);
        }

        db.setTransactionSuccessful();
    } finally {
        ih.close();

        db.endTransaction();
    }

    long end = System.currentTimeMillis();
    if (App.DEBUG)
        Log.v("SYNC BENCHMARK",
                String.format("Total Observations: \n%d\nTotal Time: \n%d\nRecords per second: \n%.2f",
                        sLoopCount.get(), (int) (end - start),
                        1000 * (double) sLoopCount.get() / (double) (end - start)));

}

From source file:org.openmrs.module.odkconnector.serialization.web.PatientWebConnectorTest.java

@Test
public void serialize_shouldDisplayAllPatientInformation() throws Exception {

    // compose url
    URL u = new URL(SERVER_URL + "/module/odkconnector/download/patients.form");

    // setup http url connection
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setDoOutput(true);/*from w  ww .j a v  a  2  s . c  om*/
    connection.setRequestMethod("POST");
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);
    connection.addRequestProperty("Content-type", "application/octet-stream");

    // write auth details to connection
    DataOutputStream outputStream = new DataOutputStream(new GZIPOutputStream(connection.getOutputStream()));
    outputStream.writeUTF("admin");
    outputStream.writeUTF("test");
    outputStream.writeBoolean(true);
    outputStream.writeInt(2);
    outputStream.writeInt(1);
    outputStream.close();

    DataInputStream inputStream = new DataInputStream(new GZIPInputStream(connection.getInputStream()));
    Integer responseStatus = inputStream.readInt();

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

    int count = 0;
    byte[] buffer = new byte[1024];
    while ((count = inputStream.read(buffer)) > 0) {
        arrayOutputStream.write(buffer, 0, count);
    }
    arrayOutputStream.close();
    inputStream.close();

    File file = new File("/home/nribeka/connector.data");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(arrayOutputStream.toByteArray());
    fos.close();

    inputStream = new DataInputStream(new FileInputStream(file));

    if (responseStatus == HttpURLConnection.HTTP_OK) {

        // total number of patients
        Integer patientCounter = inputStream.readInt();
        System.out.println("Patient Counter: " + patientCounter);
        for (int j = 0; j < patientCounter; j++) {
            System.out.println("=================Patient=====================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Family Name: " + inputStream.readUTF());
            System.out.println("Middle Name: " + inputStream.readUTF());
            System.out.println("Last Name: " + inputStream.readUTF());
            System.out.println("Gender: " + inputStream.readUTF());
            System.out.println("Birth Date: " + inputStream.readUTF());
            System.out.println("Identifier: " + inputStream.readUTF());
            System.out.println("Patients: " + j + " out of " + patientCounter);
        }

        Integer obsCounter = inputStream.readInt();
        System.out.println("Observation Counter: " + obsCounter);
        for (int j = 0; j < obsCounter; j++) {
            System.out.println("==================Observation=================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Concept Name: " + inputStream.readUTF());

            byte type = inputStream.readByte();
            if (type == ObsSerializer.TYPE_STRING)
                System.out.println("Value: " + inputStream.readUTF());
            else if (type == ObsSerializer.TYPE_INT)
                System.out.println("Value: " + inputStream.readInt());
            else if (type == ObsSerializer.TYPE_DOUBLE)
                System.out.println("Value: " + inputStream.readDouble());
            else if (type == ObsSerializer.TYPE_DATE)
                System.out.println("Value: " + inputStream.readUTF());
            System.out.println("Time: " + inputStream.readUTF());
            System.out.println("Obs: " + j + " out of: " + obsCounter);
        }
        Integer formCounter = inputStream.readInt();
        System.out.println("Form Counter: " + formCounter);
        for (int j = 0; j < formCounter; j++) {
            System.out.println("==================Observation=================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Concept Name: " + inputStream.readUTF());

            byte type = inputStream.readByte();
            if (type == ObsSerializer.TYPE_STRING)
                System.out.println("Value: " + inputStream.readUTF());
            else if (type == ObsSerializer.TYPE_INT)
                System.out.println("Value: " + inputStream.readInt());
            else if (type == ObsSerializer.TYPE_DOUBLE)
                System.out.println("Value: " + inputStream.readDouble());
            else if (type == ObsSerializer.TYPE_DATE)
                System.out.println("Value: " + inputStream.readUTF());
            System.out.println("Time: " + inputStream.readUTF());
            System.out.println("Form: " + j + " out of: " + formCounter);
        }
    }
    inputStream.close();
}

From source file:org.lemurproject.galago.core.parse.WARCRecord.java

private static byte[] readNextRecord(DataInputStream in, StringBuffer headerBuffer) throws IOException {
    if (in == null) {
        return null;
    }/*from  w  w  w . j  a  v a2  s  .c  o  m*/
    if (headerBuffer == null) {
        return null;
    }

    String line = null;
    //    boolean foundMark = false;
    byte[] retContent = null;

    boolean foundMark = findNextWARCRecord(in);

    //    // cannot be using a buffered reader here!!!!
    //    // just read the header
    //    // first - find our WARC header
    //    while ((!foundMark) && ((line = readLineFromInputStream(in)) != null)) {
    //      if (line.startsWith(WARC_VERSION)) {
    //        WARC_VERSION_LINE = line;
    //        foundMark = true;
    //      }
    //    }

    // no WARC mark?
    if (!foundMark) {
        return null;
    }

    // LOG.info("Found WARC_VERSION");

    int contentLength = -1;
    // read until we see contentLength then an empty line
    // (to handle malformed ClueWeb09 headers that have blank lines)
    // get the content length and set our retContent
    for (line = readLineFromInputStream(in).trim(); line.length() > 0
            || contentLength < 0; line = readLineFromInputStream(in).trim()) {

        if (line.length() > 0) {
            headerBuffer.append(line);
            headerBuffer.append(LINE_ENDING);

            // find the content length designated by Content-Length: <length>
            String[] parts = line.split(":", 2);
            if (parts.length == 2 && parts[0].equals("Content-Length")) {
                try {
                    contentLength = Integer.parseInt(parts[1].trim());
                    // LOG.info("WARC record content length: " + contentLength);

                    // if this document is too long
                    if (contentLength > MAX_CONTENT_LENGTH) {
                        in.skip(contentLength);
                        if (!findNextWARCRecord(in)) {
                            return null;
                        }
                        headerBuffer.delete(0, headerBuffer.length());
                    }

                } catch (NumberFormatException nfEx) {
                    contentLength = -1;
                }
            }
        }
    }

    // now read the bytes of the content
    retContent = new byte[contentLength];
    int totalWant = contentLength;
    int totalRead = 0;
    //
    // LOOP TO REMOVE LEADING CR * LF 
    // To prevent last few characters from being cut off of the content
    // when reading
    //
    while ((totalRead == 0) && (totalRead < contentLength)) {
        byte CR = in.readByte();
        byte LF = in.readByte();
        if ((CR != 13) && (LF != 10)) {
            retContent[0] = CR;
            retContent[1] = LF;
            totalRead = 2;
            totalWant = contentLength - totalRead;
        }
    }
    //
    //
    //
    while (totalRead < contentLength) {
        try {
            int numRead = in.read(retContent, totalRead, totalWant);
            if (numRead < 0) {
                return null;
            } else {
                totalRead += numRead;
                totalWant = contentLength - totalRead;
            } // end if (numRead < 0) / else
        } catch (EOFException eofEx) {
            // resize to what we have
            if (totalRead > 0) {
                byte[] newReturn = new byte[totalRead];
                System.arraycopy(retContent, 0, newReturn, 0, totalRead);
                return newReturn;
            } else {
                return null;
            }
        } // end try/catch (EOFException)
    } // end while (totalRead < contentLength)

    return retContent;
}

From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java

/**
 * Used by client-side CacheClientUpdater to handshake with a server in order to receive messages
 * generated by subscriptions (register-interest, continuous query)
 *///from  w  ww .jav a2  s . c o  m
public ServerQueueStatus handshakeWithSubscriptionFeed(Socket sock, boolean isPrimary)
        throws IOException, AuthenticationRequiredException, AuthenticationFailedException,
        ServerRefusedConnectionException, ClassNotFoundException {
    ServerQueueStatus sqs = null;
    try {
        DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
        final InputStream in = sock.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        DistributedMember member = getIDForSocket(sock);
        if (!this.multiuserSecureMode) {
            this.credentials = getCredentials(member);
        }
        CommunicationMode mode = isPrimary ? CommunicationMode.PrimaryServerToClient
                : CommunicationMode.SecondaryServerToClient;
        write(dos, dis, mode, REPLY_OK, 0, new ArrayList(), this.credentials, member, true);

        // Wait here for a reply before continuing. This ensures that the client
        // updater is registered with the server before continuing.
        byte acceptanceCode = dis.readByte();
        if (acceptanceCode == (byte) 21 && !(sock instanceof SSLSocket)) {
            // This is likely the case of server setup with SSL and client not using
            // SSL
            throw new AuthenticationRequiredException(
                    LocalizedStrings.HandShake_SERVER_EXPECTING_SSL_CONNECTION.toLocalizedString());
        }

        // No need to check for return value since DataInputStream already throws
        // EOFException in case of EOF
        byte qType = dis.readByte();
        // read and ignore qSize flag
        int qSize = dis.readInt();
        sqs = new ServerQueueStatus(qType, qSize, member);

        // Read the message (if any)
        readMessage(dis, dos, acceptanceCode, member);

        // [sumedh] nothing more to be done for older clients used in tests
        // there is a difference in serializer map registration for >= 6.5.1.6
        // clients but that is not used in tests
        if (currentClientVersion.compareTo(Version.GFE_61) < 0) {
            return sqs;
        }
        HashMap instantiatorMap = DataSerializer.readHashMap(dis);
        for (Iterator itr = instantiatorMap.entrySet().iterator(); itr.hasNext();) {
            Map.Entry instantiator = (Map.Entry) itr.next();
            Integer id = (Integer) instantiator.getKey();
            ArrayList instantiatorArguments = (ArrayList) instantiator.getValue();
            InternalInstantiator.register((String) instantiatorArguments.get(0),
                    (String) instantiatorArguments.get(1), id, false);
        }

        HashMap dataSerializersMap = DataSerializer.readHashMap(dis);
        for (Iterator itr = dataSerializersMap.entrySet().iterator(); itr.hasNext();) {
            Map.Entry dataSerializer = (Map.Entry) itr.next();
            Integer id = (Integer) dataSerializer.getKey();
            InternalDataSerializer.register((String) dataSerializer.getValue(), false, null, null, id);
        }
        HashMap<Integer, ArrayList<String>> dsToSupportedClassNames = DataSerializer.readHashMap(dis);
        InternalDataSerializer.updateSupportedClassesMap(dsToSupportedClassNames);
    } catch (IOException ex) {
        CancelCriterion stopper = this.system.getCancelCriterion();
        stopper.checkCancelInProgress(null);
        throw ex;
    } catch (ClassNotFoundException ex) {
        CancelCriterion stopper = this.system.getCancelCriterion();
        stopper.checkCancelInProgress(null);
        throw ex;
    }
    return sqs;
}