Example usage for java.io DataInputStream readInt

List of usage examples for java.io DataInputStream readInt

Introduction

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

Prototype

public final int readInt() throws IOException 

Source Link

Document

See the general contract of the readInt method of DataInput.

Usage

From source file:org.calrissian.accumulorecipes.commons.support.qfd.KeyToAttributeStoreQueryXform.java

@Override
public V apply(Map.Entry<Key, Value> keyValueEntry) {
    EventFields eventFields = new EventFields();
    eventFields.read(kryo, new Input(keyValueEntry.getValue().get()), EventFields.class);
    B entry = null;/*ww w . ja  va2  s .c o m*/
    for (Map.Entry<String, Set<EventFields.FieldValue>> fieldValue : eventFields.entrySet()) {

        for (EventFields.FieldValue fieldValue1 : fieldValue.getValue()) {

            String[] aliasVal = splitPreserveAllTokens(new String(fieldValue1.getValue()), ONE_BYTE);
            Object javaVal = typeRegistry.decode(aliasVal[0], aliasVal[1]);

            String vis = fieldValue1.getVisibility().getExpression().length > 0
                    ? new String(fieldValue1.getVisibility().getExpression())
                    : "";

            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(fieldValue1.getMetadata());
                DataInputStream dis = new DataInputStream(bais);
                dis.readLong(); // minimum expiration of keys and values
                long timestamp = dis.readLong();
                if (entry == null)
                    entry = buildAttributeCollectionFromKey(
                            new Key(keyValueEntry.getKey().getRow(), keyValueEntry.getKey().getColumnFamily(),
                                    keyValueEntry.getKey().getColumnQualifier(), timestamp));

                int length = dis.readInt();
                byte[] metaBytes = new byte[length];
                dis.readFully(metaBytes);

                Map<String, String> meta = metadataSerDe.deserialize(metaBytes);
                Map<String, String> metadata = (length == 0 ? new HashMap<String, String>()
                        : new HashMap<String, String>(meta));
                setVisibility(metadata, vis);
                Attribute attribute = new Attribute(fieldValue.getKey(), javaVal, metadata);
                entry.attr(attribute);
            } catch (Exception e) {
                log.error("There was an error deserializing the metadata for a attribute", e);
            }
        }
    }
    return entry.build();
}

From source file:J2MESearchMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);// w w  w. jav  a 2s  .  c  o  m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "A", "B", "M" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();
            String inputString;
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            if (recordstore.getNumRecords() > 0) {
                filter = new Filter("Mary");
                recordEnumeration = recordstore.enumerateRecords(filter, null, false);
                while (recordEnumeration.hasNextElement()) {
                    recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                    inputString = inputDataStream.readUTF() + " " + inputDataStream.readInt();
                    alert = new Alert("Reading", inputString, null, AlertType.WARNING);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert);
                }
            }
            inputStream.close();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
                filter.filterClose();
                recordEnumeration.destroy();
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }
}

From source file:org.apache.hadoop.hdfs.protocol.datatransfer.sasl.SaslDataTransferServer.java

/**
 * This method actually executes the server-side SASL handshake.
 *
 * @param underlyingOut connection output stream
 * @param underlyingIn connection input stream
 * @param saslProps properties of SASL negotiation
 * @param callbackHandler for responding to SASL callbacks
 * @return new pair of streams, wrapped after SASL negotiation
 * @throws IOException for any error//from  ww w .j  a  v a2 s  .  c om
 */
