Example usage for java.io ByteArrayInputStream read

List of usage examples for java.io ByteArrayInputStream read

Introduction

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

Prototype

public synchronized int read() 

Source Link

Document

Reads the next byte of data from this input stream.

Usage

From source file:com.intuit.autumn.web.InputStreamHttpServletRequestWrapper.java

/**
 * Return wrapped intput stream/*from   w  ww  .  j a  va  2  s  . co  m*/
 *
 * @return invalid response stream
 * @throws IOException unintended exception
 */

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);

    return new ServletInputStream() {
        @Override
        public boolean isFinished() {
            return byteArrayInputStream.available() == 0;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
            throw new UnsupportedOperationException("Not implemented");
        }

        @Override
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }
    };
}

From source file:com.maverick.ssl.SSLHandshakeProtocol.java

private void onServerHelloMsg(byte[] msg) throws SSLException {

    try {/* www.  j  a v  a  2 s .  c  om*/
        ByteArrayInputStream in = new ByteArrayInputStream(msg);

        majorVersion = in.read();
        minorVersion = in.read();

        serverRandom = new byte[32];
        in.read(serverRandom);

        sessionID = new byte[(in.read() & 0xFF)];
        in.read(sessionID);

        cipherSuiteID = new SSLCipherSuiteID(in.read(), in.read());

        pendingCipherSuite = (SSLCipherSuite) context.getCipherSuiteClass(cipherSuiteID).newInstance();

        compressionID = in.read();

        currentHandshakeStep = SERVER_HELLO_MSG;
    } catch (IllegalAccessException ex) {
        throw new SSLException(SSLException.INTERNAL_ERROR,
                ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());

    } catch (InstantiationException ex) {
        throw new SSLException(SSLException.INTERNAL_ERROR,
                ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
    } catch (IOException ex) {
        throw new SSLException(SSLException.INTERNAL_ERROR,
                ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
    }

}

From source file:org.jzkit.a2j.codec.runtime.BERInputStream.java

public int[] oid_codec(Object instance, boolean is_constructed) throws java.io.IOException {
    // System.err.println("Decoding OID, length = "+next_length);

    int[] retval = new int[next_length + 1];
    byte[] decode_buffer = new byte[next_length];
    int pos = 2;/* w w w.ja va  2  s. c  om*/

    int bytes_left_to_read = next_length;
    int offset = 0;

    // We may need to call read repeatedly until we have all the data.
    while (bytes_left_to_read > 0) {
        int bytes_read = read(decode_buffer, offset, bytes_left_to_read);
        bytes_left_to_read -= bytes_read;
        offset += bytes_read;
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(decode_buffer);

    byte octet = (byte) bais.read();

    if (octet >= 80) {
        retval[0] = 2;
        retval[1] = octet - 80;
    } else if (octet >= 40) {
        retval[0] = 1;
        retval[1] = octet - 40;
    } else {
        retval[0] = 0;
        retval[1] = octet;
    }

    // Split first octet into first 2 elements of OID

    while (bais.available() > 0) {
        retval[pos++] = decodeBase128Int(bais);
    }

    int[] result = new int[pos];
    System.arraycopy(retval, 0, result, 0, pos);

    // debug("oid_codec returns "+result+" length="+next_length);

    return result;
}

From source file:org.apache.jackrabbit.oak.spi.blob.AbstractBlobStore.java

private void mark(ByteArrayInputStream idStream) throws Exception {
    while (true) {
        int type = idStream.read();
        if (type == -1) {
            return;
        } else if (type == TYPE_DATA) {
            int len = IOUtils.readVarInt(idStream);
            IOUtils.skipFully(idStream, len);
        } else if (type == TYPE_HASH) {
            int level = IOUtils.readVarInt(idStream);
            // level = 0: size (size of this block)
            // level > 0: total size (size of all sub-blocks)
            // (see class level javadoc for details)
            IOUtils.readVarLong(idStream);
            if (level > 0) {
                // block length (ignored)
                IOUtils.readVarLong(idStream);
            }/*from w  w  w  .  ja v a  2  s  . c  o  m*/
            byte[] digest = new byte[IOUtils.readVarInt(idStream)];
            IOUtils.readFully(idStream, digest, 0, digest.length);
            BlockId id = new BlockId(digest, 0);
            mark(id);
            if (level > 0) {
                byte[] block = readBlock(digest, 0);
                idStream = new ByteArrayInputStream(block);
                mark(idStream);
            }
        } else {
            throw new IOException("Unknown blobs id type " + type);
        }
    }
}

From source file:com.maverick.ssl.SSLHandshakeProtocol.java

private void onCertificateMsg(byte[] msg) throws SSLException {

    ByteArrayInputStream in = new ByteArrayInputStream(msg);

    // Get the length of the certificate chain
    int length2 = (in.read() & 0xFF) << 16 | (in.read() & 0xFF) << 8 | (in.read() & 0xFF);

    try {/*from   w  w  w .  ja v  a  2s. c om*/

        boolean trusted = false;

        X509Certificate chainCert;
        while (in.available() > 0 && !trusted) {
            // The length of the next certificate (we dont need this as rthe
            // DERInputStream does the work
            int certlen = (in.read() & 0xFF) << 16 | (in.read() & 0xFF) << 8 | (in.read() & 0xFF);

            // Now read the certificate
            DERInputStream der = new DERInputStream(in);

            ASN1Sequence certificate = (ASN1Sequence) der.readObject();

            // Get the x509 certificate structure
            chainCert = new X509Certificate(X509CertificateStructure.getInstance(certificate));

            if (x509 == null)
                x509 = chainCert;

            // Verify if this part of the chain is trusted
            try {
                trusted = context.getTrustedCACerts().isTrustedCertificate(chainCert,
                        context.isInvalidCertificateAllowed(), context.isUntrustedCertificateAllowed());
            } catch (SSLException ex1) {
                // #ifdef DEBUG
                log.warn(Messages.getString("SSLHandshakeProtocol.failedToVerifyCertAgainstTruststore"), ex1); //$NON-NLS-1$
                // #endif
            }
        }

        if (!trusted)
            throw new SSLException(SSLException.BAD_CERTIFICATE,
                    Messages.getString("SSLHandshakeProtocol.certInvalidOrUntrusted")); //$NON-NLS-1$

    } catch (IOException ex) {
        throw new SSLException(SSLException.INTERNAL_ERROR, ex.getMessage());
    }

    // #ifdef DEBUG
    log.debug(Messages.getString("SSLHandshakeProtocol.x509Cert")); //$NON-NLS-1$

    log.debug(Messages.getString("SSLHandshakeProtocol.x509Cert.subject") + x509.getSubjectDN()); //$NON-NLS-1$

    log.debug(Messages.getString("SSLHandshakeProtocol.x509Cert.issuer") + x509.getIssuerDN()); //$NON-NLS-1$
    // #endif

    currentHandshakeStep = CERTIFICATE_MSG;

}

From source file:org.apache.jackrabbit.oak.spi.blob.AbstractBlobStore.java

@Override
public int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws IOException {
    if (isMarkEnabled()) {
        mark(blobId);/*from  w  w w  .  j ava 2s  .  co m*/
    }
    byte[] id = StringUtils.convertHexToBytes(blobId);
    ByteArrayInputStream idStream = new ByteArrayInputStream(id);
    while (true) {
        int type = idStream.read();
        if (type == -1) {
            statsCollector.downloadCompleted(blobId);
            return -1;
        } else if (type == TYPE_DATA) {
            int len = IOUtils.readVarInt(idStream);
            if (pos < len) {
                IOUtils.skipFully(idStream, (int) pos);
                len -= pos;
                if (length < len) {
                    len = length;
                }
                IOUtils.readFully(idStream, buff, off, len);
                return len;
            }
            IOUtils.skipFully(idStream, len);
            pos -= len;
        } else if (type == TYPE_HASH) {
            int level = IOUtils.readVarInt(idStream);
            // level = 0: size (size of this block)
            // level > 0: total size (size of all sub-blocks)
            // (see class level javadoc for details)
            long totalLength = IOUtils.readVarLong(idStream);
            if (level > 0) {
                // block length (ignored)
                IOUtils.readVarLong(idStream);
            }
            byte[] digest = new byte[IOUtils.readVarInt(idStream)];
            IOUtils.readFully(idStream, digest, 0, digest.length);
            if (pos >= totalLength) {
                pos -= totalLength;
            } else {
                if (level > 0) {
                    byte[] block = readBlock(digest, 0);
                    idStream = new ByteArrayInputStream(block);
                } else {
                    long readPos = pos - pos % blockSize;
                    byte[] block = readBlock(digest, readPos);
                    ByteArrayInputStream in = new ByteArrayInputStream(block);
                    IOUtils.skipFully(in, pos - readPos);
                    return IOUtils.readFully(in, buff, off, length);
                }
            }
        } else {
            throw new IOException("Unknown blobs id type " + type + " for blob " + blobId);
        }
    }
}

From source file:org.apache.jackrabbit.oak.spi.blob.AbstractBlobStore.java

@Override
public long getBlobLength(String blobId) throws IOException {
    if (isMarkEnabled()) {
        mark(blobId);/*from   w w w.j  av  a2s.c o m*/
    }
    byte[] id = StringUtils.convertHexToBytes(blobId);
    ByteArrayInputStream idStream = new ByteArrayInputStream(id);
    long totalLength = 0;
    while (true) {
        int type = idStream.read();
        if (type == -1) {
            break;
        }
        if (type == TYPE_DATA) {
            int len = IOUtils.readVarInt(idStream);
            IOUtils.skipFully(idStream, len);
            totalLength += len;
        } else if (type == TYPE_HASH) {
            int level = IOUtils.readVarInt(idStream);
            // level = 0: size (size of this block)
            // level > 0: total size (size of all sub-blocks)
            // (see class level javadoc for details)                
            totalLength += IOUtils.readVarLong(idStream);
            if (level > 0) {
                // block length (ignored)
                IOUtils.readVarLong(idStream);
            }
            int digestLength = IOUtils.readVarInt(idStream);
            IOUtils.skipFully(idStream, digestLength);
        } else {
            throw new IOException("Datastore id type " + type + " for blob " + blobId);
        }
    }
    return totalLength;
}

From source file:it.eng.spagobi.tools.scheduler.dispatcher.MailDocumentDispatchChannel.java

private byte[] zipDocument(String fileZipName, byte[] content) {
    logger.debug("IN");

    ByteArrayOutputStream bos = null;
    ZipOutputStream zos = null;/*from ww w.  ja  v  a  2 s .  c o m*/
    ByteArrayInputStream in = null;
    try {

        bos = new ByteArrayOutputStream();
        zos = new ZipOutputStream(bos);
        ZipEntry ze = new ZipEntry(fileZipName);
        zos.putNextEntry(ze);
        in = new ByteArrayInputStream(content);

        for (int c = in.read(); c != -1; c = in.read()) {
            zos.write(c);
        }

        return bos.toByteArray();

    } catch (IOException ex) {
        logger.error("Error zipping the document", ex);
        return null;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                logger.error("Error closing output stream", e);
            }
        }
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
                logger.error("Error closing output stream", e);
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Error closing output stream", e);
            }
        }
    }

}

