Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

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

Prototype

public final void readFully(byte b[]) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:view.CertificateManagementDialog.java

private InputStream fullStream(String fname) throws IOException {
    FileInputStream fis = new FileInputStream(fname);
    DataInputStream dis = new DataInputStream(fis);
    byte[] bytes = new byte[dis.available()];
    dis.readFully(bytes);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    return bais;//from w w  w  . j a  va2  s.  c om
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Read stream.//from  w ww.ja  va  2 s  .c  o m
 *
 * @param inStream
 *            InputStream
 * @return String
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] readStream(final InputStream inStream) throws IOException {
    DataInputStream in = null;
    try {
        in = new DataInputStream(inStream);
        final int size = in.available();
        final byte arr[] = new byte[size];
        in.readFully(arr);
        return arr;
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

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

@Override
public V apply(Map.Entry<Key, Value> keyValueEntry) {
    try {/*w  ww . j a  v  a  2 s  . co  m*/

        B entry = null;

        List<Map.Entry<Key, Value>> groupedKVs = decodeRow(keyValueEntry.getKey(), keyValueEntry.getValue());

        for (Map.Entry<Key, Value> groupedEvent : groupedKVs) {

            String[] colQParts = splitPreserveAllTokens(groupedEvent.getKey().getColumnQualifier().toString(),
                    NULL_BYTE);
            String[] aliasValue = splitPreserveAllTokens(colQParts[1], ONE_BYTE);
            String visibility = groupedEvent.getKey().getColumnVisibility().toString();

            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(groupedEvent.getValue().get());
                DataInputStream dis = new DataInputStream(bais);
                dis.readLong(); // minimum expiration of keys and values
                long timestamp = dis.readLong();

                if (entry == null)
                    entry = buildEntryFromKey(
                            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, visibility);
                Attribute attribute = new Attribute(colQParts[0],
                        typeRegistry.decode(aliasValue[0], aliasValue[1]), metadata);
                entry.attr(attribute);
            } catch (Exception e) {
                log.error("There was an error deserializing the metadata for a attribute", e);
            }
        }

        return entry.build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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;// www .  j a v a 2  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:org.carbondata.query.util.CacheUtil.java

private static Member[][] populateMemberCache(DataInputStream fileChannel, CarbonFile memberFile,
        String fileName, String dataType) throws IOException {
    // ByteBuffer toltalLength, memberLength, surrogateKey, bf3;
    // subtracted 4 as last 4 bytes will have the max value for no of
    // records/*ww w . j  ava 2s.com*/
    long currPositionIndex = 0;
    long size = memberFile.getSize() - 4;
    long skipSize = size;
    long actualSkipSize = 0;
    while (actualSkipSize != size) {
        actualSkipSize += fileChannel.skip(skipSize);
        skipSize = skipSize - actualSkipSize;
    }
    //        LOGGER.debug(CarbonEngineLogEvent.UNIBI_CARBONENGINE_MSG, "Bytes skipped " +
    // skipSize);
    int maxVal = fileChannel.readInt();
    CarbonUtil.closeStreams(fileChannel);
    fileChannel = FileFactory.getDataInputStream(fileName, FileFactory.getFileType(fileName));
    // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
    ByteBuffer buffer = ByteBuffer.allocate((int) size);
    // CHECKSTYLE:OFF
    fileChannel.readFully(buffer.array());
    int minVal = buffer.getInt();
    int totalArraySize = maxVal - minVal + 1;
    Member[][] surogateKeyArrays = null;
    if (totalArraySize > CarbonCommonConstants.LEVEL_ARRAY_SIZE) {
        int div = totalArraySize / CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        int rem = totalArraySize % CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        if (rem > 0) {
            div++;
        }
        surogateKeyArrays = new Member[div][];

        for (int i = 0; i < div - 1; i++) {
            surogateKeyArrays[i] = new Member[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }

        if (rem > 0) {
            surogateKeyArrays[surogateKeyArrays.length - 1] = new Member[rem];
        } else {
            surogateKeyArrays[surogateKeyArrays.length
                    - 1] = new Member[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }
    } else {
        surogateKeyArrays = new Member[1][totalArraySize];
    }
    //        Member[] surogateKeyArrays = new Member[maxVal-minVal+1];
    //        int surrogateKeyIndex = minVal;
    currPositionIndex += 4;
    //
    int current = 0;
    // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
    boolean enableEncoding = Boolean
            .valueOf(CarbonProperties.getInstance().getProperty(CarbonCommonConstants.ENABLE_BASE64_ENCODING,
                    CarbonCommonConstants.ENABLE_BASE64_ENCODING_DEFAULT));
    // CHECKSTYLE:ON
    int index = 0;
    int prvArrayIndex = 0;
    while (currPositionIndex < size) {
        int len = buffer.getInt();
        // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
        // CHECKSTYLE:ON
        currPositionIndex += 4;
        byte[] rowBytes = new byte[len];
        buffer.get(rowBytes);
        currPositionIndex += len;
        // No:Approval-361
        if (enableEncoding) {
            rowBytes = Base64.decodeBase64(rowBytes);
        }
        surogateKeyArrays[current / CarbonCommonConstants.LEVEL_ARRAY_SIZE][index] = new Member(rowBytes);
        current++;
        if (current / CarbonCommonConstants.LEVEL_ARRAY_SIZE > prvArrayIndex) {
            prvArrayIndex++;
            index = 0;
        } else {
            index++;
        }
    }
    return surogateKeyArrays;
}

From source file:netscape.security.pkcs.PKCS7.java

/**
 * Unmarshals a PKCS7 block from its encoded form, parsing the
 * encoded bytes from the InputStream./*from  w w  w. j a v a 2  s  .c  o  m*/
 *
 * @param in an input stream holding at least one PKCS7 block.
 * @exception ParsingException on parsing errors.
 * @exception IOException on other errors.
 */
public PKCS7(InputStream in) throws ParsingException, IOException {
    DataInputStream dis = new DataInputStream(in);

    int len = 0;
    byte[] newbuf = new byte[len];
    byte[] oldbuf = new byte[len];
    byte[] data = new byte[len];

    do {
        newbuf = new byte[dis.available()];
        len += dis.available();
        dis.readFully(newbuf);
        data = new byte[len];

        System.arraycopy(oldbuf, 0, data, 0, oldbuf.length);
        System.arraycopy(newbuf, 0, data, oldbuf.length, newbuf.length);
        oldbuf = new byte[len];
        System.arraycopy(data, 0, oldbuf, 0, data.length);

    } while (dis.available() > 0);

    parse(new DerInputStream(data));
}

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

/**
 * Write a small binary value and return the data.
 *
 * @param value the binary value// w  w w. j  av  a2 s . co  m
 * @param state the property state (for error messages)
 * @param i the index (for error messages)
 * @return the data
 * @throws IOException if the data could not be read
 */
private byte[] writeSmallBinary(InternalValue value, NodePropBundle.PropertyEntry state, int i)
        throws IOException {
    try {
        int size = (int) value.getLength();
        out.writeInt(size);
        byte[] data = new byte[size];
        DataInputStream in = new DataInputStream(value.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
        out.write(data, 0, data.length);
        return data;
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + value;
        log.error(msg, e);
        throw new IOExceptionWithCause(msg, e);
    }
}

From source file:com.limegroup.gnutella.metadata.ASFParser.java

/**
 * Parses a value from an extended tag, assuming the value is of the 'string' dataType.
 *///from www.ja  va 2 s . c  o m
private void parseExtendedString(String field, int size, DataInputStream ds) throws IOException {
    byte[] data = new byte[Math.min(250, size)];
    ds.readFully(data);
    int leftover = Math.max(0, size - 250);
    IOUtils.ensureSkip(ds, leftover);
    String info = string(data);

    if (LOG.isDebugEnabled())
        LOG.debug("Parsing extended String.  field: " + field + ", Value: " + info);

    if (Extended.WM_TITLE.equals(field)) {
        if (_title == null)
            _title = info;
    } else if (Extended.WM_AUTHOR.equals(field)) {
        if (_artist == null)
            _artist = info;
    } else if (Extended.WM_ALBUMTITLE.equals(field)) {
        if (_album == null)
            _album = info;
    } else if (Extended.WM_TRACK_NUMBER.equals(field)) {
        if (_track == -1)
            _track = toShort(info);
    } else if (Extended.WM_YEAR.equals(field)) {
        if (_year == null)
            _year = info;
    } else if (Extended.WM_GENRE.equals(field)) {
        if (_genre == null)
            _genre = info;
    } else if (Extended.WM_DESCRIPTION.equals(field)) {
        if (_comment == null)
            _comment = info;
    }
}

From source file:ee.sk.digidoc.SignedDoc.java

/**
 * Reads in data file//w  w w.  ja  v a 2s. c  o  m
 * @param inFile input file
 */
public static byte[] readFile(File inFile) throws IOException, FileNotFoundException {
    byte[] data = null;
    FileInputStream is = new FileInputStream(inFile);
    DataInputStream dis = new DataInputStream(is);
    data = new byte[dis.available()];
    dis.readFully(data);
    dis.close();
    is.close();
    return data;
}

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

/**
 * Write a small binary value and return the data.
 *
 * @param out the output stream to write
 * @param size the size//from w ww .  ja v a 2  s . c om
 * @param blobVal the binary value
 * @param state the property state (for error messages)
 * @param i the index (for error messages)
 * @return the data
 * @throws IOException if the data could not be read
 */
private byte[] writeSmallBinary(DataOutputStream out, BLOBFileValue blobVal, NodePropBundle.PropertyEntry state,
        int i) throws IOException {
    int size = (int) blobVal.getLength();
    out.writeInt(size);
    byte[] data = new byte[size];
    try {
        DataInputStream in = new DataInputStream(blobVal.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size;
        log.error(msg, e);
        throw new IOException(msg);
    }
    out.write(data, 0, data.length);
    return data;
}