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:org.jopenray.util.PacketAnalyser.java

public static void decode(PrintStream out, byte[] udpData) throws IOException {
    boolean dump = false;
    ByteArrayInputStream bIn = new ByteArrayInputStream(udpData);
    int r = readInt16(bIn);
    if (r == 1) {
        out.println("===================================================================================");
    }/*from  w  w  w .  j  a  v  a 2s.  com*/
    out.print("Seq number:" + r);
    int flag = readInt16(bIn);
    out.print(" Flag:" + flag);
    int type = readInt16(bIn);
    out.print(" Type:" + type);
    int dir = readInt16(bIn);
    out.println(" Dir:" + dir + " dataSize:" + udpData.length);
    if (dir == 0) {
        // Server -> Sunray
        int a = readInt16(bIn);
        int b = readInt16(bIn);
        int c = readInt16(bIn);
        int d = readInt16(bIn);
        out.println("Server -> Sunray:" + a + "," + b + "," + c + "," + d);

        while (bIn.available() > 0) {
            String opCodeHeader = "";
            int opcode = bIn.read();
            opCodeHeader += "[ Opcode: " + opcode;
            int f = bIn.read();
            opCodeHeader += " Flag" + f;
            int oseq = readInt16(bIn);
            opCodeHeader += " OpcodeSeq:" + oseq;
            int x = readInt16(bIn);
            int y = readInt16(bIn);

            int w = readInt16(bIn);
            int h = readInt16(bIn);

            opCodeHeader += " x,y: " + x + "," + y + " w:" + w + " h:" + h + " ]";
            if (opcode != 0xB1) {
                return;
            }
            switch (opcode) {
            case 0x03:
                out.println("0x03 Strange opcode " + opCodeHeader);
                break;
            case 0xA1:
                int ap1 = bIn.read();
                int ap2 = bIn.read();
                int ap3 = bIn.read();
                int ap4 = bIn.read();
                out.println("0xA1:" + ap1 + "," + ap2 + "," + ap3 + "," + ap4 + opCodeHeader);
                break;
            case 0xA2:
                // out.println("FillRect");
                int a2p1 = bIn.read();
                int a2p2 = bIn.read();
                int a2p3 = bIn.read();
                int a2p4 = bIn.read();
                out.println("FillRect: Color:" + a2p1 + "," + a2p2 + "," + a2p3 + "," + a2p4 + opCodeHeader);
                break;
            case 0xA3: {

                int a3p1 = bIn.read();
                int a3p2 = bIn.read();
                int a3p3 = bIn.read();
                int a3p4 = bIn.read();
                int nbBytesPerRow = round(w, 8) / 8;
                int nbBytes = round(nbBytesPerRow * h, 4);
                byte[] unkuonw = new byte[nbBytes];
                try {
                    int lRead = bIn.read(unkuonw);
                    out.println("FillRectBitmap: Color:" + a3p1 + "," + a3p2 + "," + a3p3 + "," + a3p4
                            + " | bytes/row:" + nbBytesPerRow + "l:" + nbBytes + " lRead:" + lRead
                            + opCodeHeader);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            case 0xA4:
                int xsrc = readInt16(bIn);
                int ysrc = readInt16(bIn);
                out.println("CopyRect from : " + xsrc + "," + ysrc + opCodeHeader);

                break;
            case 0xA5: {
                // out.println("SetRectBitmap");
                // err.println("SetRectBitmap not yet implemented");
                try {
                    Color c1 = readColor(bIn);
                    Color c2 = readColor(bIn);
                    int nbBytesPerRow = round(w, 8) / 8;
                    int nbBytes = round(nbBytesPerRow * h, 4);
                    byte[] unkuonw = new byte[nbBytes];

                    int lRead = bIn.read(unkuonw);

                    out.println("SetRectBitmap: " + w + "x" + h + " at " + x + "," + y + " Color:" + c1 + " / "
                            + c2 + " | bytes/row:" + nbBytesPerRow + " l:" + nbBytes + " lRead:" + lRead
                            + opCodeHeader);
                    if (nbBytes > 1024) {
                        out.println("! data too long:" + nbBytes);
                    } else {
                        HexDump.dump(unkuonw, 0, System.err, 0);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    dump = true;
                }

                break;
            }
            case 0xA6: {

                out.println("SetRect:" + opCodeHeader);
                int nbBytesPerRow = round(w * 3, 4);
                int nbBytes = round(nbBytesPerRow * h, 4);
                // int nbBytes=w*h;
                if (nbBytes > 1000000) {
                    System.out.println("Bad length:" + nbBytes);
                } else {
                    byte[] colors = new byte[nbBytes];

                    int lRead = bIn.read(colors);
                    if (lRead != nbBytes) {
                        System.out.println("Bad length:" + nbBytes + " != " + lRead);
                    }
                    // colors contains colors (r,g,b)
                }
                break;
            }
            case 0xA8: {
                int xbound = readInt16(bIn);
                int ybound = readInt16(bIn);
                int wbound = readInt16(bIn);
                int hbound = readInt16(bIn);
                out.println("SetMouseBound to: " + xbound + "," + ybound + " w:" + wbound + " h:" + hbound + " "
                        + opCodeHeader + opCodeHeader);

                break;
            }
            case 0xA9: {

                Color c1 = readColor(bIn);
                Color c2 = readColor(bIn);
                out.println("SetMousePointer pos:" + x + "," + y + " size:" + w + "x" + h + " Color:" + c1
                        + " , " + c2 + opCodeHeader);
                int l = (w * h) / 8;
                byte[] b1 = new byte[l];
                bIn.read(b1);
                out.println("Bitmap");
                // printBits(w, h, b1);

                byte[] b2 = new byte[l];
                bIn.read(b2);
                out.println("Mask");
                // printBits(w, h, b2);
                break;
            }
            case 0xAA:
                int aap1 = bIn.read();
                int aap2 = bIn.read();
                int aap3 = bIn.read();
                int aap4 = bIn.read();
                out.println("SetMousePosition: " + aap1 + "," + aap2 + "," + aap3 + "," + aap4 + opCodeHeader);
                break;
            case 0xAB:
                int ab1 = readInt16(bIn);
                int ab2 = readInt16(bIn);
                out.println("SetKeyLock: " + ab1 + " " + ab2 + opCodeHeader);

                break;
            case 0xAC:

                int ac1 = readInt16(bIn);
                int ac2 = readInt16(bIn);
                int ac3 = readInt16(bIn);
                int ac4 = readInt16(bIn);
                out.println(
                        "0xAC : " + ac1 + " , " + ac2 + "," + ac3 + " , " + ac4 + opCodeHeader + opCodeHeader);
                break;
            case 0xAD:
                out.println("0xAD" + opCodeHeader);

                int l = readInt16(bIn);
                // l = (l & 0xfffc) + 2;

                out.println("l: " + l);
                out.println("(l & 0xfffc) + 2 :" + (l & 0xfffc) + 2);
                byte[] unkuonwn = new byte[l];
                dump = true;
                try {
                    int lRead = bIn.read(unkuonwn);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            case 0xAF: {

                int p1 = bIn.read();
                int p2 = bIn.read();
                int p3 = bIn.read();
                int p4 = bIn.read();
                for (int i = 0; i < 8; i++) {
                    bIn.read();
                }
                if (p1 != 255 && p2 != 255 && p3 != 255 && p4 != 255) {
                    out.println("PAD:" + p1 + "," + p2 + "," + p3 + "," + p4 + opCodeHeader);
                } else {
                    out.println("PAD " + opCodeHeader);
                }
                break;
            }
            case 0xB1:

                out.println("AUDIO:" + r + "|" + flag + "|" + type + "|" + dir + " l:" + udpData.length + " "
                        + opCodeHeader);
                outTest.print("AUDIO:" + r + "|" + flag + "|" + type + "|" + dir + " " + opCodeHeader);
                /*
                 * int xbound = readInt16(bIn); int ybound = readInt16(bIn);
                 * int wbound = readInt16(bIn); int hbound = readInt16(bIn);
                 * out.println(" to: " + xbound + "," + ybound + " w:" +
                 * wbound + " h:" + hbound + " " + opCodeHeader +
                 * opCodeHeader); dump=true;
                 */

                int v1 = 0;
                int v2 = 0;
                int totalv1et2 = 0;
                int bigTotal = 0;
                while (bIn.available() >= 0) {
                    int b1 = bIn.read();
                    int b2 = bIn.read();
                    if (b1 == -1 && b2 == -1) {
                        outTest.print(totalv1et2 + " : big total: " + bigTotal);
                        break;
                    }
                    soundOut.write(b2);
                    soundOut.write(b1);

                    if (b1 == 0x7F && b2 == 0xFF) {
                        v1++;
                        bigTotal++;
                        totalv1et2++;
                        if (v2 > 0)
                            out.println("v2=" + v2);
                        v2 = 0;
                    } else if (b1 == 0x80 && b2 == 0x01) {
                        v2++;
                        totalv1et2++;
                        bigTotal++;
                        if (v1 > 0)
                            out.println("v1=" + v1);
                        v1 = 0;
                    } else {
                        if (v2 > 0)
                            out.println("v2=" + v2);
                        if (v1 > 0)
                            out.println("v1=" + v1);
                        out.println("Unknwon:" + b1 + " et " + b2 + "[" + (b1 * 256 + b2) + "] total v1+v2:"
                                + totalv1et2);
                        if (totalv1et2 > 0)
                            outTest.print(totalv1et2 + ",");
                        v1 = 0;
                        v2 = 0;
                        totalv1et2 = 0;
                    }
                    /*
                     * bIn.read(); bIn.read(); for (int j = 0; j < 8; j++) {
                     * for (int i = 0; i < 12; i++) {
                     * 
                     * int aaa1 = bIn.read(); int aaa2 = bIn.read(); if (i %
                     * 2 == 0) { soundOut.write(aaa2); soundOut.write(aaa1);
                     * } } }
                     */

                }
                outTest.println();

                break;
            case 0xD1:
                out.println("0xD1 " + opCodeHeader + opCodeHeader);
                break;
            case 0xD8:
                out.println("0xD8 " + opCodeHeader + opCodeHeader);
                break;
            case 0xB0: {
                out.println("0xB0 " + opCodeHeader + opCodeHeader);
                int p1 = readInt16(bIn);

                int p2 = readInt16(bIn);
                int p3 = readInt16(bIn);
                int nb = readInt16(bIn);
                out.println(p1 + " ; " + p2 + " ; " + p3);
                for (int i = 0; i < nb; i++) {
                    int xx = readInt16(bIn);
                    int yy = readInt16(bIn);
                    int ww = readInt16(bIn);
                    int hh = readInt16(bIn);
                    out.println("[" + xx + "," + yy + " " + ww + "x" + hh + "]");
                }
                break;
            }
            case 0xB4: {
                // ??
                out.println("0xB4 " + opCodeHeader + opCodeHeader);

                for (int i = 0; i < 19; i++) {
                    int p1 = readInt16(bIn);
                    out.print(p1 + ",");
                }
                int end = readInt16(bIn);
                out.println(end);
                break;
            }
            case 0xB9: {
                // ??
                out.println("0xB9 " + opCodeHeader + opCodeHeader);
                break;
            }
            case 0xBF: {
                int le = readInt16(bIn);
                out.println("0xBF " + le + " bytes " + opCodeHeader);

                byte[] unknown = new byte[le];

                try {
                    int lRead = bIn.read(unknown);
                    if (lRead != le) {
                        out.println("Bad length:" + lRead + " / " + le);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            default:
                out.println("Unknown opcode:" + opcode + opCodeHeader);
                dump = true;
                break;
            }
        }
    } else if (dir == 2000) {

        // Sunray -> Server
        int a = readInt16(bIn);
        int b = readInt16(bIn);
        int c = readInt16(bIn);
        int d = readInt16(bIn);
        out.println("Sunray -> Server:" + a + "," + b + "," + c + "," + d);

        while (bIn.available() > 0) {
            int opcode = bIn.read();
            int hdat = readInt16(bIn);
            int idat = bIn.read();
            String opCodeHeader = "";

            opCodeHeader += "[ Opcode: " + opcode + " , " + hdat + " ," + idat + " ]";
            switch (opcode) {
            case 0:
                out.println("Empty (keep alive?)");
                break;
            case 0xc1:

                int jdat = readInt16(bIn);
                int shift = readInt16(bIn);
                // 6 octet
                int key1 = bIn.read();
                int key2 = bIn.read();
                int key3 = bIn.read();
                int key4 = bIn.read();
                int key5 = bIn.read();
                int key6 = bIn.read();

                //
                int mdat = readInt16(bIn);
                out.println("Keyboard " + opCodeHeader + " " + jdat + " shift:" + shift + " keys:(" + key1 + ","
                        + key2 + "," + key3 + "," + key4 + "," + key5 + "," + key6 + ") " + mdat);
                break;
            case 0xc2:
                int buttons = readInt16(bIn);
                int mouseX = readInt16(bIn);
                int mouseY = readInt16(bIn);
                int c2 = readInt16(bIn);
                out.println("Mouse" + opCodeHeader + " buttons:" + buttons + " (" + mouseX + "," + mouseY + ")"
                        + c2);
                break;
            case 0xc4:
                int c41 = readInt32(bIn);
                int c42 = readInt32(bIn);
                int c43 = readInt32(bIn);

                out.println("NACK " + c41 + "," + c42 + "," + c43);

                break;
            case 0xc5:
                int c51 = bIn.read();
                int c52 = bIn.read();
                int c53 = bIn.read();
                int c54 = bIn.read();

                out.println("0xC5 " + opCodeHeader + " " + c51 + "," + c52 + "," + c53 + "," + c54);
                break;
            case 0xc6:
                int dataLength = readInt16(bIn);
                int stringLength = bIn.read();
                byte[] string = new byte[stringLength];

                try {
                    int rL = bIn.read(string);
                    out.println(dataLength + " , " + stringLength + " readLength" + rL);
                    out.println("Firmware: " + new String(string));
                    // dump = true;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            case 0xc7: {
                int x1 = readInt16(bIn);
                int y1 = readInt16(bIn);
                int w1 = readInt16(bIn);
                int h1 = readInt16(bIn);
                int x2 = readInt16(bIn);
                int y2 = readInt16(bIn);
                int w2 = readInt16(bIn);
                int h2 = readInt16(bIn);
                int x3 = readInt16(bIn);
                int y3 = readInt16(bIn);
                int w3 = readInt16(bIn);
                int h3 = readInt16(bIn);
                out.println("Rect: " + opCodeHeader + " [" + x1 + "," + y1 + "," + w1 + "," + h1 + "][" + x2
                        + "," + y2 + "," + w2 + "," + h2 + "][" + x3 + "," + y3 + "," + w3 + "," + h3 + "]");

                break;

            }
            case 0xCA:
                int ca1 = readInt16(bIn);
                int ca2 = readInt16(bIn);
                int ca3 = readInt16(bIn);
                int ca4 = readInt16(bIn);
                int ca5 = readInt16(bIn);
                int ca6 = readInt16(bIn);

                out.println("0xCA " + opCodeHeader + " " + ca1 + "," + ca2 + "," + ca3 + "," + ca4 + "," + ca5
                        + "," + ca6);
                break;
            case 0xcb:
                int bc1 = bIn.read();
                int bc2 = readInt16(bIn);
                int bc3 = readInt16(bIn);
                int bc4 = readInt16(bIn);
                int bc5 = readInt16(bIn);
                int bc6 = readInt16(bIn);
                int bc7 = readInt16(bIn);
                int bc8 = readInt16(bIn);
                int bc9 = readInt16(bIn);
                int bc10 = readInt16(bIn);
                int bc11 = readInt16(bIn);
                out.println("0xCB " + opCodeHeader + " " + bc1 + "," + bc2 + "," + bc3 + "," + bc4 + "," + bc5
                        + "," + bc6 + "," + bc7 + "," + bc8 + "," + bc9 + "," + bc10 + "," + bc11);
                break;
            default:
                out.println("Unknown opcode: " + opCodeHeader);
                dump = true;
                break;
            }
        }
    } else {
        out.println("Unknown packet direction:" + dir);
        dump = true;
    }
    if (dump) {
        HexDump.dump(udpData, 0, System.err, 0);
    }
}

From source file:byps.test.TestRemoteStreams.java

/**
 * Send and receive a stream without content length information
 * //from  w w  w  .  java2s .co m
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testRemoteStreamsOneStreamChunked() throws InterruptedException, IOException {
    log.info("testRemoteStreamsOneStreamChunked(");

    String str = "hello";
    final ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
    InputStream istrm = new BContentStream() {

        @Override
        public long getContentLength() {
            return -1L;
        }

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

    };

    remote.setImage(istrm);

    InputStream istrmR = remote.getImage();
    ByteBuffer buf = BWire.bufferFromStream(istrmR);
    String strR = new String(buf.array(), buf.position(), buf.remaining());
    TestUtils.assertEquals(log, "stream", str, strR);

    remote.setImage(null);
    TestUtils.checkTempDirEmpty(client);

    log.info(")testRemoteStreamsOneStreamChunked");
}

From source file:com.sixt.service.framework.servicetest.mockservice.MockHttpServletRequest.java

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream stream = new ByteArrayInputStream(postBody);
    return new ServletInputStream() {
        @Override/*from   ww  w. j a v a  2 s  . c o  m*/
        public boolean isFinished() {
            return false;
        }

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

        @Override
        public void setReadListener(ReadListener readListener) {

        }

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

From source file:de.micromata.genome.tpsb.httpmockup.MockHttpServletRequest.java

@Override
public ServletInputStream getInputStream() throws IOException {
    if (requestData == null) {
        return null;
    }/*from  w ww  .ja  v  a  2 s  .  co  m*/
    final ByteArrayInputStream bis = new ByteArrayInputStream(requestData);
    return new ServletInputStream() {

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

        @Override
        public boolean isFinished() {
            return false;
        }

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

        @Override
        public void setReadListener(ReadListener readListener) {

        }
    };

}

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

void processMessage(byte[] fragment, int off, int len) throws SSLException {

    ByteArrayInputStream reader = new ByteArrayInputStream(fragment, off, len);

    // Update the handshake hashes
    updateHandshakeHashes(fragment);/*w  w w.  j a v a  2 s  . c o m*/

    while (reader.available() > 0 && !isComplete()) {

        int type = reader.read();

        int length = (reader.read() & 0xFF) << 16 | (reader.read() & 0xFF) << 8 | (reader.read() & 0xFF);

        // #ifdef DEBUG
        log.debug(MessageFormat.format(Messages.getString("SSLHandshakeProtocol.processingType"), //$NON-NLS-1$
                new Object[] { new Integer(type), new Long(length) }));
        // #endif

        byte[] msg = new byte[length];
        try {
            reader.read(msg);
        } catch (IOException ex) {
            throw new SSLException(SSLException.INTERNAL_ERROR,
                    ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
        }

        switch (type) {
        case HELLO_REQUEST_MSG:

            // #ifdef DEBUG
            log.debug(Messages.getString("SSLHandshakeProtocol.receivedHELLO")); //$NON-NLS-1$
            // #endif
            /**
             * If we receive a hello request then a handshake must be
             * re-negotiated. But ignore it if were already performing a
             * handshake operation
             */
            if (currentHandshakeStep == HANDSHAKE_PENDING_OR_COMPLETE) {
                startHandshake();
            }
            break;

        case SERVER_HELLO_MSG:

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

            if (currentHandshakeStep != CLIENT_HELLO_MSG) {
                throw new SSLException(SSLException.PROTOCOL_VIOLATION,
                        MessageFormat.format(
                                Messages.getString("SSLHandshakeProtocol.receivedUnexpectedServerHello"), //$NON-NLS-1$
                                new Object[] { new Integer(currentHandshakeStep) }));
            }

            onServerHelloMsg(msg);
            break;

        case CERTIFICATE_MSG:

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

            if (currentHandshakeStep != SERVER_HELLO_MSG) {
                throw new SSLException(SSLException.PROTOCOL_VIOLATION,
                        MessageFormat.format(
                                Messages.getString("SSLHandshakeProtocol.unexpectedCertificateMessageReceived"), //$NON-NLS-1$
                                new Object[] { new Integer(currentHandshakeStep) }));
            }
            onCertificateMsg(msg);
            break;

        case KEY_EXCHANGE_MSG:

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

            throw new SSLException(SSLException.UNSUPPORTED_OPERATION,
                    Messages.getString("SSLHandshakeProtocol.kexNotSupported")); //$NON-NLS-1$

        case CERTIFICATE_REQUEST_MSG:

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

            wantsClientAuth = true;
            break;

        case SERVER_HELLO_DONE_MSG:

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

            if (currentHandshakeStep != CERTIFICATE_MSG) {
                throw new SSLException(SSLException.PROTOCOL_VIOLATION,
                        MessageFormat.format(
                                Messages.getString("SSLHandshakeProtocol.unexpectedServerHelloDone"), //$NON-NLS-1$
                                new Object[] { new Integer(currentHandshakeStep) }));
            }

            if (wantsClientAuth) {

                // #ifdef DEBUG
                log.debug(Messages.getString("SSLHandshakeProtocol.sendingNoCert")); //$NON-NLS-1$
                // #endif
                socket.sendMessage(SSLTransportImpl.ALERT_PROTOCOL, new byte[] {
                        (byte) SSLTransportImpl.WARNING_ALERT, (byte) SSLException.NO_CERTIFICATE });
            }
            onServerHelloDoneMsg();
            break;

        case FINISHED_MSG:

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

            if (currentHandshakeStep != FINISHED_MSG) {
                throw new SSLException(SSLException.PROTOCOL_VIOLATION);
            }

            currentHandshakeStep = HANDSHAKE_PENDING_OR_COMPLETE;

            break;

        default:

        }
    }
}

From source file:org.kohsuke.stapler.RequestImplTest.java

@Test
public void test_mutipart_formdata() throws IOException, ServletException {
    final Stapler stapler = new Stapler();
    final byte[] buf = generateMultipartData();
    final ByteArrayInputStream is = new ByteArrayInputStream(buf);
    final MockRequest mockRequest = new MockRequest() {
        @Override/*from ww w . j  a  va 2s .  c  o  m*/
        public String getContentType() {
            return "multipart/form-data; boundary=mpboundary";
        }

        @Override
        public String getCharacterEncoding() {
            return "UTF-8";
        }

        @Override
        public int getContentLength() {
            return buf.length;
        }

        @Override
        public Enumeration getParameterNames() {
            return Collections.enumeration(Arrays.asList("p1"));
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            return new ServletInputStream() {
                @Override
                public int read() throws IOException {
                    return is.read();
                }

                @Override
                public int read(byte[] b) throws IOException {
                    return is.read(b);
                }

                @Override
                public int read(byte[] b, int off, int len) throws IOException {
                    return is.read(b, off, len);
                }
            };
        }
    };

    RequestImpl request = new RequestImpl(stapler, mockRequest, Collections.<AncestorImpl>emptyList(), null);

    // Check that we can get the Form Fields. See https://github.com/stapler/stapler/issues/52
    Assert.assertEquals("text1_val", request.getParameter("text1"));
    Assert.assertEquals("text2_val", request.getParameter("text2"));

    // Check that we can get the file
    FileItem fileItem = request.getFileItem("pomFile");
    Assert.assertNotNull(fileItem);

    // Check getParameterValues
    Assert.assertEquals("text1_val", request.getParameterValues("text1")[0]);

    // Check getParameterNames
    Assert.assertTrue(Collections.list(request.getParameterNames()).contains("p1"));
    Assert.assertTrue(Collections.list(request.getParameterNames()).contains("text1"));

    // Check getParameterMap
    Assert.assertTrue(request.getParameterMap().containsKey("text1"));
}

From source file:com.microsoft.azure.storage.blob.CloudBlobClientEncryptionTests.java

@Test
public void testBlockBlobValidateEncryption() throws InvalidKeyException, NoSuchAlgorithmException,
        NoSuchPaddingException, StorageException, IOException, InvalidAlgorithmParameterException,
        URISyntaxException, InterruptedException, ExecutionException {
    int size = 5 * 1024 * 1024;
    byte[] buffer = TestHelper.getRandomBuffer(size);

    CloudBlockBlob blob = container.getBlockBlobReference("blob1");

    // Create the Key to be used for wrapping.
    SymmetricKey aesKey = TestHelper.getSymmetricKey();

    // Create the encryption policy to be used for upload.
    BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy(aesKey, null);

    // Set the encryption policy on the request options.
    BlobRequestOptions uploadOptions = new BlobRequestOptions();
    uploadOptions.setEncryptionPolicy(uploadPolicy);

    // Upload the encrypted contents to the blob.
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
    blob.upload(stream, size, null, uploadOptions, null);

    // Encrypt locally.
    String metadata = blob.getMetadata().get(Constants.EncryptionConstants.BLOB_ENCRYPTION_DATA);
    BlobEncryptionData encryptionData = BlobEncryptionData.deserialize(metadata);

    Cipher myAes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParameterSpec = new IvParameterSpec(encryptionData.getContentEncryptionIV());

    byte[] contentEncryptionKey = aesKey.unwrapKeyAsync(encryptionData.getWrappedContentKey().getEncryptedKey(),
            encryptionData.getWrappedContentKey().getAlgorithm()).get();
    SecretKey keySpec = new SecretKeySpec(contentEncryptionKey, 0, contentEncryptionKey.length, "AES");

    myAes.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);

    CipherInputStream encryptedStream = new CipherInputStream(new ByteArrayInputStream(buffer), myAes);

    // Download the encrypted contents from the blob.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    blob.download(outputStream);/*from  w ww .  j  av a  2 s.  c  o m*/

    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (int i = 0; i < outputStream.size(); i++) {
        assertEquals(encryptedStream.read(), inputStream.read());
    }

    encryptedStream.close();
}

From source file:net.bull.javamelody.TestMonitoringFilter.java

private ServletInputStream createInputStreamForString(final String string) {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(string.getBytes());
    // CHECKSTYLE:OFF
    final ServletInputStream inputStream = new ServletInputStream() {
        // CHECKSTYLE:ON
        @Override/*from   ww  w.  j a  v  a2  s .com*/
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }

        @Override
        public boolean isFinished() {
            return false;
        }

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

        @Override
        public void setReadListener(ReadListener readListener) {
            // nothing
        }
    };
    return inputStream;
}

From source file:org.jopenray.server.thinclient.DisplayReaderThread.java

private void handlePacket(byte[] udpData, int l) {
    boolean dump = false;
    ByteArrayInputStream bIn = new ByteArrayInputStream(udpData, 0, l);
    int r = readInt16(bIn);
    int flag = readInt16(bIn);
    int type = readInt16(bIn);
    int dir = readInt16(bIn);
    if (DEBUG)//from   w  ww  .ja v a 2 s  .c o m
        System.out.print("Seq number:" + r + " Flag:" + flag + " Type:" + type + " Dir:" + dir);

    // Sunray ->Server
    int a = readInt16(bIn);
    int b = readInt16(bIn);
    int c = readInt16(bIn);
    int d = readInt16(bIn);
    if (DEBUG)
        System.out.println("Sunray -> Server:" + a + "," + b + "," + c + "," + d);
    if (bIn.available() == 0) {
        DisplayMessage m = new DisplayMessage(client.getWriter());
        m.addOperation(new PadOperation());
        this.client.getWriter().addMessage(m);
    } else
        while (bIn.available() > 0) {
            int opcode = bIn.read();
            int hdat = readInt16(bIn);
            int idat = bIn.read();
            String opCodeHeader = "";

            opCodeHeader += "[ Opcode: " + opcode + " , " + hdat + " ," + idat + " ]";

            switch (opcode) {
            case 0xc1:
                dump = true;
                int jdat = readInt16(bIn);
                int modifier = readInt16(bIn);
                // 6 octet
                int key1 = bIn.read();
                int key2 = bIn.read();
                int key3 = bIn.read();
                int key4 = bIn.read();
                int key5 = bIn.read();
                int key6 = bIn.read();

                //
                int mdat = readInt16(bIn);
                System.out.println("Keyboard " + opCodeHeader + " " + jdat + " modifier:" + modifier + " keys:("
                        + key1 + "," + key2 + "," + key3 + "," + key4 + "," + key5 + "," + key6 + ") " + mdat);

                /*
                 * if(lastPressed>0){
                 * System.out.println("Send Key Released:"+lastPressed);
                 * sendKeyReleased(lastPressed); lastPressed=-1; }
                 */

                if (key1 > 0) {
                    if (!keysPressed.contains(key1)) {

                        int hidToKeyCode = HID.hidToKeyCode(key1);
                        System.out.println(modifier + " : " + (modifier & 0x2));
                        sendKeyPressed(hidToKeyCode, (modifier & 2) != 0 || (modifier & 32) != 0,
                                (modifier & 1) != 0, (modifier & 4) != 0, (modifier & 8) != 0,
                                (modifier & 64) != 0);
                        keysPressed.add(key1);
                    }

                } else {
                    for (int i = 0; i < this.keysPressed.size(); i++) {
                        int k = keysPressed.get(i);
                        int hidToKeyCode = HID.hidToKeyCode(k);
                        sendKeyReleased(hidToKeyCode);

                    }
                    keysPressed.clear();
                }

                break;
            case 0xc2:
                int buttons = readInt16(bIn);
                int mouseX = readInt16(bIn);
                int mouseY = readInt16(bIn);
                int c2 = readInt16(bIn);
                if (DEBUG)
                    System.out.println("Mouse" + opCodeHeader + " buttons:" + buttons + " (" + mouseX + ","
                            + mouseY + ")" + c2);
                processMouseEvent(buttons, mouseX, mouseY);
                break;
            case 0xc4: {
                int c41 = readInt32(bIn);
                int c42 = readInt32(bIn);
                int c43 = readInt32(bIn);

                System.out.println("NACK  seq= " + c41 + "  type: " + c42 + " , " + c43);
                client.resend(c42, c43);
                break;
            }
            case 0xc5:
                int c51 = bIn.read();
                int c52 = bIn.read();
                int c53 = bIn.read();
                int c54 = bIn.read();

                System.out.println("0xC5 " + opCodeHeader + " " + c51 + "," + c52 + "," + c53 + "," + c54);
                break;
            case 0xc6:
                int dataLength = readInt16(bIn);
                int stringLength = bIn.read();
                byte[] string = new byte[stringLength];

                try {
                    int rL = bIn.read(string);
                    System.out.println(dataLength + " , " + stringLength + " readLength" + rL);
                    System.out.println("Firmware: " + new String(string));
                    // dump = true;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;

            case 0xc7: {
                int x1 = readInt16(bIn);
                int y1 = readInt16(bIn);
                int w1 = readInt16(bIn);
                int h1 = readInt16(bIn);
                int x2 = readInt16(bIn);
                int y2 = readInt16(bIn);
                int w2 = readInt16(bIn);
                int h2 = readInt16(bIn);
                int x3 = readInt16(bIn);
                int y3 = readInt16(bIn);
                int w3 = readInt16(bIn);
                int h3 = readInt16(bIn);
                if (DEBUG)
                    System.out.println("Rect: " + opCodeHeader + " [" + x1 + "," + y1 + "," + w1 + "," + h1
                            + "][" + x2 + "," + y2 + "," + w2 + "," + h2 + "][" + x3 + "," + y3 + "," + w3 + ","
                            + h3 + "]");

                break;
            }
            case 0xcb:
                int bc1 = bIn.read();
                int bc2 = readInt16(bIn);
                int bc3 = readInt16(bIn);
                int bc4 = readInt16(bIn);
                int bc5 = readInt16(bIn);
                int bc6 = readInt16(bIn);
                int bc7 = readInt16(bIn);
                int bc8 = readInt16(bIn);
                int bc9 = readInt16(bIn);
                int bc10 = readInt16(bIn);
                int bc11 = readInt16(bIn);
                if (DEBUG)
                    System.out.println(
                            "0xCB " + opCodeHeader + " " + bc1 + "," + bc2 + "," + bc3 + "," + bc4 + "," + bc5
                                    + "," + bc6 + "," + bc7 + "," + bc8 + "," + bc9 + "," + bc10 + "," + bc11);
                break;
            default:
                System.out.println("Unknown opcode: " + opCodeHeader);
                dump = true;
                break;
            }
        }
    if (dump) {
        try {
            HexDump.dump(udpData, 0, System.err, 0);
        } catch (Exception ignr) {
        }
    }

}

From source file:org.apache.tika.parser.rtf.RTFObjDataParser.java

/**
 * can return null if there is a linked object
 * instead of an embedded file/* w  w  w .  j  a  va2  s. co  m*/
 */
private byte[] handlePackage(byte[] pkgBytes, Metadata metadata) throws IOException, TikaException {
    //now parse the package header
    ByteArrayInputStream is = new ByteArrayInputStream(pkgBytes);
    readUShort(is);

    String displayName = readAnsiString(is);

    //should we add this to the metadata?
    readAnsiString(is); //iconFilePath
    try {
        //iconIndex
        EndianUtils.readUShortBE(is);
    } catch (EndianUtils.BufferUnderrunException e) {
        throw new IOException(e);
    }
    int type = readUShort(is); //type

    //1 is link, 3 is embedded object
    //this only handles embedded objects
    if (type != 3) {
        return null;
    }
    //should we really be ignoring this filePathLen?
    readUInt(is); //filePathLen

    String ansiFilePath = readAnsiString(is); //filePath
    long bytesLen = readUInt(is);
    byte[] objBytes = initByteArray(bytesLen);
    IOUtils.readFully(is, objBytes);
    StringBuilder unicodeFilePath = new StringBuilder();

    try {
        long unicodeLen = readUInt(is);

        for (int i = 0; i < unicodeLen; i++) {
            int lo = is.read();
            int hi = is.read();
            int sum = lo + 256 * hi;
            if (hi == -1 || lo == -1) {
                //stream ran out; empty SB and stop
                unicodeFilePath.setLength(0);
                break;
            }
            unicodeFilePath.append((char) sum);
        }
    } catch (IOException e) {
        //swallow; the unicode file path is optional and might not happen
        unicodeFilePath.setLength(0);
    }
    String fileNameToUse = "";
    String pathToUse = "";
    if (unicodeFilePath.length() > 0) {
        String p = unicodeFilePath.toString();
        fileNameToUse = p;
        pathToUse = p;
    } else {
        fileNameToUse = displayName == null ? "" : displayName;
        pathToUse = ansiFilePath == null ? "" : ansiFilePath;
    }
    metadata.set(TikaCoreProperties.ORIGINAL_RESOURCE_NAME, fileNameToUse);
    metadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(fileNameToUse));
    metadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, pathToUse);

    return objBytes;
}