Example usage for java.io ByteArrayInputStream available

List of usage examples for java.io ByteArrayInputStream available

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream available.

Prototype

public synchronized int available() 

Source Link

Document

Returns the number of remaining bytes that can be read (or skipped over) from this input stream.

Usage

From source file:pl.otros.logview.api.io.Utils.java

public static boolean checkIfIsGzipped(FileObject fileObject) throws IOException {
    boolean gziped = false;
    if (fileObject.getContent().getSize() == 0) {
        LOGGER.debug("File object " + fileObject.getName() + " is empty, can't detect gzip compression");
        return false;
    }/* w w w . ja v a 2  s  .  c  om*/
    InputStream inputStream = fileObject.getContent().getInputStream();
    byte[] loadProbe = loadProbe(inputStream, GZIP_CHECK_BUFFER_SIZE);
    // IOUtils.closeQuietly(inputStream);
    if (loadProbe.length < GZIP_MIN_SIZE) {
        LOGGER.info("Loaded probe is too small to check if it is gziped");
        return false;
    }
    try {
        ByteArrayInputStream bin = new ByteArrayInputStream(loadProbe);
        int available = bin.available();
        byte[] b = new byte[available < GZIP_CHECK_BUFFER_SIZE ? available : GZIP_CHECK_BUFFER_SIZE];
        int read = bin.read(b);
        gziped = checkIfIsGzipped(b, read);
    } catch (IOException e) {
        // Not gziped
        LOGGER.debug(fileObject.getName() + " is not gzip");
    }

    return gziped;
}

From source file:org.wso2.carbon.apimgt.rest.api.publisher.utils.CertificateRestApiUtils.java

/**
 * To get the decoded certificate input stream.
 *
 * @param certificate Relevant encoded certificate
 * @return Input stream of the certificate.
 * @throws APIManagementException API Management Exception.
 *///from w w w .  jav  a  2s.  com
public static ByteArrayInputStream getDecodedCertificate(String certificate) throws APIManagementException {
    byte[] cert = (Base64.decodeBase64(certificate.getBytes(StandardCharsets.UTF_8)));
    ByteArrayInputStream serverCert = new ByteArrayInputStream(cert);
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        if (serverCert.available() > 0) {
            Certificate generatedCertificate = cf.generateCertificate(serverCert);
            X509Certificate x509Certificate = (X509Certificate) generatedCertificate;
            return new ByteArrayInputStream(x509Certificate.getEncoded());
        }
    } catch (CertificateException e) {
        throw new APIManagementException("Error while decoding the certificate", e);
    }
    return null;
}

From source file:eu.openanalytics.rsb.component.JobProcessor.java

private static void uploadPropertiesToR(final RServi rServi, final Map<String, Serializable> metas,
        final Set<String> filesUploadedToR) throws CoreException, IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Properties properties = new Properties();
    for (final Entry<String, Serializable> meta : metas.entrySet()) {
        properties.setProperty(meta.getKey(), meta.getValue().toString());
    }/*from   w  ww. j a  va2s . co m*/
    properties.store(baos, null);
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    rServi.uploadFile(bais, bais.available(), Constants.MULTIPLE_FILES_JOB_CONFIGURATION, 0, null);
    filesUploadedToR.add(Constants.MULTIPLE_FILES_JOB_CONFIGURATION);
}

From source file:pl.otros.logview.io.Utils.java

public static boolean checkIfIsGzipped(byte[] buffer, int lenght) throws IOException {
    boolean gziped = false;
    try {//from ww  w  .  ja v a2s. c om
        ByteArrayInputStream bin = new ByteArrayInputStream(buffer, 0, lenght);
        GZIPInputStream gzipInputStream = new GZIPInputStream(bin);
        gzipInputStream.read(new byte[buffer.length], 0, bin.available());
        gziped = true;
    } catch (IOException e) {
        gziped = false;
    }
    return gziped;
}

