Search in sources :

Example 86 with SequenceInputStream

use of java.io.SequenceInputStream in project ovirt-engine by oVirt.

the class SSHDialog method executeCommand.

/**
 * Execute command.
 *
 * @param sink
 *            sink to use.
 * @param command
 *            command to execute.
 * @param initial
 *            initial input streams to send to host before dialog begins.
 */
public void executeCommand(Sink sink, String command, InputStream[] initial) throws Exception {
    log.info("SSH execute '{}' '{}'", client.getDisplayHost(), command);
    try (final PipedInputStream pinStdin = new PipedInputStream(BUFFER_SIZE);
        final OutputStream poutStdin = new PipedOutputStream(pinStdin);
        final PipedInputStream pinStdout = new PipedInputStream(BUFFER_SIZE);
        final OutputStream poutStdout = new PipedOutputStream(pinStdout);
        final ByteArrayOutputStream stderr = new ConstraintByteArrayOutputStream(1024)) {
        try {
            List<InputStream> stdinList;
            if (initial == null) {
                stdinList = new LinkedList<>();
            } else {
                stdinList = new LinkedList<>(Arrays.asList(initial));
            }
            stdinList.add(pinStdin);
            sink.setControl(() -> {
                if (client != null) {
                    client.close();
                }
            });
            sink.setStreams(pinStdout, poutStdin);
            sink.start();
            try {
                client.executeCommand(command, new SequenceInputStream(Collections.enumeration(stdinList)), poutStdout, stderr);
            } catch (Exception e) {
                if (stderr.size() == 0) {
                    throw e;
                }
                log.error("Swallowing exception as preferring stderr", ExceptionUtils.getRootCauseMessage(e));
                log.debug("Exception", e);
            } finally {
                if (stderr.size() > 0) {
                    throw new RuntimeException(String.format("Unexpected error during execution: %1$s", new String(stderr.toByteArray(), StandardCharsets.UTF_8)));
                }
            }
        } catch (Exception e) {
            log.error("SSH error running command {}:'{}': {}", client.getDisplayHost(), command, ExceptionUtils.getRootCauseMessage(e));
            log.debug("Exception", e);
            throw e;
        } finally {
            sink.stop();
            sink.setStreams(null, null);
        }
    }
    log.debug("execute leave");
}
Also used : SequenceInputStream(java.io.SequenceInputStream) SequenceInputStream(java.io.SequenceInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 87 with SequenceInputStream

use of java.io.SequenceInputStream in project plugin-prov by ligoj.

the class ProvQuoteInstanceResource method upload.

/**
 * Upload a file of quote in add mode.
 *
 * @param subscription
 *            The subscription identifier, will be used to filter the locations from the associated provider.
 * @param uploadedFile
 *            Instance entries files to import. Currently support only CSV format.
 * @param headers
 *            the CSV header names. When <code>null</code> or empty, the default headers are used.
 * @param headersIncluded
 *            When <code>true</code>, the first line is the headers and the given <code>headers</code> parameter is
 *            ignored. Otherwise the <code>headers</code> parameter is used.
 * @param usage
 *            The optional usage name. When not <code>null</code>, each quote instance will be associated to this
 *            usage.
 * @param ramMultiplier
 *            The multiplier for imported RAM values. Default is 1.
 * @param encoding
 *            CSV encoding. Default is UTF-8.
 * @throws IOException
 *             When the CSV stream cannot be written.
 */
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{subscription:\\d+}/upload")
public void upload(@PathParam("subscription") final int subscription, @Multipart(value = "csv-file") final InputStream uploadedFile, @Multipart(value = "headers", required = false) final String[] headers, @Multipart(value = "headers-included", required = false) final boolean headersIncluded, @Multipart(value = "usage", required = false) final String usage, @Multipart(value = "memoryUnit", required = false) final Integer ramMultiplier, @Multipart(value = "encoding", required = false) final String encoding) throws IOException {
    subscriptionResource.checkVisibleSubscription(subscription).getNode().getId();
    final String safeEncoding = ObjectUtils.defaultIfNull(encoding, StandardCharsets.UTF_8.name());
    // Check headers validity
    final String[] sanitizeColumns;
    final Reader reader;
    if (headersIncluded) {
        // Header at first line
        final String rawFile = IOUtils.toString(uploadedFile, safeEncoding);
        sanitizeColumns = StringUtils.defaultString(new BufferedReader(new StringReader(rawFile)).readLine(), "").replace(',', ';').split(";");
        reader = new StringReader(rawFile);
    } else {
        // Headers are provided separately
        sanitizeColumns = ArrayUtils.isEmpty(headers) ? DEFAULT_HEADERS : headers;
        reader = new InputStreamReader(new SequenceInputStream(new ByteArrayInputStream((StringUtils.chop(ArrayUtils.toString(sanitizeColumns)).substring(1).replace(',', ';') + "\n").getBytes(safeEncoding)), uploadedFile), safeEncoding);
    }
    checkHeaders(ACCEPTED_HEADERS, sanitizeColumns);
    // Build entries
    csvForBean.toBean(InstanceUpload.class, reader).stream().filter(Objects::nonNull).forEach(i -> persist(i, subscription, usage, ramMultiplier));
}
Also used : InputStreamReader(java.io.InputStreamReader) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 88 with SequenceInputStream

use of java.io.SequenceInputStream in project Orekit by CS-SI.

the class OEMParserTest method testITRFFrames.

/**
 * Check the parser can parse several ITRF frames. Test case for #361.
 *
 * @throws OrekitException on error.
 */
@Test
public void testITRFFrames() throws OrekitException {
    // setup
    Charset utf8 = StandardCharsets.UTF_8;
    IERSConventions conventions = IERSConventions.IERS_2010;
    boolean simpleEop = true;
    Frame itrf2008 = FramesFactory.getITRF(conventions, simpleEop);
    OEMParser parser = new OEMParser().withSimpleEOP(simpleEop).withConventions(conventions);
    // frames to check
    List<Pair<String, Frame>> frames = new ArrayList<>();
    frames.add(new Pair<>("ITRF-93", Predefined.ITRF_2008_TO_ITRF_93.createTransformedITRF(itrf2008, "ITRF93")));
    frames.add(new Pair<>("ITRF-97", Predefined.ITRF_2008_TO_ITRF_97.createTransformedITRF(itrf2008, "ITRF97")));
    frames.add(new Pair<>("ITRF2000", Predefined.ITRF_2008_TO_ITRF_2000.createTransformedITRF(itrf2008, "ITRF2000")));
    frames.add(new Pair<>("ITRF2005", Predefined.ITRF_2008_TO_ITRF_2005.createTransformedITRF(itrf2008, "ITRF2005")));
    frames.add(new Pair<>("ITRF2008", itrf2008));
    for (Pair<String, Frame> frame : frames) {
        final String frameName = frame.getFirst();
        InputStream pre = OEMParserTest.class.getResourceAsStream("/ccsds/OEMExample7.txt.pre");
        InputStream middle = new ByteArrayInputStream(("REF_FRAME = " + frameName).getBytes(utf8));
        InputStream post = OEMParserTest.class.getResourceAsStream("/ccsds/OEMExample7.txt.post");
        InputStream input = new SequenceInputStream(pre, new SequenceInputStream(middle, post));
        // action
        OEMFile actual = parser.parse(input);
        // verify
        EphemeridesBlock actualBlock = actual.getEphemeridesBlocks().get(0);
        Assert.assertEquals(actualBlock.getFrameString(), frameName);
        // check expected frame
        Frame actualFrame = actualBlock.getFrame();
        Frame expectedFrame = frame.getSecond();
        Assert.assertEquals(actualFrame.getName(), expectedFrame.getName());
        Assert.assertEquals(actualFrame.getTransformProvider(), expectedFrame.getTransformProvider());
    }
}
Also used : Frame(org.orekit.frames.Frame) FactoryManagedFrame(org.orekit.frames.FactoryManagedFrame) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) IERSConventions(org.orekit.utils.IERSConventions) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) EphemeridesBlock(org.orekit.files.ccsds.OEMFile.EphemeridesBlock) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Pair(org.hipparchus.util.Pair) Test(org.junit.Test)

