Search in sources :

Example 21 with SequenceInputStream

use of java.io.SequenceInputStream in project jackrabbit-oak by apache.

the class DataStoreBlobStore method writeStream.

/**
 * Create a BLOB value from in input stream. Small objects will create an in-memory object,
 * while large objects are stored in the data store
 *
 * @param in the input stream
 * @param options
 * @return the value
 */
private DataRecord writeStream(InputStream in, BlobOptions options) throws IOException, DataStoreException {
    int maxMemorySize = Math.max(0, delegate.getMinRecordLength() + 1);
    byte[] buffer = new byte[maxMemorySize];
    int pos = 0, len = maxMemorySize;
    while (pos < maxMemorySize) {
        int l = in.read(buffer, pos, len);
        if (l < 0) {
            break;
        }
        pos += l;
        len -= l;
    }
    DataRecord record;
    if (pos < maxMemorySize) {
        // shrink the buffer
        byte[] data = new byte[pos];
        System.arraycopy(buffer, 0, data, 0, pos);
        record = InMemoryDataRecord.getInstance(data);
    } else {
        // a few bytes are already read, need to re-build the input stream
        in = new SequenceInputStream(new ByteArrayInputStream(buffer, 0, pos), in);
        record = addRecord(in, options);
    }
    return record;
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 22 with SequenceInputStream

use of java.io.SequenceInputStream in project jackrabbit-oak by apache.

the class AbstractDataStoreTest method doTest.

/**
 * Assert randomly read stream from record.
 */
void doTest(DataStore ds, int offset) throws Exception {
    ArrayList<DataRecord> list = new ArrayList<DataRecord>();
    HashMap<DataRecord, Integer> map = new HashMap<DataRecord, Integer>();
    for (int i = 0; i < 10; i++) {
        int size = 100000 - (i * 100);
        RandomInputStream in = new RandomInputStream(size + offset, size);
        DataRecord rec = ds.addRecord(in);
        list.add(rec);
        map.put(rec, size);
    }
    Random random = new Random(1);
    for (int i = 0; i < list.size(); i++) {
        int pos = random.nextInt(list.size());
        DataRecord rec = list.get(pos);
        int size = map.get(rec);
        rec = ds.getRecord(rec.getIdentifier());
        Assert.assertEquals(size, rec.getLength());
        RandomInputStream expected = new RandomInputStream(size + offset, size);
        InputStream in = rec.getStream();
        // Workaround for race condition that can happen with low cache size relative to the test
        // read immediately
        byte[] buffer = new byte[1];
        in.read(buffer);
        in = new SequenceInputStream(new ByteArrayInputStream(buffer), in);
        if (random.nextBoolean()) {
            in = readInputStreamRandomly(in, random);
        }
        assertEquals(expected, in);
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Random(java.util.Random) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream)

Example 23 with SequenceInputStream

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

the class FileSplit method newInputStream.

@Override
public InputStream newInputStream() throws IOException {
    InputStream input = getBase().newInputStream();
    for (int i = 1; ; i++) {
        FilePath f = getBase(i);
        if (f.exists()) {
            InputStream i2 = f.newInputStream();
            input = new SequenceInputStream(input, i2);
        } else {
            break;
        }
    }
    return input;
}
Also used : SequenceInputStream(java.io.SequenceInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream)

Example 24 with SequenceInputStream

use of java.io.SequenceInputStream in project syncope by apache.

the class ReportSyncopeOperations method exportExecutionResult.

public String exportExecutionResult(final String executionKey, final String reportExecExportFormat) throws Exception {
    ReportExecExportFormat format = ReportExecExportFormat.valueOf(reportExecExportFormat);
    SequenceInputStream report = (SequenceInputStream) reportService.exportExecutionResult(executionKey, format).getEntity();
    String fileName = "export_" + executionKey;
    OutputStream os = null;
    switch(format) {
        case XML:
            fileName += ".xml";
            XMLUtils.createXMLFile(report, fileName);
            break;
        case CSV:
            fileName += ".csv";
            os = Files.newOutputStream(Paths.get(fileName));
            IOUtils.copyAndCloseInput(report, os);
            break;
        case PDF:
            fileName += ".pdf";
            os = Files.newOutputStream(Paths.get(fileName));
            IOUtils.copyAndCloseInput(report, os);
            break;
        case HTML:
            fileName += ".html";
            os = Files.newOutputStream(Paths.get(fileName));
            IOUtils.copyAndCloseInput(report, os);
            break;
        case RTF:
            fileName += ".rtf";
            os = Files.newOutputStream(Paths.get(fileName));
            IOUtils.copyAndCloseInput(report, os);
            break;
        default:
            return format + " not supported";
    }
    if (os != null) {
        os.close();
    }
    return fileName;
}
Also used : ReportExecExportFormat(org.apache.syncope.common.lib.types.ReportExecExportFormat) SequenceInputStream(java.io.SequenceInputStream) OutputStream(java.io.OutputStream)

Example 25 with SequenceInputStream

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

the class VerifierServletTransaction method validate.

/**
 * @throws SAXException
 */
@SuppressWarnings({ "deprecation", "unchecked" })
void validate() throws SAXException {
    if (!willValidate()) {
        return;
    }
    boolean isHtmlOrXhtml = (outputFormat == OutputFormat.HTML || outputFormat == OutputFormat.XHTML);
    if (isHtmlOrXhtml) {
        try {
            out.flush();
        } catch (IOException e1) {
            throw new SAXException(e1);
        }
    }
    httpRes = new PrudentHttpEntityResolver(SIZE_LIMIT, laxType, errorHandler, request);
    httpRes.setUserAgent(userAgent);
    dataRes = new DataUriEntityResolver(httpRes, laxType, errorHandler);
    contentTypeParser = new ContentTypeParser(errorHandler, laxType);
    entityResolver = new LocalCacheEntityResolver(dataRes);
    setAllowRnc(true);
    setAllowCss(true);
    try {
        this.errorHandler.start(document);
        PropertyMapBuilder pmb = new PropertyMapBuilder();
        pmb.put(ValidateProperty.ERROR_HANDLER, errorHandler);
        pmb.put(ValidateProperty.ENTITY_RESOLVER, entityResolver);
        pmb.put(ValidateProperty.XML_READER_CREATOR, new VerifierServletXMLReaderCreator(errorHandler, entityResolver));
        pmb.put(ValidateProperty.SCHEMA_RESOLVER, this);
        RngProperty.CHECK_ID_IDREF.add(pmb);
        jingPropertyMap = pmb.toPropertyMap();
        tryToSetupValidator();
        setAllowRnc(false);
        loadDocAndSetupParser();
        setErrorProfile();
        contentType = documentInput.getType();
        if ("text/css".equals(contentType)) {
            String charset = "UTF-8";
            if (documentInput.getEncoding() != null) {
                charset = documentInput.getEncoding();
            }
            List<InputStream> streams = new ArrayList<>();
            streams.add(new ByteArrayInputStream(CSS_CHECKING_PROLOG));
            streams.add(documentInput.getByteStream());
            streams.add(new ByteArrayInputStream(CSS_CHECKING_EPILOG));
            Enumeration<InputStream> e = Collections.enumeration(streams);
            documentInput.setByteStream(new SequenceInputStream(e));
            documentInput.setEncoding(charset);
            errorHandler.setLineOffset(-1);
            sourceCode.setIsCss();
            parser = ParserMode.HTML;
            loadDocAndSetupParser();
        }
        reader.setErrorHandler(errorHandler);
        sourceCode.initialize(documentInput);
        if (validator == null) {
            checkNormalization = true;
        }
        if (checkNormalization) {
            reader.setFeature("http://xml.org/sax/features/unicode-normalization-checking", true);
        }
        WiretapXMLReaderWrapper wiretap = new WiretapXMLReaderWrapper(reader);
        ContentHandler recorder = sourceCode.getLocationRecorder();
        if (baseUriTracker == null) {
            wiretap.setWiretapContentHander(recorder);
        } else {
            wiretap.setWiretapContentHander(new CombineContentHandler(recorder, baseUriTracker));
        }
        wiretap.setWiretapLexicalHandler((LexicalHandler) recorder);
        reader = wiretap;
        if (htmlParser != null) {
            htmlParser.addCharacterHandler(sourceCode);
            htmlParser.setMappingLangToXmlLang(true);
            htmlParser.setErrorHandler(errorHandler.getExactErrorHandler());
            htmlParser.setTreeBuilderErrorHandlerOverride(errorHandler);
            errorHandler.setHtml(true);
        } else if (xmlParser != null) {
            // this must be after wiretap!
            if (!filteredNamespaces.isEmpty()) {
                reader = new NamespaceDroppingXMLReaderWrapper(reader, filteredNamespaces);
            }
            xmlParser.setErrorHandler(errorHandler.getExactErrorHandler());
            xmlParser.lockErrorHandler();
        } else {
            throw new RuntimeException("Bug. Unreachable.");
        }
        // make
        reader = new AttributesPermutingXMLReaderWrapper(reader);
        // better
        if (charsetOverride != null) {
            String charset = documentInput.getEncoding();
            if (charset == null) {
                errorHandler.warning(new SAXParseException("Overriding document character encoding from none to \u201C" + charsetOverride + "\u201D.", null));
            } else {
                errorHandler.warning(new SAXParseException("Overriding document character encoding from \u201C" + charset + "\u201D to \u201C" + charsetOverride + "\u201D.", null));
            }
            documentInput.setEncoding(charsetOverride);
        }
        if (showOutline) {
            reader = new OutlineBuildingXMLReaderWrapper(reader, request, false);
            reader = new OutlineBuildingXMLReaderWrapper(reader, request, true);
        }
        reader.parse(documentInput);
        if (showOutline) {
            outline = (Deque<Section>) request.getAttribute("http://validator.nu/properties/document-outline");
            headingOutline = (Deque<Section>) request.getAttribute("http://validator.nu/properties/heading-outline");
        }
    } catch (CannotFindPresetSchemaException e) {
    } catch (ResourceNotRetrievableException e) {
        log4j.debug(e.getMessage());
    } catch (NonXmlContentTypeException e) {
        log4j.debug(e.getMessage());
    } catch (FatalSAXException e) {
        log4j.debug(e.getMessage());
    } catch (SocketTimeoutException e) {
        errorHandler.ioError(new IOException(e.getMessage(), null));
    } catch (ConnectTimeoutException e) {
        errorHandler.ioError(new IOException(e.getMessage(), null));
    } catch (TooManyErrorsException e) {
        errorHandler.fatalError(e);
    } catch (SAXException e) {
        String msg = e.getMessage();
        if (!cannotRecover.equals(msg) && !changingEncoding.equals(msg)) {
            log4j.debug("SAXException: " + e.getMessage());
        }
    } catch (IOException e) {
        isHtmlOrXhtml = false;
        if (e.getCause() instanceof org.apache.http.TruncatedChunkException) {
            log4j.debug("TruncatedChunkException", e.getCause());
        } else {
            errorHandler.ioError(e);
        }
    } catch (IncorrectSchemaException e) {
        log4j.debug("IncorrectSchemaException", e);
        errorHandler.schemaError(e);
    } catch (RuntimeException e) {
        isHtmlOrXhtml = false;
        log4j.error("RuntimeException, doc: " + document + " schema: " + schemaUrls + " lax: " + laxType, e);
        errorHandler.internalError(e, "Oops. That was not supposed to happen. A bug manifested itself in the application internals. Unable to continue. Sorry. The admin was notified.");
    } catch (Error e) {
        isHtmlOrXhtml = false;
        log4j.error("Error, doc: " + document + " schema: " + schemaUrls + " lax: " + laxType, e);
        errorHandler.internalError(e, "Oops. That was not supposed to happen. A bug manifested itself in the application internals. Unable to continue. Sorry. The admin was notified.");
    } finally {
        errorHandler.end(successMessage(), failureMessage(), (String) request.getAttribute("http://validator.nu/properties/document-language"));
        gatherStatistics();
    }
    if (isHtmlOrXhtml) {
        XhtmlOutlineEmitter outlineEmitter = new XhtmlOutlineEmitter(contentHandler, outline, headingOutline);
        outlineEmitter.emitHeadings();
        outlineEmitter.emit();
        emitDetails();
        StatsEmitter.emit(contentHandler, this);
    }
}
Also used : TooManyErrorsException(nu.validator.messages.TooManyErrorsException) WiretapXMLReaderWrapper(nu.validator.xml.WiretapXMLReaderWrapper) ArrayList(java.util.ArrayList) NonXmlContentTypeException(nu.validator.xml.ContentTypeParser.NonXmlContentTypeException) PrudentHttpEntityResolver(nu.validator.xml.PrudentHttpEntityResolver) CombineContentHandler(nu.validator.xml.CombineContentHandler) ContentHandler(org.xml.sax.ContentHandler) FatalSAXException(nu.validator.gnu.xml.aelfred2.FatalSAXException) SAXException(org.xml.sax.SAXException) ContentTypeParser(nu.validator.xml.ContentTypeParser) CombineContentHandler(nu.validator.xml.CombineContentHandler) NamespaceDroppingXMLReaderWrapper(nu.validator.xml.NamespaceDroppingXMLReaderWrapper) SAXParseException(org.xml.sax.SAXParseException) FatalSAXException(nu.validator.gnu.xml.aelfred2.FatalSAXException) ResourceNotRetrievableException(nu.validator.xml.PrudentHttpEntityResolver.ResourceNotRetrievableException) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) DataUriEntityResolver(nu.validator.xml.DataUriEntityResolver) BoundedInputStream(nu.validator.io.BoundedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AttributesPermutingXMLReaderWrapper(nu.validator.xml.AttributesPermutingXMLReaderWrapper) IncorrectSchemaException(com.thaiopensource.validate.IncorrectSchemaException) IOException(java.io.IOException) Section(nu.validator.servlet.OutlineBuildingXMLReaderWrapper.Section) LocalCacheEntityResolver(nu.validator.localentities.LocalCacheEntityResolver) SocketTimeoutException(java.net.SocketTimeoutException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)119 InputStream (java.io.InputStream)76 ByteArrayInputStream (java.io.ByteArrayInputStream)65 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)31 FileInputStream (java.io.FileInputStream)22 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Test (org.junit.Test)10 Vector (java.util.Vector)9 lombok.val (lombok.val)9 OutputStream (java.io.OutputStream)8 List (java.util.List)8 FileOutputStream (java.io.FileOutputStream)7 InputStreamReader (java.io.InputStreamReader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5