From source file:screenieup.UguuUpload.java

/**
 * Send the file to Uguu.// ww w.j  a v a2  s.  c  o  m
 * @param b the contents of the file in a byte array
 * @param conn the connection to use
 */
private void send(byte[] b, HttpURLConnection conn) {
    String first = String.format(
            "Content-Type: multipart/form-data; boundary=" + boundary + "\"\r\nContent-Length: 30721\r\n");
    String second = String
            .format("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n" + "100000000\r\n");
    String data = String.format("Content-Disposition: form-data; name=\"file\";filename=\"" + filename + "."
            + extension + "\"\r\nContent-type:" + tmpfiletype + "\r\n");
    String last = String.format("Content-Disposition: form-data; name=\"name\"");
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    System.out.println("Sending data...");
    DataOutputStream outstream;
    try {
        outstream = new DataOutputStream(conn.getOutputStream());
        outstream.writeBytes(first);
        outstream.writeBytes("\r\n");
        outstream.writeBytes("\r\n");

        outstream.writeBytes("--" + boundary);
        outstream.writeBytes(second);
        outstream.writeBytes("--" + boundary + "\r\n");

        outstream.writeBytes(data);
        outstream.writeBytes("\r\n");

        int i;
        while ((i = bais.read()) > -1) {
            outstream.write(i);
        }
        bais.close();
        outstream.writeBytes("\r\n");
        outstream.writeBytes("--" + boundary + "\r\n");
        outstream.writeBytes(last);
        outstream.writeBytes("\r\n");
        outstream.writeBytes("\r\n");
        outstream.writeBytes("\r\n");
        outstream.writeBytes("--" + boundary + "--");
        outstream.flush();
        outstream.close();
    } catch (IOException ex) {
        System.out.println("IOException in sendImage()");
    }
}

From source file:org.apache.falcon.resource.proxy.ExtensionManagerProxy.java

private HttpServletRequest getEntityStream(Entity entity, EntityType type, HttpServletRequest request)
        throws IOException, JAXBException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    type.getMarshaller().marshal(entity, baos);
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray());
    ServletInputStream servletInputStream = new ServletInputStream() {
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }/*from w  ww. j  a v a2 s .c o m*/
    };
    return getBufferedRequest(new HttpServletRequestInputStreamWrapper(request, servletInputStream));
}