Example 89 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class OperationLogTestBase method getExpectedContents.

/**
 * Given a list of Log Operations, generates an InputStream for each encountered StreamSegment that contains the final
 * contents of that StreamSegment. Only considers operations of type StreamSegmentAppendOperation and MergeSegmentOperation.
 */
private AbstractMap<Long, InputStream> getExpectedContents(Collection<OperationWithCompletion> operations) {
    HashMap<Long, List<InputStream>> partialContents = new HashMap<>();
    for (OperationWithCompletion o : operations) {
        Assert.assertTrue("Operation is not completed.", o.completion.isDone());
        if (o.completion.isCompletedExceptionally()) {
            // This is failed operation; ignore it.
            continue;
        }
        if (o.operation instanceof StreamSegmentAppendOperation) {
            StreamSegmentAppendOperation appendOperation = (StreamSegmentAppendOperation) o.operation;
            List<InputStream> segmentContents = partialContents.get(appendOperation.getStreamSegmentId());
            if (segmentContents == null) {
                segmentContents = new ArrayList<>();
                partialContents.put(appendOperation.getStreamSegmentId(), segmentContents);
            }
            segmentContents.add(appendOperation.getData().getReader());
        } else if (o.operation instanceof MergeSegmentOperation) {
            MergeSegmentOperation mergeOperation = (MergeSegmentOperation) o.operation;
            List<InputStream> targetSegmentContents = partialContents.get(mergeOperation.getStreamSegmentId());
            if (targetSegmentContents == null) {
                targetSegmentContents = new ArrayList<>();
                partialContents.put(mergeOperation.getStreamSegmentId(), targetSegmentContents);
            }
            List<InputStream> sourceSegmentContents = partialContents.get(mergeOperation.getSourceSegmentId());
            targetSegmentContents.addAll(sourceSegmentContents);
            partialContents.remove(mergeOperation.getSourceSegmentId());
        }
    }
    // Construct final result.
    HashMap<Long, InputStream> result = new HashMap<>();
    for (Map.Entry<Long, List<InputStream>> e : partialContents.entrySet()) {
        result.put(e.getKey(), new SequenceInputStream(Iterators.asEnumeration(e.getValue().iterator())));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) MergeSegmentOperation(io.pravega.segmentstore.server.logs.operations.MergeSegmentOperation) SequenceInputStream(java.io.SequenceInputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 90 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class AsyncReadResultProcessorTests method testProcessAll.

/**
 * Tests the {@link AsyncReadResultProcessor#processAll} method.
 */
@Test
public void testProcessAll() throws Exception {
    // Pre-generate some entries.
    ArrayList<byte[]> entries = new ArrayList<>();
    int totalLength = generateEntries(entries);
    // Setup an entry provider supplier.
    AtomicInteger currentIndex = new AtomicInteger();
    StreamSegmentReadResult.NextEntrySupplier supplier = (offset, length, makeCopy) -> {
        int idx = currentIndex.getAndIncrement();
        if (idx == entries.size() - 1) {
            // Future read result.
            Supplier<BufferView> entryContentsSupplier = () -> new ByteArraySegment(entries.get(idx));
            return new TestFutureReadResultEntry(offset, length, entryContentsSupplier, executorService());
        } else if (idx >= entries.size()) {
            return null;
        }
        // Normal read.
        return new CacheReadResultEntry(offset, entries.get(idx), 0, entries.get(idx).length);
    };
    // Fetch all the data and compare with expected.
    @Cleanup StreamSegmentReadResult rr = new StreamSegmentReadResult(0, totalLength, supplier, "");
    val result = AsyncReadResultProcessor.processAll(rr, executorService(), TIMEOUT);
    val actualData = result.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).getReader();
    val expectedData = new SequenceInputStream(Iterators.asEnumeration(entries.stream().map(ByteArrayInputStream::new).iterator()));
    AssertExtensions.assertStreamEquals("Unexpected data read back.", expectedData, actualData, totalLength);
}
Also used : AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) Timeout(org.junit.rules.Timeout) Executor(java.util.concurrent.Executor) SequenceInputStream(java.io.SequenceInputStream) Semaphore(java.util.concurrent.Semaphore) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert(org.junit.Assert) Futures(io.pravega.common.concurrent.Futures) lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) SequenceInputStream(java.io.SequenceInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(java.util.function.Supplier) Test(org.junit.Test)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)123 InputStream (java.io.InputStream)78 ByteArrayInputStream (java.io.ByteArrayInputStream)67 IOException (java.io.IOException)47 ArrayList (java.util.ArrayList)32 FileInputStream (java.io.FileInputStream)24 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Vector (java.util.Vector)10 Test (org.junit.Test)10 FileOutputStream (java.io.FileOutputStream)9 List (java.util.List)9 lombok.val (lombok.val)9 File (java.io.File)8 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 InputStreamReader (java.io.InputStreamReader)7 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5