private IOStreamPair doSaslHandshake(OutputStream underlyingOut, InputStream underlyingIn,
        Map<String, String> saslProps, CallbackHandler callbackHandler) throws IOException {

    DataInputStream in = new DataInputStream(underlyingIn);
    DataOutputStream out = new DataOutputStream(underlyingOut);

    SaslParticipant sasl = SaslParticipant.createServerSaslParticipant(saslProps, callbackHandler);

    int magicNumber = in.readInt();
    if (magicNumber != SASL_TRANSFER_MAGIC_NUMBER) {
        throw new InvalidMagicNumberException(magicNumber, dnConf.getEncryptDataTransfer());
    }
    try {
        // step 1
        byte[] remoteResponse = readSaslMessage(in);
        byte[] localResponse = sasl.evaluateChallengeOrResponse(remoteResponse);
        sendSaslMessage(out, localResponse);

        // step 2 (server-side only)
        List<CipherOption> cipherOptions = Lists.newArrayList();
        remoteResponse = readSaslMessageAndNegotiationCipherOptions(in, cipherOptions);
        localResponse = sasl.evaluateChallengeOrResponse(remoteResponse);

        // SASL handshake is complete
        checkSaslComplete(sasl, saslProps);

        CipherOption cipherOption = null;
        if (sasl.isNegotiatedQopPrivacy()) {
            // Negotiate a cipher option
            cipherOption = negotiateCipherOption(dnConf.getConf(), cipherOptions);
            if (cipherOption != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Server using cipher suite " + cipherOption.getCipherSuite().getName());
                }
            }
        }

        // If negotiated cipher option is not null, wrap it before sending.
        sendSaslMessageAndNegotiatedCipherOption(out, localResponse, wrap(cipherOption, sasl));

        // If negotiated cipher option is not null, we will use it to create 
        // stream pair.
        return cipherOption != null
                ? createStreamPair(dnConf.getConf(), cipherOption, underlyingOut, underlyingIn, true)
                : sasl.createStreamPair(out, in);
    } catch (IOException ioe) {
        if (ioe instanceof SaslException && ioe.getCause() != null
                && ioe.getCause() instanceof InvalidEncryptionKeyException) {
            // This could just be because the client is long-lived and hasn't gotten
            // a new encryption key from the NN in a while. Upon receiving this
            // error, the client will get a new encryption key from the NN and retry
            // connecting to this DN.
            sendInvalidKeySaslErrorMessage(out, ioe.getCause().getMessage());
        } else {
            sendGenericSaslErrorMessage(out, ioe.getMessage());
        }
        throw ioe;
    }
}

From source file:com.joey.software.MoorFLSI.RepeatImageTextReader.java

public void loadData(File f, boolean clearData) throws IOException {
    BufferedInputStream inReader = new BufferedInputStream(new FileInputStream(f), 1000000);// RandomAccessFile in = new
    // RandomAccessFile(f, "r");
    DataInputStream in = new DataInputStream(inReader);
    int tot = in.readInt();
    wide = in.readInt();/*  ww  w.j a  v  a 2s. c  o  m*/
    high = in.readInt();

    short[][][] holder = new short[tot][wide][high];

    byte[] inputHolder = new byte[high * 2];
    for (int i = 0; i < tot; i++) {
        Date d = new Date(in.readLong());
        for (int x = 0; x < wide; x++) {
            in.read(inputHolder);
            for (int y = 0; y < high; y++) {
                holder[i][wide - 1 - x][high - y - 1] = BinaryToolkit.readShort(inputHolder, y * 2);
            }
            // for (int y = 0; y < high; y++)
            // {
            // holder[i][x][y] = in.readShort();
            // }
        }
        addData(d, holder[i]);
    }
    ((SpinnerNumberModel) currentValue.getModel()).setMaximum(imageData.size() - 1);
    in.close();
    setCurrentImage(0);
    image.updateMaxMin();
}

From source file:J2MEMixedRecordEnumerationExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from w  w  w . j  a va2s .  co  m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "First Record", "Second Record", "Third Record" };
            int outputInteger[] = { 15, 10, 5 };
            boolean outputBoolean[] = { true, false, true };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeBoolean(outputBoolean[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
            }
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
            StringBuffer buffer = new StringBuffer();
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            recordEnumeration = recordstore.enumerateRecords(null, null, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append("\n");
                buffer.append(inputDataStream.readBoolean());
                buffer.append("\n");
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
            inputStream.close();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
                recordEnumeration.destroy();
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }

}

From source file:edu.cornell.med.icb.goby.compression.HybridChunkCodec1.java

