Search in sources :

Example 81 with PipedInputStream

use of java.io.PipedInputStream in project robovm by robovm.

the class OldAndroidPipedStreamTest method testC.

public void testC() throws Exception {
    final PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    final byte[] readBytes = new byte[1024 * 2];
    assertEquals(0, in.available());
    TestThread reader, writer;
    reader = new TestThread() {

        @Override
        public void runTest() throws Exception {
            int ret;
            for (; ; ) {
                int nread = 0;
                while (nread < readBytes.length) {
                    ret = in.read(readBytes, nread, readBytes.length - nread);
                    if (ret == -1) {
                        return;
                    }
                    nread += ret;
                }
            }
        }
    };
    reader.start();
    writer = new TestThread() {

        Fibonacci fib = new Fibonacci();

        @Override
        public void runTest() throws Exception {
            byte[] writeBytes = new byte[1024 * 2];
            for (int i = 0; i < (writeBytes.length - 4); i += 4) {
                int toWrite = fib.next();
                writeBytes[i] = (byte) (toWrite >> 24);
                writeBytes[i + 1] = (byte) (toWrite >> 16);
                writeBytes[i + 2] = (byte) (toWrite >> 8);
                writeBytes[i + 3] = (byte) (toWrite);
            }
            out.write(writeBytes, 0, writeBytes.length);
            out.close();
        }
    };
    writer.start();
    for (; ; ) {
        try {
            reader.join(60 * 1000);
            writer.join(1000);
            break;
        } catch (InterruptedException ex) {
        }
    }
    if (reader.exception != null) {
        throw new Exception(reader.exception);
    }
    if (writer.exception != null) {
        throw new Exception(writer.exception);
    }
    Fibonacci fib = new Fibonacci();
    for (int i = 0; i < (readBytes.length - 4); i += 4) {
        int readInt = (((int) readBytes[i] & 0xff) << 24) | (((int) readBytes[i + 1] & 0xff) << 16) | (((int) readBytes[i + 2] & 0xff) << 8) | (((int) readBytes[i + 3] & 0xff));
        assertEquals("Error at " + i, readInt, fib.next());
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 82 with PipedInputStream

use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.

the class HttpChunkedResponseTest method thatChunkedContentIsChunked.

@org.junit.Test
public void thatChunkedContentIsChunked() throws Exception {
    PipedInputStream pipedInputStream = new ChunkedInputStream(new String[] { "some", "thing which is longer than sixteen characters", "whee!", "" });
    String[] expected = { "HTTP/1.1 200 OK", "Content-Type: what/ever", "Date: .*", "Connection: keep-alive", "Transfer-Encoding: chunked", "", "4", "some", "2d", "thing which is longer than sixteen characters", "5", "whee!", "0", "" };
    this.testServer.response = Response.newChunkedResponse(Status.OK, "what/ever", pipedInputStream);
    this.testServer.response.setChunkedTransfer(true);
    ByteArrayOutputStream byteArrayOutputStream = invokeServer("GET / HTTP/1.1");
    assertResponse(byteArrayOutputStream, expected);
}
Also used : PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 83 with PipedInputStream

use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.

the class TestCorsHttpServerWithSingleOrigin method setUp.

@BeforeClass
public static void setUp() throws Exception {
    stdIn = new PipedOutputStream();
    System.setIn(new PipedInputStream(stdIn));
    serverStartThread = new Thread(new Runnable() {

        @Override
        public void run() {
            String[] args = { "--host", "localhost", "--port", "9090", "--dir", "src/test/resources", "--cors=http://localhost:9090" };
            SimpleWebServer.main(args);
        }
    });
    serverStartThread.start();
    // give the server some tine to start.
    Thread.sleep(100);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BeforeClass(org.junit.BeforeClass)

Example 84 with PipedInputStream

use of java.io.PipedInputStream in project blueprints by tinkerpop.

the class GraphMigrator method migrateGraph.

/**
     * Pipe the data from one graph to another graph.
     *
     * @param fromGraph the graph to take data from
     * @param toGraph   the graph to take data to
     * @throws XMLStreamException thrown if the serialization process causes an exception
     * @throws IOException        thrown if there is an error in steam between the two graphs
     */
public static void migrateGraph(final Graph fromGraph, final Graph toGraph) throws XMLStreamException, IOException {
    final PipedInputStream inPipe = new PipedInputStream() {

        // Default is 1024
        protected static final int PIPE_SIZE = 1024;
    };
    final PipedOutputStream outPipe = new PipedOutputStream(inPipe) {

        public void close() throws IOException {
            while (inPipe.available() > 0) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            super.close();
        }
    };
    new Thread(new Runnable() {

        public void run() {
            try {
                GraphMLWriter.outputGraph(fromGraph, outPipe);
                outPipe.flush();
                outPipe.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    GraphMLReader.inputGraph(toGraph, inPipe);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 85 with PipedInputStream

use of java.io.PipedInputStream in project Gadgetbridge by Freeyourgadget.

the class PebbleIoThread method connect.

@Override
protected boolean connect() {
    String deviceAddress = gbDevice.getAddress();
    GBDevice.State originalState = gbDevice.getState();
    gbDevice.setState(GBDevice.State.CONNECTING);
    gbDevice.sendDeviceUpdateIntent(getContext());
    try {
        // contains only one ":"? then it is addr:port
        int firstColon = deviceAddress.indexOf(":");
        if (firstColon == deviceAddress.lastIndexOf(":")) {
            mIsTCP = true;
            InetAddress serverAddr = InetAddress.getByName(deviceAddress.substring(0, firstColon));
            mTCPSocket = new Socket(serverAddr, Integer.parseInt(deviceAddress.substring(firstColon + 1)));
            mInStream = mTCPSocket.getInputStream();
            mOutStream = mTCPSocket.getOutputStream();
        } else {
            mIsTCP = false;
            if (gbDevice.getVolatileAddress() != null && prefs.getBoolean("pebble_force_le", false)) {
                deviceAddress = gbDevice.getVolatileAddress();
            }
            BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(deviceAddress);
            if (btDevice.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                LOG.info("This is a Pebble 2 or Pebble-LE/Pebble Time LE, will use BLE");
                mInStream = new PipedInputStream();
                mOutStream = new PipedOutputStream();
                mPebbleLESupport = new PebbleLESupport(this.getContext(), btDevice, (PipedInputStream) mInStream, (PipedOutputStream) mOutStream);
            } else {
                ParcelUuid[] uuids = btDevice.getUuids();
                if (uuids == null) {
                    return false;
                }
                for (ParcelUuid uuid : uuids) {
                    LOG.info("found service UUID " + uuid);
                }
                mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
                mBtSocket.connect();
                mInStream = mBtSocket.getInputStream();
                mOutStream = mBtSocket.getOutputStream();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        gbDevice.setState(originalState);
        gbDevice.sendDeviceUpdateIntent(getContext());
        mInStream = null;
        mOutStream = null;
        mBtSocket = null;
        return false;
    }
    mPebbleProtocol.setForceProtocol(prefs.getBoolean("pebble_force_protocol", false));
    mIsConnected = true;
    write(mPebbleProtocol.encodeFirmwareVersionReq());
    gbDevice.setState(GBDevice.State.CONNECTED);
    gbDevice.sendDeviceUpdateIntent(getContext());
    return true;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothDevice(android.bluetooth.BluetoothDevice) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) PebbleLESupport(nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.ble.PebbleLESupport) InetAddress(java.net.InetAddress) BluetoothSocket(android.bluetooth.BluetoothSocket) Socket(java.net.Socket)

Aggregations

PipedInputStream (java.io.PipedInputStream)128 PipedOutputStream (java.io.PipedOutputStream)121 IOException (java.io.IOException)42 Test (org.junit.Test)33 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 DataInputStream (java.io.DataInputStream)13 DataOutputStream (java.io.DataOutputStream)13 InputStream (java.io.InputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Before (org.junit.Before)10 OutputStream (java.io.OutputStream)9 PrintStream (java.io.PrintStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5