From source file:pl.otros.logview.api.io.Utils.java

public static boolean checkIfIsGzipped(byte[] buffer, int lenght) throws IOException {
    boolean gziped;
    try {// w  w w.  jav  a 2s .c  o  m
        ByteArrayInputStream bin = new ByteArrayInputStream(buffer, 0, lenght);
        GZIPInputStream gzipInputStream = new GZIPInputStream(bin);
        gzipInputStream.read(new byte[buffer.length], 0, bin.available());
        gziped = true;
    } catch (IOException e) {
        gziped = false;
    }
    return gziped;
}

From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormatTest.java

private static byte[] getTaredText(String entryName) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarArchiveOutputStream tos = new TarArchiveOutputStream(baos);
    try {// w  w  w.  j  av a 2 s. co m
        TarArchiveEntry entry = new TarArchiveEntry(entryName);
        entry.setSize(bais.available());
        tos.putArchiveEntry(entry);
        IOHelper.copy(bais, tos);
    } finally {
        tos.closeArchiveEntry();
        IOHelper.close(bais, tos);
    }
    return baos.toByteArray();
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

private static byte[] read(ByteArrayInputStream bais) throws IOException {
    byte[] array = new byte[bais.available()];
    bais.read(array);// ww w .j  a  va  2  s .com

    return array;
}

From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java

/**
 * Method used to get the list of TLV inside the parameter tag specified in parameter
 * /*from   w ww . j a va2s  .co  m*/
 * @param pData
 *            data to parse
 * @param pTag
 *            tag to find
 * @param pAdd
 * @return the list of TLV tag inside
 */
public static List<TLV> getlistTLV(final byte[] pData, final ITag pTag, final boolean pAdd) {

    List<TLV> list = new ArrayList<TLV>();

    ByteArrayInputStream stream = new ByteArrayInputStream(pData);

    while (stream.available() > 0) {

        TLV tlv = TlvUtil.getNextTLV(stream);
        if (pAdd) {
            list.add(tlv);
        } else if (tlv.getTag().isConstructed()) {
            list.addAll(TlvUtil.getlistTLV(tlv.getValueBytes(), pTag, tlv.getTag() == pTag));
        }
    }

    return list;
}

From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java

/**
 * Method used to get the list of TLV corresponding to tags specified in parameters
 * /*  w w w. j  a v a 2 s. co m*/
 * @param pData
 *            data to parse
 * @param pTag
 *            tags to find
 * @param pAdd
 * @return the list of TLV
 */
public static List<TLV> getlistTLV(final byte[] pData, final ITag... pTag) {

    List<TLV> list = new ArrayList<TLV>();

    ByteArrayInputStream stream = new ByteArrayInputStream(pData);

    while (stream.available() > 0) {

        TLV tlv = TlvUtil.getNextTLV(stream);
        if (ArrayUtils.contains(pTag, tlv.getTag())) {
            list.add(tlv);
        } else if (tlv.getTag().isConstructed()) {
            list.addAll(TlvUtil.getlistTLV(tlv.getValueBytes(), pTag));
        }
    }

    return list;
}

From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java

/**
 * Method used to parser Tag and length/* w w  w  . j a va2s. c o  m*/
 * 
 * @param data
 *            data to parse
 * @return list of tag and length
 */
public static List<TagAndLength> parseTagAndLength(final byte[] data) {
    List<TagAndLength> tagAndLengthList = new ArrayList<TagAndLength>();
    if (data != null) {
        ByteArrayInputStream stream = new ByteArrayInputStream(data);

        while (stream.available() > 0) {
            if (stream.available() < 2) {
                throw new TlvException("Data length < 2 : " + stream.available());
            }

            ITag tag = searchTagById(TlvUtil.readTagIdBytes(stream));
            int tagValueLength = TlvUtil.readTagLength(stream);

            tagAndLengthList.add(new TagAndLength(tag, tagValueLength));
        }
    }
    return tagAndLengthList;
}