@Override
public boolean validate(byte firstByte, final DataInputStream input) {

    try {//from   w  w w.  j a  v a 2  s .  c o  m
        crc32.reset();

        final byte b = input.readByte();
        final byte c = input.readByte();
        final byte d = input.readByte();
        final int fullCodecContentSize = firstByte << 24 | (b & 0xFF) << 16 | (c & 0xFF) << 8 | (d & 0xFF);
        // System.out.printf("read %X %X %X %X but found size=%X", firstByte, b, c, d,fullCodecContentSize);

        final int hybridContentSize = input.readInt();
        final int storedChecksum = input.readInt();
        if (fullCodecContentSize < 0) {
            return false;
        }

        if (hybridContentSize < 0) {
            return false;
        }

        final byte[] bytes = new byte[hybridContentSize];
        int totalRead = 0;
        int offset = 0;
        while (totalRead < hybridContentSize) {
            final int numRead = input.read(bytes, offset, hybridContentSize - totalRead);
            if (numRead == -1) {
                break;
            }
            totalRead += numRead;
            offset += numRead;

        }
        if (totalRead != hybridContentSize) {
            return false;
        }
        crc32.update(bytes);
        final int computedChecksum = (int) crc32.getValue();
        return computedChecksum == storedChecksum;
    } catch (IOException e) {
        return false;
    }

}

From source file:ReadWriteStreams.java

public void readStream() {
    try {/* ww w. ja  v a2s  . c o m*/
        // Careful: Make sure this is big enough!
        // Better yet, test and reallocate if necessary      
        byte[] recData = new byte[50];

        // Read from the specified byte array
        ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);

        // Read Java data types from the above byte array
        DataInputStream strmDataType = new DataInputStream(strmBytes);

        for (int i = 1; i <= rs.getNumRecords(); i++) {
            // Get data into the byte array
            rs.getRecord(i, recData, 0);

            // Read back the data types      
            System.out.println("Record #" + i);
            System.out.println("UTF: " + strmDataType.readUTF());
            System.out.println("Boolean: " + strmDataType.readBoolean());
            System.out.println("Int: " + strmDataType.readInt());
            System.out.println("--------------------");

            // Reset so read starts at beginning of array 
            strmBytes.reset();
        }

        strmBytes.close();
        strmDataType.close();

    } catch (Exception e) {
        db(e.toString());
    }
}

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.TestOfflineImageViewer.java

