Example usage for java.io PipedOutputStream PipedOutputStream

List of usage examples for java.io PipedOutputStream PipedOutputStream

Introduction

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

Prototype

public PipedOutputStream(PipedInputStream snk) throws IOException 

Source Link

Document

Creates a piped output stream connected to the specified piped input stream.

Usage

From source file:com.streamsets.pipeline.lib.jdbc.JdbcLoadRecordWriter.java

@Override
public List<OnRecordErrorException> writeBatch(Iterator<Record> recordIterator) throws StageException {
    final List<OnRecordErrorException> errorRecords = new LinkedList<>();
    if (!recordIterator.hasNext()) {
        return errorRecords;
    }/*from w w w .  j  a  v  a2 s .  c  o m*/

    // Assume all records have the same columns.
    final Record first = recordIterator.next();
    SortedMap<String, String> columnsToParameters = recordReader.getColumnsToParameters(first,
            OperationType.LOAD_CODE, getColumnsToParameters(), getColumnsToFields());
    if (columnsToParameters.isEmpty()) {
        throw new StageException(JdbcErrors.JDBC_22);
    }

    final Set<String> columnNames = columnsToParameters.keySet();
    final String loadSql = "LOAD DATA LOCAL INFILE '' " + duplicateKeyAction.getKeyword() + " INTO TABLE "
            + getTableName() + " (" + Joiner.on(", ").join(columnNames) + ")";
    try (Connection connection = getDataSource().getConnection()) {
        Connection conn = connection.unwrap(Connection.class);
        try (PreparedStatement statement = conn.prepareStatement(loadSql)) {
            PipedInputStream is = new PipedInputStream();
            PipedOutputStream os = new PipedOutputStream(is);
            statement.getClass().getMethod("setLocalInfileInputStream", InputStream.class).invoke(statement,
                    is);

            Future<?> future = loadOutputExecutor.submit(() -> {
                try (OutputStreamWriter writer = new OutputStreamWriter(os)) {
                    CSVPrinter printer = new CSVPrinter(writer, CSVFormat.MYSQL);
                    Record record = first;
                    while (record != null) {
                        int opCode = getOperationCode(record, errorRecords);
                        if (opCode == OperationType.LOAD_CODE) {
                            for (String column : columnNames) {
                                Field field = record.get(getColumnsToFields().get(column));
                                printer.print(field.getValue());
                            }
                            printer.println();
                        } else if (opCode > 0) {
                            LOG.debug("Sending record to error due to unsupported operation {}", opCode);
                            errorRecords.add(new OnRecordErrorException(record, JdbcErrors.JDBC_70, opCode));
                        } else {
                            // It should be added to the error records.
                        }
                        record = recordIterator.hasNext() ? recordIterator.next() : null;
                    }
                    ;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });

            if (LOG.isDebugEnabled()) {
                LOG.debug("Executing query: {}", statement.toString());
            }
            statement.execute();
            future.get();
        }
        connection.commit();
    } catch (SQLException e) {
        handleSqlException(e);
    } catch (Exception e) {
        throw new StageException(JdbcErrors.JDBC_14, e.getMessage(), e);
    }
    return errorRecords;
}

From source file:eu.esdihumboldt.hale.io.wfs.AbstractWFSWriter.java

@Override
public IOReport execute(ProgressIndicator progress) throws IOProviderConfigurationException, IOException {
    progress.begin("WFS Transaction", ProgressIndicator.UNKNOWN);

    // configure internal provider
    internalProvider.setDocumentWrapper(createTransaction());

    final PipedInputStream pIn = new PipedInputStream();
    PipedOutputStream pOut = new PipedOutputStream(pIn);
    currentExecuteStream = pOut;/* w  w w.  ja  v  a2 s  . c  o  m*/

    Future<Response> futureResponse = null;
    IOReporter reporter = createReporter();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        // read the stream (in another thread)
        futureResponse = executor.submit(new Callable<Response>() {

            @Override
            public Response call() throws Exception {

                Proxy proxy = ProxyUtil.findProxy(targetWfs.getLocation());
                Request request = Request.Post(targetWfs.getLocation()).bodyStream(pIn,
                        ContentType.APPLICATION_XML);
                Executor executor = FluentProxyUtil.setProxy(request, proxy);

                // authentication
                String user = getParameter(PARAM_USER).as(String.class);
                String password = getParameter(PARAM_PASSWORD).as(String.class);

                if (user != null) {
                    // target host
                    int port = targetWfs.getLocation().getPort();
                    String hostName = targetWfs.getLocation().getHost();
                    String scheme = targetWfs.getLocation().getScheme();
                    HttpHost host = new HttpHost(hostName, port, scheme);

                    // add credentials
                    Credentials cred = ClientProxyUtil.createCredentials(user, password);
                    executor.auth(new AuthScope(host), cred);
                    executor.authPreemptive(host);
                }

                try {
                    return executor.execute(request);
                } finally {
                    pIn.close();
                }
            }
        });

        // write the stream
        SubtaskProgressIndicator subprogress = new SubtaskProgressIndicator(progress);
        reporter = (IOReporter) super.execute(subprogress);
    } finally {
        executor.shutdown();
    }

    try {
        Response response = futureResponse.get();
        HttpResponse res = response.returnResponse();
        int statusCode = res.getStatusLine().getStatusCode();
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        if (statusCode >= 200 && statusCode < 300) {
            // success
            reporter.setSuccess(reporter.isSuccess());

            // construct summary from response
            try {
                Document responseDoc = parseResponse(res.getEntity());

                // totalInserted
                String inserted = xpath.compile("//TransactionSummary/totalInserted").evaluate(responseDoc);
                // XXX totalUpdated
                // XXX totalReplaced
                // XXX totalDeleted
                reporter.setSummary("Inserted " + inserted + " features.");
            } catch (XPathExpressionException e) {
                log.error("Error in XPath used to evaluate service response");
            } catch (ParserConfigurationException | SAXException e) {
                reporter.error(new IOMessageImpl(MessageFormat.format(
                        "Server returned status code {0}, but could not parse server response", statusCode),
                        e));
                reporter.setSuccess(false);
            }
        } else {
            // failure
            reporter.error(
                    new IOMessageImpl("Server reported failure with code " + res.getStatusLine().getStatusCode()
                            + ": " + res.getStatusLine().getReasonPhrase(), null));
            reporter.setSuccess(false);

            try {
                Document responseDoc = parseResponse(res.getEntity());
                String errorText = xpath.compile("//ExceptionText/text()").evaluate(responseDoc);
                reporter.setSummary("Request failed: " + errorText);
            } catch (XPathExpressionException e) {
                log.error("Error in XPath used to evaluate service response");
            } catch (ParserConfigurationException | SAXException e) {
                reporter.error(new IOMessageImpl("Could not parse server response", e));
                reporter.setSuccess(false);
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        reporter.error(new IOMessageImpl("Failed to execute WFS-T request", e));
        reporter.setSuccess(false);
    }

    progress.end();

    return reporter;
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

public boolean removePage(String pageUuid) throws IOException {
    boolean removed = false;
    String sitemapIndex = metadata.getSitemapIndex(pageUuid);
    if (sitemapIndex == null) {
        log.warn("Page {} does not exist in sitemap", pageUuid);
        return removed;
    }/* w  w  w . j ava 2 s.  c  om*/

    String sitemapZip = SITEMAP_NAME_PREFIX + sitemapIndex + ".xml.gz";
    File sizemapZipFile = new File(mapResourcesRoot.getFile(), sitemapZip);
    if (!sizemapZipFile.exists()) {
        throw new IOException("Remove pageUuid " + pageUuid + " from sitemap failed becuase sitemap not found");
    }

    PipedInputStream bis = new PipedInputStream();
    PipedOutputStream bos = new PipedOutputStream(bis);
    InputStream zipfile = new FileInputStream(sizemapZipFile);
    GZIPInputStream gzipstream = new GZIPInputStream(zipfile);
    byte[] bytes = new byte[1024 * 1000];
    int len = 0;
    while ((len = gzipstream.read(bytes)) > 0) {
        bos.write(bytes, 0, len);
    }

    IOUtils.closeQuietly(zipfile);
    IOUtils.closeQuietly(gzipstream);

    String pageUrl = metadata.getPageUrl(pageUuid);
    String body = IOUtils.toString(bis);
    int loc = body.indexOf("<loc>" + pageUrl + "</loc>");
    if (loc != -1) {
        int start = StringUtils.lastIndexOf(body, "<url>", loc);
        int end = StringUtils.indexOf(body, "</url>", loc);
        if (start != -1 && end != -1) {
            //remove this URL
            body = StringUtils.substring(body, start, end + 6);
            zipToFile(sizemapZipFile, body.getBytes());
            removed = true;
        }
    }

    metadata.removePageMap(pageUuid);

    return removed;
}

From source file:org.waarp.openr66.context.task.ExecOutputTask.java

@Override
public void run() {
    /*/*from  w  ww.j a  v a2  s . com*/
     * First apply all replacements and format to argRule from context and argTransfer. Will
     * call exec (from first element of resulting string) with arguments as the following value
     * from the replacements. Return 0 if OK, else 1 for a warning else as an error. In case of
     * an error (> 0), all the line from output will be send back to the partner with the Error
     * code. No change is made to the file.
     */
    logger.info("ExecOutput with " + argRule + ":" + argTransfer + " and {}", session);
    String finalname = argRule;
    finalname = getReplacedValue(finalname, argTransfer.split(" "));
    // Force the WaitForValidation
    waitForValidation = true;
    if (Configuration.configuration.isUseLocalExec() && useLocalExec) {
        LocalExecClient localExecClient = new LocalExecClient();
        if (localExecClient.connect()) {
            localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion);
            LocalExecResult result = localExecClient.getLocalExecResult();
            finalize(result.getStatus(), result.getResult(), finalname);
            localExecClient.disconnect();
            return;
        } // else continue
    }
    String[] args = finalname.split(" ");
    File exec = new File(args[0]);
    if (exec.isAbsolute()) {
        if (!exec.canExecute()) {
            logger.error("Exec command is not executable: " + finalname);
            R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner());
            futureCompletion.setResult(result);
            futureCompletion.cancel();
            return;
        }
    }
    CommandLine commandLine = new CommandLine(args[0]);
    for (int i = 1; i < args.length; i++) {
        commandLine.addArgument(args[i]);
    }
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    PipedInputStream inputStream = new PipedInputStream();
    PipedOutputStream outputStream = null;
    try {
        outputStream = new PipedOutputStream(inputStream);
    } catch (IOException e1) {
        try {
            inputStream.close();
        } catch (IOException e) {
        }
        logger.error("Exception: " + e1.getMessage() + " Exec in error with " + commandLine.toString(), e1);
        futureCompletion.setFailure(e1);
        return;
    }
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, null);
    defaultExecutor.setStreamHandler(pumpStreamHandler);
    int[] correctValues = { 0, 1 };
    defaultExecutor.setExitValues(correctValues);
    ExecuteWatchdog watchdog = null;
    if (delay > 0) {
        watchdog = new ExecuteWatchdog(delay);
        defaultExecutor.setWatchdog(watchdog);
    }
    AllLineReader allLineReader = new AllLineReader(inputStream);
    Thread thread = new Thread(allLineReader, "ExecRename" + session.getRunner().getSpecialId());
    thread.setDaemon(true);
    Configuration.configuration.getExecutorService().execute(thread);
    int status = -1;
    try {
        status = defaultExecutor.execute(commandLine);
    } catch (ExecuteException e) {
        if (e.getExitValue() == -559038737) {
            // Cannot run immediately so retry once
            try {
                Thread.sleep(Configuration.RETRYINMS);
            } catch (InterruptedException e1) {
            }
            try {
                status = defaultExecutor.execute(commandLine);
            } catch (ExecuteException e1) {
                finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status,
                        commandLine);
                return;
            } catch (IOException e1) {
                try {
                    outputStream.flush();
                } catch (IOException e2) {
                }
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                thread.interrupt();
                try {
                    inputStream.close();
                } catch (IOException e2) {
                }
                try {
                    pumpStreamHandler.stop();
                } catch (IOException e2) {
                }
                logger.error(
                        "IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
                futureCompletion.setFailure(e);
                return;
            }
        } else {
            finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status,
                    commandLine);
            return;
        }
    } catch (IOException e) {
        try {
            outputStream.close();
        } catch (IOException e1) {
        }
        thread.interrupt();
        try {
            inputStream.close();
        } catch (IOException e1) {
        }
        try {
            pumpStreamHandler.stop();
        } catch (IOException e2) {
        }
        logger.error("IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
        futureCompletion.setFailure(e);
        return;
    }
    try {
        outputStream.flush();
    } catch (IOException e) {
    }
    try {
        outputStream.close();
    } catch (IOException e) {
    }
    try {
        pumpStreamHandler.stop();
    } catch (IOException e2) {
    }
    try {
        if (delay > 0) {
            thread.join(delay);
        } else {
            thread.join();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    try {
        inputStream.close();
    } catch (IOException e1) {
    }
    String newname = null;
    if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) {
        // kill by the watchdoc (time out)
        status = -1;
        newname = "TimeOut";
    } else {
        newname = allLineReader.getLastLine().toString();
    }
    finalize(status, newname, commandLine.toString());
}

From source file:org.springframework.integration.ip.tcp.connection.TcpNetConnectionTests.java

@Test
public void transferHeaders() throws Exception {
    Socket inSocket = mock(Socket.class);
    PipedInputStream pipe = new PipedInputStream();
    when(inSocket.getInputStream()).thenReturn(pipe);

    TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, nullPublisher, null);
    inboundConnection.setDeserializer(new MapJsonSerializer());
    MapMessageConverter inConverter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter);
    inboundConnection.setMapper(inMapper);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Socket outSocket = mock(Socket.class);
    TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, nullPublisher, null);
    when(outSocket.getOutputStream()).thenReturn(baos);

    MapMessageConverter outConverter = new MapMessageConverter();
    outConverter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter);
    outboundConnection.setMapper(outMapper);
    outboundConnection.setSerializer(new MapJsonSerializer());

    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    outboundConnection.send(message);/*from w  ww.  java 2 s . com*/
    PipedOutputStream out = new PipedOutputStream(pipe);
    out.write(baos.toByteArray());
    out.close();

    final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>();
    TcpListener listener = new TcpListener() {

        public boolean onMessage(Message<?> message) {
            if (!(message instanceof ErrorMessage)) {
                inboundMessage.set(message);
            }
            return false;
        }
    };
    inboundConnection.registerListener(listener);
    inboundConnection.run();
    assertNotNull(inboundMessage.get());
    assertEquals("foo", inboundMessage.get().getPayload());
    assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}

