Example usage for java.io DataInputStream readUTF

List of usage examples for java.io DataInputStream readUTF

Introduction

In this page you can find the example usage for java.io DataInputStream readUTF.

Prototype

public final String readUTF() throws IOException 

Source Link

Document

See the general contract of the readUTF method of DataInput.

Usage

From source file:org.apache.giraph.graph.BspServiceMaster.java

/**
 * Read the finalized checkpoint file and associated metadata files for the
 * checkpoint.  Modifies the {@link PartitionOwner} objects to get the
 * checkpoint prefixes.  It is an optimization to prevent all workers from
 * searching all the files.  Also read in the aggregator data from the
 * finalized checkpoint file and setting it.
 *
 * @param superstep Checkpoint set to examine.
 * @param partitionOwners Partition owners to modify with checkpoint
 *        prefixes//from ww w  .j av  a  2  s.co m
 * @throws IOException
 * @throws InterruptedException
 * @throws KeeperException
 */
private void prepareCheckpointRestart(long superstep, Collection<PartitionOwner> partitionOwners)
        throws IOException, KeeperException, InterruptedException {
    FileSystem fs = getFs();
    List<Path> validMetadataPathList = new ArrayList<Path>();
    String finalizedCheckpointPath = getCheckpointBasePath(superstep) + CHECKPOINT_FINALIZED_POSTFIX;
    DataInputStream finalizedStream = fs.open(new Path(finalizedCheckpointPath));
    int prefixFileCount = finalizedStream.readInt();
    for (int i = 0; i < prefixFileCount; ++i) {
        String metadataFilePath = finalizedStream.readUTF() + CHECKPOINT_METADATA_POSTFIX;
        validMetadataPathList.add(new Path(metadataFilePath));
    }

    // Set the merged aggregator data if it exists.
    int aggregatorDataSize = finalizedStream.readInt();
    if (aggregatorDataSize > 0) {
        byte[] aggregatorZkData = new byte[aggregatorDataSize];
        int actualDataRead = finalizedStream.read(aggregatorZkData, 0, aggregatorDataSize);
        if (actualDataRead != aggregatorDataSize) {
            throw new RuntimeException("prepareCheckpointRestart: Only read " + actualDataRead + " of "
                    + aggregatorDataSize + " aggregator bytes from " + finalizedCheckpointPath);
        }
        String mergedAggregatorPath = getMergedAggregatorPath(getApplicationAttempt(), superstep - 1);
        if (LOG.isInfoEnabled()) {
            LOG.info("prepareCheckpointRestart: Reloading merged " + "aggregator " + "data '"
                    + Arrays.toString(aggregatorZkData) + "' to previous checkpoint in path "
                    + mergedAggregatorPath);
        }
        if (getZkExt().exists(mergedAggregatorPath, false) == null) {
            getZkExt().createExt(mergedAggregatorPath, aggregatorZkData, Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT, true);
        } else {
            getZkExt().setData(mergedAggregatorPath, aggregatorZkData, -1);
        }
    }
    masterCompute.readFields(finalizedStream);
    finalizedStream.close();

    Map<Integer, PartitionOwner> idOwnerMap = new HashMap<Integer, PartitionOwner>();
    for (PartitionOwner partitionOwner : partitionOwners) {
        if (idOwnerMap.put(partitionOwner.getPartitionId(), partitionOwner) != null) {
            throw new IllegalStateException("prepareCheckpointRestart: Duplicate partition " + partitionOwner);
        }
    }
    // Reading the metadata files.  Simply assign each partition owner
    // the correct file prefix based on the partition id.
    for (Path metadataPath : validMetadataPathList) {
        String checkpointFilePrefix = metadataPath.toString();
        checkpointFilePrefix = checkpointFilePrefix.substring(0,
                checkpointFilePrefix.length() - CHECKPOINT_METADATA_POSTFIX.length());
        DataInputStream metadataStream = fs.open(metadataPath);
        long partitions = metadataStream.readInt();
        for (long i = 0; i < partitions; ++i) {
            long dataPos = metadataStream.readLong();
            int partitionId = metadataStream.readInt();
            PartitionOwner partitionOwner = idOwnerMap.get(partitionId);
            if (LOG.isInfoEnabled()) {
                LOG.info("prepareSuperstepRestart: File " + metadataPath + " with position " + dataPos
                        + ", partition id = " + partitionId + " assigned to " + partitionOwner);
            }
            partitionOwner.setCheckpointFilesPrefix(checkpointFilePrefix);
        }
        metadataStream.close();
    }
}