private void changeLayoutVersion(File src, File dest, int newVersion) throws IOException {
    DataInputStream in = null;
    DataOutputStream out = null;//from w w  w  . ja  v  a2  s  . co  m

    try {
        in = new DataInputStream(new FileInputStream(src));
        out = new DataOutputStream(new FileOutputStream(dest));

        in.readInt();
        out.writeInt(newVersion);

        byte[] b = new byte[1024];
        while (in.read(b) > 0) {
            out.write(b);
        }
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
}

From source file:org.bdval.cache.TableCache.java

/**
 * Retrieve a cached table from the cache. isTableCached should be called
 * before calling this to verify that the table is cached. Null will
 * be returned if the table wasn't in the cache or there was a problem
 * reading the table.//from  ww  w . j av  a2s . c o  m
 *
 * @param splitId        The split id
 * @param splitType      The Split type
 * @param datasetName    The dataset name
 * @param geneListFilter A gene list. If not null, the gene list is queried with each double
 * column identifier to determine if the identifier is contained in the gene list. Columns
 * that do not match the gene list are not loaded.
 * @return the table read from the cache (or null if it could not be read)
 */
public Table getCachedTable(final int splitId, final String splitType, final String datasetName,
        final GeneList geneListFilter) {
    final File cachedTableFile = getCachedTableFile(splitId, splitType, datasetName);

    if (geneListFilter != null && !(geneListFilter instanceof FullGeneList)) {
        final ObjectSet<CharSequence> tableColumnIds = getTableColumnIds(splitId, splitType, datasetName);
        geneListFilter.calculateProbeSetSelection(tableColumnIds);
    }
    DataInputStream dataInput = null;
    try {
        dataInput = new DataInputStream(new FastBufferedInputStream(new FileInputStream(cachedTableFile)));
        final ArrayTable result = new ArrayTable();
        final int numberOfColumns = dataInput.readInt();
        LOG.info("Reading cached table with " + numberOfColumns + " columns");
        for (int i = 0; i < numberOfColumns; i++) {
            final String colType = dataInput.readUTF();
            final String colId = dataInput.readUTF();

            if ("s".equals(colType)) {
                final int numStrings = dataInput.readInt();
                resize(result, numStrings);
                final int columnIndex = result.addColumn(colId, String.class);
                for (int j = 0; j < numStrings; j++) {
                    result.appendObject(columnIndex, dataInput.readUTF());
                }
            } else if ("d".equals(colType)) {
                final int numDoubles = dataInput.readInt();
                resize(result, numDoubles);
                if (geneListFilter != null && !geneListFilter.isProbesetInList(colId)) {
                    // the column does not match the gene list. Skip this column
                    // we don't need to read these doubles, just skip them;
                    final int numBytes = Double.SIZE * numDoubles / 8;
                    final int actualBytes = dataInput.skipBytes(numBytes);
                    if (actualBytes != numBytes) {
                        LOG.warn("actual bytes skipped (" + actualBytes + ") does" + "not equal expected of "
                                + numBytes);
                    }
                    continue;
                }

                final int columnIndex = result.addColumn(colId, double.class);
                for (int j = 0; j < numDoubles; j++) {
                    result.appendDoubleValue(columnIndex, dataInput.readDouble());
                }

            } else {
                LOG.error("UNKNOWN COLUMN TYPE " + colType + " cannot read cached table from file "
                        + filenameOf(cachedTableFile));
                return null;
            }
        }
        return result;
    } catch (IOException e) {
        LOG.error(e);
        return null;
    } catch (TypeMismatchException e) {
        LOG.error("TypeMismatchException adding data to Table " + filenameOf(cachedTableFile), e);
        return null;
    } finally {
        IOUtils.closeQuietly(dataInput);
    }
}

From source file:net.sf.gazpachoquest.rest.auth.TokenStore.java

/**
 * Load the current set of tokens from the token file. If reading the tokens
 * fails or the token file does not exist, tokens will be generated on
 * demand./*from w  w  w.  j  a  va 2  s. c  o  m*/
 */
private void loadTokens() {
    if (tokenFile.isFile() && tokenFile.canRead()) {
        FileInputStream fin = null;
        DataInputStream keyInputStream = null;
        try {
            fin = new FileInputStream(tokenFile);
            keyInputStream = new DataInputStream(fin);
            int newCurrentToken = keyInputStream.readInt();
            long newNextUpdate = keyInputStream.readLong();
            SecretKey[] newKeys = new SecretKey[TOKEN_BUFFER_SIZE];
            for (int i = 0; i < newKeys.length; i++) {
                int isNull = keyInputStream.readInt();
                if (isNull == 1) {
                    int l = keyInputStream.readInt();
                    byte[] b = new byte[l];
                    keyInputStream.read(b);
                    newKeys[i] = new SecretKeySpec(b, HMAC_SHA1);
                } else {
                    newKeys[i] = null;
                }
            }

            // assign the tokes and schedule a next update
            nextUpdate = newNextUpdate;
            currentToken = newCurrentToken;
            currentTokens = newKeys;

        } catch (IOException e) {

            log.error("Failed to load cookie keys " + e.getMessage());

        } finally {

            if (keyInputStream != null) {
                try {
                    keyInputStream.close();
                } catch (IOException e) {
                }
            } else if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                }
            }
        }
    }

    // if there was a failure to read the current tokens, create new ones
    if (currentTokens == null) {
        currentTokens = new SecretKey[TOKEN_BUFFER_SIZE];
        nextUpdate = System.currentTimeMillis();
        currentToken = 0;
    }
}