From source file:com.xebialabs.overthere.telnet.TelnetConnection.java

@Override
public OverthereProcess startProcess(final CmdLine cmd) {
    checkNotNull(cmd, "Cannot execute null command line");
    checkArgument(cmd.getArguments().size() > 0, "Cannot execute empty command line");

    final String obfuscatedCmd = cmd.toCommandLine(os, true);
    logger.info("Starting command [{}] on [{}]", obfuscatedCmd, this);

    try {//from   w w  w .  j  a  v a2 s. co  m
        final TelnetClient tc = new TelnetClient();
        tc.setSocketFactory(mapper.socketFactory());
        tc.setConnectTimeout(connectionTimeoutMillis);
        tc.addOptionHandler(new WindowSizeOptionHandler(299, 25, true, false, true, false));
        logger.info("Connecting to telnet://{}@{}", username, address);
        tc.connect(address, port);
        tc.setSoTimeout(socketTimeoutMillis);
        final InputStream stdout = tc.getInputStream();
        final OutputStream stdin = tc.getOutputStream();
        final PipedInputStream callersStdout = new PipedInputStream();
        final PipedOutputStream toCallersStdout = new PipedOutputStream(callersStdout);
        final ByteArrayOutputStream outputBuf = new ByteArrayOutputStream();
        final int[] exitValue = new int[1];
        exitValue[0] = -1;

        final Thread outputReaderThread = new Thread("Telnet output reader") {
            @Override
            public void run() {
                try {
                    receive(stdout, outputBuf, toCallersStdout, "ogin:");
                    send(stdin, username);

                    receive(stdout, outputBuf, toCallersStdout, "assword:");
                    send(stdin, password);

                    receive(stdout, outputBuf, toCallersStdout, ">", "ogon failure");
                    send(stdin, "PROMPT " + DETECTABLE_WINDOWS_PROMPT);
                    // We must wait for the prompt twice; the first time is an echo of the PROMPT command,
                    // the second is the actual prompt
                    receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
                    receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);

                    if (workingDirectory != null) {
                        send(stdin, "CD /D " + workingDirectory.getPath());
                        receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
                    }

                    send(stdin, cmd.toCommandLine(os, false));

                    receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);

                    send(stdin, "ECHO \"" + ERRORLEVEL_PREAMBLE + "%errorlevel%" + ERRORLEVEL_POSTAMBLE);
                    receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE);
                    receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE);
                    String outputBufStr = outputBuf.toString();
                    int preamblePos = outputBufStr.indexOf(ERRORLEVEL_PREAMBLE);
                    int postamblePos = outputBufStr.indexOf(ERRORLEVEL_POSTAMBLE);
                    if (preamblePos >= 0 && postamblePos >= 0) {
                        String errorlevelString = outputBufStr
                                .substring(preamblePos + ERRORLEVEL_PREAMBLE.length(), postamblePos);
                        logger.debug("Errorlevel string found: {}", errorlevelString);

                        try {
                            synchronized (exitValue) {
                                exitValue[0] = Integer.parseInt(errorlevelString);
                            }
                        } catch (NumberFormatException exc) {
                            logger.error("Cannot parse errorlevel in Windows output: " + outputBuf);
                        }
                    } else {
                        logger.error("Cannot find errorlevel in Windows output: " + outputBuf);
                    }
                } catch (IOException exc) {
                    throw new RuntimeIOException(
                            format("Cannot start command [%s] on [%s]", obfuscatedCmd, TelnetConnection.this),
                            exc);
                } finally {
                    closeQuietly(toCallersStdout);
                }
            }
        };
        outputReaderThread.setDaemon(true);
        outputReaderThread.start();

        return new OverthereProcess() {
            @Override
            public synchronized OutputStream getStdin() {
                return stdin;
            }

            @Override
            public synchronized InputStream getStdout() {
                return callersStdout;
            }

            @Override
            public synchronized InputStream getStderr() {
                return new ByteArrayInputStream(new byte[0]);
            }

            @Override
            public synchronized int waitFor() {
                if (!tc.isConnected()) {
                    return exitValue[0];
                }

                try {
                    try {
                        outputReaderThread.join();
                    } finally {
                        disconnect();
                    }
                    return exitValue[0];
                } catch (InterruptedException exc) {
                    throw new RuntimeIOException(
                            format("Cannot start command [%s] on [%s]", obfuscatedCmd, TelnetConnection.this),
                            exc);
                }
            }

            @Override
            public synchronized void destroy() {
                if (!tc.isConnected()) {
                    return;
                }

                disconnect();
            }

            private synchronized void disconnect() {
                try {
                    tc.disconnect();
                    logger.info("Disconnected from {}", TelnetConnection.this);

                    closeQuietly(toCallersStdout);
                } catch (IOException exc) {
                    throw new RuntimeIOException(format("Cannot disconnect from %s", TelnetConnection.this),
                            exc);
                }
            }

            @Override
            public synchronized int exitValue() {
                if (tc.isConnected()) {
                    throw new IllegalThreadStateException(
                            format("Process for command [%s] on %s is still running", obfuscatedCmd,
                                    TelnetConnection.this));
                }

                synchronized (exitValue) {
                    return exitValue[0];
                }
            }
        };
    } catch (InvalidTelnetOptionException exc) {
        throw new RuntimeIOException(
                "Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc);
    } catch (IOException exc) {
        throw new RuntimeIOException(
                "Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc);
    }
}

From source file:com.emc.ecs.sync.CasMigrationTest.java

@Test
public void testCASSingleObject() throws Exception {
    FPPool sourcePool = new FPPool(connectString1);
    FPPool targetPool = new FPPool(connectString2);

    // create clip in source (<=1MB blob size) - capture summary for comparison
    StringWriter sourceSummary = new StringWriter();
    List<String> clipIds = createTestClips(sourcePool, 1048576, 1, sourceSummary);
    String clipID = clipIds.iterator().next();

    // open clip in source
    FPClip clip = new FPClip(sourcePool, clipID, FPLibraryConstants.FP_OPEN_FLAT);

    // buffer CDF
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    clip.RawRead(baos);// w  w w . jav  a2  s. c om

    // write CDF to target
    FPClip targetClip = new FPClip(targetPool, clipID, new ByteArrayInputStream(baos.toByteArray()),
            CLIP_OPTIONS);

    // migrate blobs
    FPTag tag, targetTag;
    int tagCount = 0;
    while ((tag = clip.FetchNext()) != null) {
        targetTag = targetClip.FetchNext();
        Assert.assertEquals("Tag names don't match", tag.getTagName(), targetTag.getTagName());
        Assert.assertTrue("Tag " + tag.getTagName() + " attributes not equal",
                Arrays.equals(tag.getAttributes(), targetTag.getAttributes()));

        int blobStatus = tag.BlobExists();
        if (blobStatus == 1) {
            PipedInputStream pin = new PipedInputStream(BUFFER_SIZE);
            PipedOutputStream pout = new PipedOutputStream(pin);
            BlobReader reader = new BlobReader(tag, pout);

            // start reading in parallel
            Thread readThread = new Thread(reader);
            readThread.start();

            // write inside this thread
            targetTag.BlobWrite(pin);

            readThread.join(); // this shouldn't do anything, but just in case

            if (!reader.isSuccess())
                throw new Exception("blob read failed", reader.getError());
        } else {
            if (blobStatus != -1)
                System.out.println("blob unavailable, clipId=" + clipID + ", tagNum=" + tagCount
                        + ", blobStatus=" + blobStatus);
        }
        tag.Close();
        targetTag.Close();
        tagCount++;
    }

    clip.Close();

    Assert.assertEquals("clip IDs not equal", clipID, targetClip.Write());
    targetClip.Close();

    // check target blob data
    targetClip = new FPClip(targetPool, clipID, FPLibraryConstants.FP_OPEN_FLAT);
    Assert.assertEquals("content mismatch", sourceSummary.toString(), summarizeClip(targetClip));
    targetClip.Close();

    // delete in source and target
    FPClip.Delete(sourcePool, clipID);
    FPClip.Delete(targetPool, clipID);
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void streamDirectory(File directory, OutputStream os) {
    try {//from  ww w  . jav  a 2  s  .  co  m
        // Start the ZipStream reader. Whatever is read is streamed to response
        PipedInputStream pis = new PipedInputStream(2048);
        ZipStreamer pipestreamer = new ZipStreamer(pis, os);
        pipestreamer.start();

        // Start Zipping folder and piping to the ZipStream reader
        PipedOutputStream pos = new PipedOutputStream(pis);
        ZipOutputStream zos = new ZipOutputStream(pos);
        StorageHandler.zipAndStream(directory, zos, directory.getName() + "/");
        zos.flush();
        zos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.emc.ecs.sync.storage.CasStorageTest.java

@Test
public void testCasSingleObject() throws Exception {
    FPPool sourcePool = new FPPool(connectString1);
    FPPool targetPool = new FPPool(connectString2);

    try {/*from  w ww  . j  a va2  s  . c om*/
        // create clip in source (<=1MB blob size) - capture summary for comparison
        StringWriter sourceSummary = new StringWriter();
        List<String> clipIds = createTestClips(sourcePool, 1048576, 1, sourceSummary);
        String clipID = clipIds.iterator().next();

        // open clip in source
        FPClip clip = new FPClip(sourcePool, clipID, FPLibraryConstants.FP_OPEN_FLAT);

        // buffer CDF
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        clip.RawRead(baos);

        // write CDF to target
        FPClip targetClip = new FPClip(targetPool, clipID, new ByteArrayInputStream(baos.toByteArray()),
                CLIP_OPTIONS);

        // migrate blobs
        FPTag tag, targetTag;
        int tagCount = 0;
        while ((tag = clip.FetchNext()) != null) {
            targetTag = targetClip.FetchNext();
            Assert.assertEquals("Tag names don't match", tag.getTagName(), targetTag.getTagName());
            Assert.assertTrue("Tag " + tag.getTagName() + " attributes not equal",
                    Arrays.equals(tag.getAttributes(), targetTag.getAttributes()));

            int blobStatus = tag.BlobExists();
            if (blobStatus == 1) {
                PipedInputStream pin = new PipedInputStream(BUFFER_SIZE);
                PipedOutputStream pout = new PipedOutputStream(pin);
                BlobReader reader = new BlobReader(tag, pout);

                // start reading in parallel
                Thread readThread = new Thread(reader);
                readThread.start();

                // write inside this thread
                targetTag.BlobWrite(pin);

                readThread.join(); // this shouldn't do anything, but just in case

                if (!reader.isSuccess())
                    throw new Exception("blob read failed", reader.getError());
            } else {
                if (blobStatus != -1)
                    System.out.println("blob unavailable, clipId=" + clipID + ", tagNum=" + tagCount
                            + ", blobStatus=" + blobStatus);
            }
            tag.Close();
            targetTag.Close();
            tagCount++;
        }

        clip.Close();

        Assert.assertEquals("clip IDs not equal", clipID, targetClip.Write());
        targetClip.Close();

        // check target blob data
        targetClip = new FPClip(targetPool, clipID, FPLibraryConstants.FP_OPEN_FLAT);
        Assert.assertEquals("content mismatch", sourceSummary.toString(), summarizeClip(targetClip));
        targetClip.Close();

        // delete in source and target
        FPClip.Delete(sourcePool, clipID);
        FPClip.Delete(targetPool, clipID);
    } finally {
        try {
            sourcePool.Close();
        } catch (Throwable t) {
            log.warn("failed to close source pool", t);
        }
        try {
            targetPool.Close();
        } catch (Throwable t) {
            log.warn("failed to close dest pool", t);
        }
    }
}

From source file:com.msopentech.odatajclient.engine.communication.response.ODataResponseImpl.java

/**
 * {@inheritDoc}/*from www . j  ava  2 s.  c o  m*/
 */
@Override
public InputStream getRawResponse() {
    if (HttpStatus.SC_NO_CONTENT == getStatusCode()) {
        throw new NoContentException();
    }

    if (payload == null && batchInfo.isValidBatch()) {
        // get input stream till the end of item
        payload = new PipedInputStream();

        try {
            final PipedOutputStream os = new PipedOutputStream((PipedInputStream) payload);

            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        ODataBatchUtilities.readBatchPart(batchInfo, os, true);
                    } catch (Exception e) {
                        LOG.error("Error streaming batch item payload", e);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }).start();

        } catch (IOException e) {
            LOG.error("Error streaming payload response", e);
            throw new IllegalStateException(e);
        }
    }

    return payload;
}