Search in sources :

Example 66 with PipedOutputStream

use of java.io.PipedOutputStream in project cdap by caskdata.

the class ASMDatumCodecTest method testMap.

@Test
public void testMap() throws IOException, UnsupportedTypeException {
    TypeToken<Map<String, List<String>>> type = new TypeToken<Map<String, List<String>>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Map<String, List<String>>> writer = getWriter(type);
    ImmutableMap<String, List<String>> map = ImmutableMap.<String, List<String>>of("k1", Lists.newArrayList("v1"), "k2", Lists.newArrayList("v2", null));
    writer.encode(map, new BinaryEncoder(os));
    ReflectionDatumReader<Map<String, List<String>>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Assert.assertEquals(map, reader.read(new BinaryDecoder(is), getSchema(type)));
}
Also used : PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 67 with PipedOutputStream

use of java.io.PipedOutputStream in project cdap by caskdata.

the class ASMDatumCodecTest method testRecordArray.

@Test
public void testRecordArray() throws IOException, UnsupportedTypeException {
    TypeToken<Record[][]> type = new TypeToken<Record[][]>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Record[][]> writer = getWriter(type);
    Record[][] writeValue = new Record[][] { { new Record(10, "testing", ImmutableList.of("a", "b", "c"), TestEnum.VALUE2) } };
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<Record[][]> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Record[][] value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertArrayEquals(writeValue, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 68 with PipedOutputStream

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

the class JUnitLauncherTask method trySwitchSysOutErr.

private Optional<SwitchedStreamHandle> trySwitchSysOutErr(final TestRequest testRequest, final StreamType streamType) {
    switch(streamType) {
        case SYS_OUT:
            {
                if (!testRequest.interestedInSysOut()) {
                    return Optional.empty();
                }
                break;
            }
        case SYS_ERR:
            {
                if (!testRequest.interestedInSysErr()) {
                    return Optional.empty();
                }
                break;
            }
        default:
            {
                // and return back
                return Optional.empty();
            }
    }
    final PipedOutputStream pipedOutputStream = new PipedOutputStream();
    final PipedInputStream pipedInputStream;
    try {
        pipedInputStream = new PipedInputStream(pipedOutputStream);
    } catch (IOException ioe) {
        // log and return
        return Optional.empty();
    }
    final PrintStream printStream = new PrintStream(pipedOutputStream, true);
    final SysOutErrStreamReader streamer;
    switch(streamType) {
        case SYS_OUT:
            {
                System.setOut(new PrintStream(printStream));
                streamer = new SysOutErrStreamReader(this, pipedInputStream, StreamType.SYS_OUT, testRequest.getSysOutInterests());
                final Thread sysOutStreamer = new Thread(streamer);
                sysOutStreamer.setDaemon(true);
                sysOutStreamer.setName("junitlauncher-sysout-stream-reader");
                sysOutStreamer.setUncaughtExceptionHandler((t, e) -> this.log("Failed in sysout streaming", e, Project.MSG_INFO));
                sysOutStreamer.start();
                break;
            }
        case SYS_ERR:
            {
                System.setErr(new PrintStream(printStream));
                streamer = new SysOutErrStreamReader(this, pipedInputStream, StreamType.SYS_ERR, testRequest.getSysErrInterests());
                final Thread sysErrStreamer = new Thread(streamer);
                sysErrStreamer.setDaemon(true);
                sysErrStreamer.setName("junitlauncher-syserr-stream-reader");
                sysErrStreamer.setUncaughtExceptionHandler((t, e) -> this.log("Failed in syserr streaming", e, Project.MSG_INFO));
                sysErrStreamer.start();
                break;
            }
        default:
            {
                return Optional.empty();
            }
    }
    return Optional.of(new SwitchedStreamHandle(pipedOutputStream, streamer));
}
Also used : Arrays(java.util.Arrays) AntClassLoader(org.apache.tools.ant.AntClassLoader) Launcher(org.junit.platform.launcher.Launcher) TestPlan(org.junit.platform.launcher.TestPlan) Task(org.apache.tools.ant.Task) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) Path(org.apache.tools.ant.types.Path) ArrayList(java.util.ArrayList) TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) PipedInputStream(java.io.PipedInputStream) Project(org.apache.tools.ant.Project) LauncherFactory(org.junit.platform.launcher.core.LauncherFactory) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) FileUtils(org.apache.tools.ant.util.FileUtils) Properties(java.util.Properties) Files(java.nio.file.Files) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) Collection(java.util.Collection) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) PipedOutputStream(java.io.PipedOutputStream) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Paths(java.nio.file.Paths) Optional(java.util.Optional) Collections(java.util.Collections) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) InputStream(java.io.InputStream) PrintStream(java.io.PrintStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 69 with PipedOutputStream

use of java.io.PipedOutputStream in project tdq-studio-se by Talend.

the class ChartUtils method bufferedToDescriptorOptimized.

public static ImageDescriptor bufferedToDescriptorOptimized(final BufferedImage image) throws IOException {
    final PipedOutputStream output = new PipedOutputStream();
    final PipedInputStream pipedInputStream = new PipedInputStream();
    output.connect(pipedInputStream);
    try {
        new Thread() {

            @Override
            public void run() {
                try {
                    ChartUtilities.writeBufferedImageAsPNG(output, image, false, 0);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    } catch (RuntimeException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        }
    }
    ImageData img = new ImageData(pipedInputStream);
    ImageDescriptor descriptor = ImageDescriptor.createFromImageData(img);
    return descriptor;
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) PipedOutputStream(java.io.PipedOutputStream) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 70 with PipedOutputStream

use of java.io.PipedOutputStream in project cypher-for-gremlin by opencypher.

the class EmbeddedGremlinConsole method start.

public void start() {
    System.setProperty("plugins", "v3d3");
    PipedInputStream in = new PipedInputStream();
    replaceSystemIn(in);
    try {
        input = new PrintWriter(new PipedOutputStream(in));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    console = new Thread(() -> new Console(new IO(), new ArrayList<>(), true));
    console.start();
}
Also used : IO(org.codehaus.groovy.tools.shell.IO) Console(org.apache.tinkerpop.gremlin.console.Console) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)221 PipedInputStream (java.io.PipedInputStream)199 IOException (java.io.IOException)89 Test (org.junit.Test)54 InputStream (java.io.InputStream)30 OutputStream (java.io.OutputStream)23 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)9 ArrayList (java.util.ArrayList)7