From source file:org.chromium.chrome.browser.tabmodel.TabPersistentStore.java

/**
 * Extracts the tab information from a given tab state stream.
 *
 * @param stream   The stream pointing to the tab state file to be parsed.
 * @param callback A callback to be streamed updates about the tab state information being read.
 * @param tabIds   A mapping of tab ID to whether the tab is an off the record tab.
 * @param forMerge Whether this state file was read as part of a merge.
 * @return The next available tab ID based on the maximum ID referenced in this state file.
 *///w  w  w.ja va 2  s .  c o m
public static int readSavedStateFile(DataInputStream stream, @Nullable OnTabStateReadCallback callback,
        @Nullable SparseBooleanArray tabIds, boolean forMerge) throws IOException {
    if (stream == null)
        return 0;
    long time = SystemClock.uptimeMillis();
    int nextId = 0;
    boolean skipUrlRead = false;
    boolean skipIncognitoCount = false;
    final int version = stream.readInt();
    if (version != SAVED_STATE_VERSION) {
        // We don't support restoring Tab data from before M18.
        if (version < 3)
            return 0;
        // Older versions are missing newer data.
        if (version < 5)
            skipIncognitoCount = true;
        if (version < 4)
            skipUrlRead = true;
    }

    final int count = stream.readInt();
    final int incognitoCount = skipIncognitoCount ? -1 : stream.readInt();
    final int incognitoActiveIndex = stream.readInt();
    final int standardActiveIndex = stream.readInt();
    if (count < 0 || incognitoActiveIndex >= count || standardActiveIndex >= count) {
        throw new IOException();
    }

    for (int i = 0; i < count; i++) {
        int id = stream.readInt();
        String tabUrl = skipUrlRead ? "" : stream.readUTF();
        if (id >= nextId)
            nextId = id + 1;
        if (tabIds != null)
            tabIds.append(id, true);

        Boolean isIncognito = (incognitoCount < 0) ? null : i < incognitoCount;
        if (callback != null) {
            callback.onDetailsRead(i, id, tabUrl, isIncognito, i == standardActiveIndex,
                    i == incognitoActiveIndex);
        }
    }

    if (forMerge) {
        logExecutionTime("ReadMergedStateTime", time);
        int tabCount = count + ((incognitoCount > 0) ? incognitoCount : 0);
        RecordHistogram.recordLinearCountHistogram("Android.TabPersistentStore.MergeStateTabCount", tabCount, 1,
                200, 200);
    }

    logExecutionTime("ReadSavedStateTime", time);

    return nextId;
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Checks a <code>NodePropBundle</code> from a data input stream.
 *
 * @param in the input stream/*  ww  w  . j  av a2 s. c o m*/
 * @return <code>true</code> if the data is valid;
 *         <code>false</code> otherwise.
 */
public boolean checkBundle(DataInputStream in) {
    int version;
    // primaryType & version
    try {
        // read version and primary type...special handling
        int index = in.readInt();

        // get version
        version = (index >> 24) & 0xff;
        index &= 0x00ffffff;
        String uri = nsIndex.indexToString(index);
        String local = nameIndex.indexToString(in.readInt());
        Name nodeTypeName = NameFactoryImpl.getInstance().create(uri, local);

        log.debug("Serialzation Version: " + version);
        log.debug("NodeTypeName: " + nodeTypeName);
    } catch (IOException e) {
        log.error("Error while reading NodeTypeName: " + e);
        return false;
    }
    try {
        NodeId parentId = readID(in);
        log.debug("ParentUUID: " + parentId);
    } catch (IOException e) {
        log.error("Error while reading ParentUUID: " + e);
        return false;
    }
    try {
        String definitionId = in.readUTF();
        log.debug("DefinitionId: " + definitionId);
    } catch (IOException e) {
        log.error("Error while reading DefinitionId: " + e);
        return false;
    }
    try {
        Name mixinName = readIndexedQName(in);
        while (mixinName != null) {
            log.debug("MixinTypeName: " + mixinName);
            mixinName = readIndexedQName(in);
        }
    } catch (IOException e) {
        log.error("Error while reading MixinTypes: " + e);
        return false;
    }
    try {
        Name propName = readIndexedQName(in);
        while (propName != null) {
            log.debug("PropertyName: " + propName);
            if (!checkPropertyState(in)) {
                return false;
            }
            propName = readIndexedQName(in);
        }
    } catch (IOException e) {
        log.error("Error while reading property names: " + e);
        return false;
    }
    try {
        boolean hasUUID = in.readBoolean();
        log.debug("hasUUID: " + hasUUID);
    } catch (IOException e) {
        log.error("Error while reading 'hasUUID': " + e);
        return false;
    }
    try {
        NodeId cneId = readID(in);
        while (cneId != null) {
            Name cneName = readQName(in);
            log.debug("ChildNodentry: " + cneId + ":" + cneName);
            cneId = readID(in);
        }
    } catch (IOException e) {
        log.error("Error while reading child node entry: " + e);
        return false;
    }

    if (version >= VERSION_1) {
        try {
            short modCount = readModCount(in);
            log.debug("modCount: " + modCount);
        } catch (IOException e) {
            log.error("Error while reading mod cout: " + e);
            return false;
        }
    }

    return true;
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Checks a <code>NodePropBundle</code> from a data input stream.
 *
 * @param in the input stream/*from  w w  w  .ja va 2  s  .  co m*/
 * @return <code>true</code> if the data is valid;
 *         <code>false</code> otherwise.
 */
public boolean checkBundle(DataInputStream in) {
    int version;
    // primaryType & version
    try {
        // read version and primary type...special handling
        int index = in.readInt();

        // get version
        version = (index >> 24) & 0xff;
        index &= 0x00ffffff;
        String uri = nsIndex.indexToString(index);
        String local = nameIndex.indexToString(in.readInt());
        Name nodeTypeName = NameFactoryImpl.getInstance().create(uri, local);

        log.debug("Serialzation Version: " + version);
        log.debug("NodeTypeName: " + nodeTypeName);
    } catch (IOException e) {
        log.error("Error while reading NodeTypeName: " + e);
        return false;
    }
    try {
        UUID parentUuid = readUUID(in);
        log.debug("ParentUUID: " + parentUuid);
    } catch (IOException e) {
        log.error("Error while reading ParentUUID: " + e);
        return false;
    }
    try {
        String definitionId = in.readUTF();
        log.debug("DefinitionId: " + definitionId);
    } catch (IOException e) {
        log.error("Error while reading DefinitionId: " + e);
        return false;
    }
    try {
        Name mixinName = readIndexedQName(in);
        while (mixinName != null) {
            log.debug("MixinTypeName: " + mixinName);
            mixinName = readIndexedQName(in);
        }
    } catch (IOException e) {
        log.error("Error while reading MixinTypes: " + e);
        return false;
    }
    try {
        Name propName = readIndexedQName(in);
        while (propName != null) {
            log.debug("PropertyName: " + propName);
            if (!checkPropertyState(in)) {
                return false;
            }
            propName = readIndexedQName(in);
        }
    } catch (IOException e) {
        log.error("Error while reading property names: " + e);
        return false;
    }
    try {
        boolean hasUUID = in.readBoolean();
        log.debug("hasUUID: " + hasUUID);
    } catch (IOException e) {
        log.error("Error while reading 'hasUUID': " + e);
        return false;
    }
    try {
        UUID cneUUID = readUUID(in);
        while (cneUUID != null) {
            Name cneName = readQName(in);
            log.debug("ChildNodentry: " + cneUUID + ":" + cneName);
            cneUUID = readUUID(in);
        }
    } catch (IOException e) {
        log.error("Error while reading child node entry: " + e);
        return false;
    }

    if (version >= VERSION_1) {
        try {
            short modCount = readModCount(in);
            log.debug("modCount: " + modCount);
        } catch (IOException e) {
            log.error("Error while reading mod cout: " + e);
            return false;
        }
    }

    return true;
}

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

private void insertPatients(DataInputStream zdis) throws Exception {

    long start = System.currentTimeMillis();
    SQLiteDatabase db = DbProvider.getDb();

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

    InsertHelper ih = new InsertHelper(db, DataModel.PATIENTS_TABLE);
    int ptIdIndex = ih.getColumnIndex(DataModel.KEY_PATIENT_ID);
    int ptIdentifierIndex = ih.getColumnIndex(DataModel.KEY_IDENTIFIER);
    int ptGivenIndex = ih.getColumnIndex(DataModel.KEY_GIVEN_NAME);
    int ptFamilyIndex = ih.getColumnIndex(DataModel.KEY_FAMILY_NAME);
    int ptMiddleIndex = ih.getColumnIndex(DataModel.KEY_MIDDLE_NAME);
    int ptBirthIndex = ih.getColumnIndex(DataModel.KEY_BIRTH_DATE);
    int ptGenderIndex = ih.getColumnIndex(DataModel.KEY_GENDER);

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

            ih.prepareForInsert();
            ih.bind(ptIdIndex, zdis.readInt());
            ih.bind(ptFamilyIndex, zdis.readUTF());
            ih.bind(ptMiddleIndex, zdis.readUTF());
            ih.bind(ptGivenIndex, zdis.readUTF());
            ih.bind(ptGenderIndex, zdis.readUTF());
            ih.bind(ptBirthIndex, parseDate(input, output, zdis.readUTF()));
            ih.bind(ptIdentifierIndex, 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 Patients: \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.apache.geode.internal.cache.tier.sockets.HandShake.java

protected void readMessage(DataInputStream dis, DataOutputStream dos, byte acceptanceCode,
        DistributedMember member) throws IOException, AuthenticationRequiredException,
        AuthenticationFailedException, ServerRefusedConnectionException {

    String message = dis.readUTF();
    if (message.length() == 0 && acceptanceCode != REPLY_WAN_CREDENTIALS) {
        return; // success
    }//from w w w  .ja  va 2s. c  om

    switch (acceptanceCode) {
    case REPLY_EXCEPTION_AUTHENTICATION_REQUIRED:
        throw new AuthenticationRequiredException(message);
    case REPLY_EXCEPTION_AUTHENTICATION_FAILED:
        throw new AuthenticationFailedException(message);
    case REPLY_EXCEPTION_DUPLICATE_DURABLE_CLIENT:
        throw new ServerRefusedConnectionException(member, message);
    case REPLY_WAN_CREDENTIALS:
        checkIfAuthenticWanSite(dis, dos, member);
        break;
    default:
        throw new ServerRefusedConnectionException(member, message);
    }
}

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();// w ww .  j ava 2  s  .  c  o  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();// w  ww  . j a  va2  s . c  o  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:com.android.leanlauncher.LauncherTransitionable.java

private static void readConfiguration(Context context, LocaleConfiguration configuration) {
    DataInputStream in = null;
    try {/*  w ww  .  j av  a 2  s  .  co  m*/
        in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
        configuration.locale = in.readUTF();
        configuration.mcc = in.readInt();
        configuration.mnc = in.readInt();
    } catch (FileNotFoundException e) {
        // Ignore
    } catch (IOException e) {
        // Ignore
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}