Search in sources :

Example 16 with PipedOutputStream

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

the class OldPipedOutputStreamTest method test_connectLjava_io_PipedInputStream.

public void test_connectLjava_io_PipedInputStream() throws IOException {
    out = new PipedOutputStream();
    try {
        out.connect(new PipedInputStream());
    } catch (Exception e) {
        fail("Test 1: Unexpected exception when connecting: " + e.getLocalizedMessage());
    }
    try {
        out.write('B');
    } catch (IOException e) {
        fail("Test 2: Unexpected IOException when writing after connecting.");
    }
    try {
        out.connect(new PipedInputStream());
        fail("Test 3: IOException expected when reconnecting the stream.");
    } catch (IOException e) {
    // Expected.
    }
    try {
        out.connect(null);
        fail("Test 4: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 17 with PipedOutputStream

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

the class OldPipedOutputStreamTest method test_writeI.

public void test_writeI() throws IOException {
    out = new PipedOutputStream();
    try {
        out.write(42);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    rt = new Thread(reader = new PReader(out));
    rt.start();
    out.write('c');
    out.flush();
    assertEquals("Test 2: The byte read does not match the byte written. ", "c", reader.read(1));
    /* Test disabled due to incomplete implementation, see ticket #92.
        rt.interrupt();

        try {
            out.write(42);
            fail("Test 3: IOException expected.");
        } catch (IOException e) {
            // Expected.
        }
    }
*/
    reader.getReader().close();
    try {
        out.write(42);
        fail("Test 4: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException)

Example 18 with PipedOutputStream

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

the class OldPipedOutputStreamTest method test_flush.

public void test_flush() throws Exception {
    out = new PipedOutputStream();
    rt = new Thread(reader = new PReader(out));
    rt.start();
    out.write(testString.getBytes(), 0, 10);
    assertTrue("Test 1: Bytes have been written before flush.", reader.available() != 0);
    out.flush();
    assertEquals("Test 2: Flush failed. ", testString.substring(0, 10), reader.read(10));
}
Also used : PipedOutputStream(java.io.PipedOutputStream)

Example 19 with PipedOutputStream

use of java.io.PipedOutputStream in project felix by apache.

the class Closure method execute.

@SuppressWarnings("unchecked")
private Object execute(List<Object> values, Channel capturingOutput) throws Exception {
    if (null != values) {
        parmv = new ArrayList<>(values);
        parms = new ArgList(parmv);
    } else if (null != parent) {
        // inherit parent closure parameters
        parmv = parent.parmv != null ? new ArrayList<>(parent.parmv) : null;
        parms = parmv != null ? new ArgList(parmv) : null;
    } else {
        // inherit session parameters
        Object args = session.get("args");
        if (null != args && args instanceof List<?>) {
            parmv = new ArrayList<>((List<Object>) args);
            parms = new ArgList(parmv);
        }
    }
    Result last = null;
    Operator operator = null;
    for (Iterator<Executable> iterator = program.tokens().iterator(); iterator.hasNext(); ) {
        Operator prevOperator = operator;
        Executable executable = iterator.next();
        if (iterator.hasNext()) {
            operator = (Operator) iterator.next();
        } else {
            operator = null;
        }
        if (prevOperator != null) {
            if (Token.eq("&&", prevOperator)) {
                if (!last.isSuccess()) {
                    continue;
                }
            } else if (Token.eq("||", prevOperator)) {
                if (last.isSuccess()) {
                    continue;
                }
            }
        }
        Channel[] streams;
        boolean[] toclose = new boolean[10];
        if (Pipe.getCurrentPipe() != null) {
            streams = Pipe.getCurrentPipe().streams.clone();
        } else {
            streams = new Channel[10];
            System.arraycopy(session.channels, 0, streams, 0, 3);
        }
        if (capturingOutput != null) {
            streams[1] = capturingOutput;
            toclose[1] = true;
        }
        CommandSessionImpl.JobImpl job;
        if (executable instanceof Pipeline) {
            Pipeline pipeline = (Pipeline) executable;
            List<Executable> exec = pipeline.tokens();
            Token s = exec.get(0);
            Token e = exec.get(exec.size() - 1);
            Token t = program.subSequence(s.start - program.start, e.start + e.length - program.start);
            job = session().createJob(t);
            for (int i = 0; i < exec.size(); i++) {
                Statement ex = (Statement) exec.get(i);
                Operator op = i < exec.size() - 1 ? (Operator) exec.get(++i) : null;
                Channel[] nstreams;
                boolean[] ntoclose;
                boolean endOfPipe;
                if (i == exec.size() - 1) {
                    nstreams = streams;
                    ntoclose = toclose;
                    endOfPipe = true;
                } else if (Token.eq("|", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else if (Token.eq("|&", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = nstreams[2] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = ntoclose[2] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else {
                    throw new IllegalStateException("Unrecognized pipe operator: '" + op + "'");
                }
                Pipe pipe = new Pipe(this, job, ex, nstreams, ntoclose, endOfPipe);
                job.addPipe(pipe);
            }
        } else {
            job = session().createJob(executable);
            Pipe pipe = new Pipe(this, job, (Statement) executable, streams, toclose, true);
            job.addPipe(pipe);
        }
        // Start pipe in background
        if (operator != null && Token.eq("&", operator)) {
            job.start(Status.Background);
            last = new Result((Object) null);
        } else // Start in foreground and wait for results
        {
            last = job.start(Status.Foreground);
            if (last == null) {
                last = new Result((Object) null);
            } else if (last.exception != null) {
                throw last.exception;
            }
        }
    }
    return last == null ? null : last.result;
}
Also used : Operator(org.apache.felix.gogo.runtime.Parser.Operator) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Channel(java.nio.channels.Channel) ArrayList(java.util.ArrayList) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Result(org.apache.felix.gogo.runtime.Pipe.Result) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) ArrayList(java.util.ArrayList) List(java.util.List) Executable(org.apache.felix.gogo.runtime.Parser.Executable)

Example 20 with PipedOutputStream

use of java.io.PipedOutputStream in project sonarlint-core by SonarSource.

the class SettingsDownloaderTest method testFetchGlobalSettings.

@Test
public void testFetchGlobalSettings() throws Exception {
    SonarLintWsClient wsClient = WsClientTestUtils.createMock();
    ValuesWsResponse response = ValuesWsResponse.newBuilder().addSettings(Setting.newBuilder().setKey("sonar.core.treemap.colormetric").setValue("violations_density").setInherited(true)).addSettings(Setting.newBuilder().setKey("sonar.core.treemap.sizemetric").setValue("ncloc")).addSettings(Setting.newBuilder().setKey("views.servers").setValues(Values.newBuilder().addValues("135817900907501"))).build();
    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    response.writeTo(out);
    out.close();
    WsClientTestUtils.addResponse(wsClient, "/api/settings/values.protobuf", in);
    new SettingsDownloader(wsClient).fetchGlobalSettingsTo("6.3", destDir);
    GlobalProperties properties = ProtobufUtil.readFile(destDir.resolve(StoragePaths.PROPERTIES_PB), GlobalProperties.parser());
    assertThat(properties.getPropertiesMap()).containsOnly(entry("sonar.core.treemap.sizemetric", "ncloc"), entry("views.servers", "135817900907501"));
}
Also used : ValuesWsResponse(org.sonarqube.ws.Settings.ValuesWsResponse) GlobalProperties(org.sonarsource.sonarlint.core.proto.Sonarlint.GlobalProperties) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) SonarLintWsClient(org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient) Test(org.junit.Test)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)227 PipedInputStream (java.io.PipedInputStream)204 IOException (java.io.IOException)91 Test (org.junit.Test)55 InputStream (java.io.InputStream)28 OutputStream (java.io.OutputStream)24 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 PrintStream (java.io.PrintStream)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 InputStreamReader (java.io.InputStreamReader)16 DataInputStream (java.io.DataInputStream)14 DataOutputStream (java.io.DataOutputStream)14 BufferedReader (java.io.BufferedReader)13 Before (org.junit.Before)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ExecutorService (java.util.concurrent.ExecutorService)8 ArrayList (java.util.ArrayList)7