Search in sources :

Example 71 with SequenceInputStream

use of java.io.SequenceInputStream in project jdk8u_jdk by JetBrains.

the class LotsOfStreams method main.

public static void main(String[] argv) throws Exception {
    try (InputStream stream = new SequenceInputStream(new LOSEnumeration())) {
        stream.read();
    }
    try (InputStream stream = new SequenceInputStream(new LOSEnumeration())) {
        byte[] b = new byte[1];
        stream.read(b, 0, 1);
    }
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream)

Example 72 with SequenceInputStream

use of java.io.SequenceInputStream in project jdk8u_jdk by JetBrains.

the class GZIPInputStream method readTrailer.

/*
     * Reads GZIP member trailer and returns true if the eos
     * reached, false if there are more (concatenated gzip
     * data set)
     */
private boolean readTrailer() throws IOException {
    InputStream in = this.in;
    int n = inf.getRemaining();
    if (n > 0) {
        in = new SequenceInputStream(new ByteArrayInputStream(buf, len - n, n), new FilterInputStream(in) {

            public void close() throws IOException {
            }
        });
    }
    // Uses left-to-right evaluation order
    if ((readUInt(in) != crc.getValue()) || // rfc1952; ISIZE is the input size modulo 2^32
    (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
        throw new ZipException("Corrupt GZIP trailer");
    // try concatenated case
    if (this.in.available() > 0 || n > 26) {
        // this.trailer
        int m = 8;
        try {
            // next.header
            m += readHeader(in);
        } catch (IOException ze) {
            // ignore any malformed, do nothing
            return true;
        }
        inf.reset();
        if (n > m)
            inf.setInput(buf, len - n + m, n - m);
        return false;
    }
    return true;
}
Also used : FilterInputStream(java.io.FilterInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 73 with SequenceInputStream

use of java.io.SequenceInputStream in project jdk8u_jdk by JetBrains.

the class StandardMidiFileWriter method writeTrack.

private InputStream writeTrack(Track track, int type) throws IOException, InvalidMidiDataException {
    int bytesWritten = 0;
    int lastBytesWritten = 0;
    int size = track.size();
    PipedOutputStream thpos = new PipedOutputStream();
    DataOutputStream thdos = new DataOutputStream(thpos);
    PipedInputStream thpis = new PipedInputStream(thpos);
    ByteArrayOutputStream tdbos = new ByteArrayOutputStream();
    tddos = new DataOutputStream(tdbos);
    ByteArrayInputStream tdbis = null;
    SequenceInputStream fStream = null;
    long currentTick = 0;
    long deltaTick = 0;
    long eventTick = 0;
    int runningStatus = -1;
    // -----------------------------
    for (int i = 0; i < size; i++) {
        MidiEvent event = track.get(i);
        int status;
        int eventtype;
        int metatype;
        int data1, data2;
        int length;
        byte[] data = null;
        ShortMessage shortMessage = null;
        MetaMessage metaMessage = null;
        SysexMessage sysexMessage = null;
        // get the tick
        // $$jb: this gets easier if we change all system-wide time to delta ticks
        eventTick = event.getTick();
        deltaTick = event.getTick() - currentTick;
        currentTick = event.getTick();
        // get the status byte
        status = event.getMessage().getStatus();
        eventtype = getType(status);
        switch(eventtype) {
            case ONE_BYTE:
                shortMessage = (ShortMessage) event.getMessage();
                data1 = shortMessage.getData1();
                bytesWritten += writeVarInt(deltaTick);
                if (status != runningStatus) {
                    runningStatus = status;
                    tddos.writeByte(status);
                    bytesWritten += 1;
                }
                tddos.writeByte(data1);
                bytesWritten += 1;
                break;
            case TWO_BYTE:
                shortMessage = (ShortMessage) event.getMessage();
                data1 = shortMessage.getData1();
                data2 = shortMessage.getData2();
                bytesWritten += writeVarInt(deltaTick);
                if (status != runningStatus) {
                    runningStatus = status;
                    tddos.writeByte(status);
                    bytesWritten += 1;
                }
                tddos.writeByte(data1);
                bytesWritten += 1;
                tddos.writeByte(data2);
                bytesWritten += 1;
                break;
            case SYSEX:
                sysexMessage = (SysexMessage) event.getMessage();
                length = sysexMessage.getLength();
                data = sysexMessage.getMessage();
                bytesWritten += writeVarInt(deltaTick);
                // $$jb: 04.08.99: always write status for sysex
                runningStatus = status;
                tddos.writeByte(data[0]);
                bytesWritten += 1;
                // $$jb: 10.18.99: we don't maintain length in
                // the message data for SysEx (it is not transmitted
                // over the line), so write the calculated length
                // minus the status byte
                bytesWritten += writeVarInt((data.length - 1));
                // $$jb: 10.18.99: now write the rest of the
                // message
                tddos.write(data, 1, (data.length - 1));
                bytesWritten += (data.length - 1);
                break;
            case META:
                metaMessage = (MetaMessage) event.getMessage();
                length = metaMessage.getLength();
                data = metaMessage.getMessage();
                bytesWritten += writeVarInt(deltaTick);
                // $$jb: 10.18.99: getMessage() returns the
                // entire valid midi message for a file,
                // including the status byte and the var-length-int
                // length value, so we can just write the data
                // here.  note that we must _always_ write the
                // status byte, regardless of runningStatus.
                runningStatus = status;
                tddos.write(data, 0, data.length);
                bytesWritten += data.length;
                break;
            case IGNORE:
                // ignore this event
                break;
            case ERROR:
                // ignore this event
                break;
            default:
                throw new InvalidMidiDataException("internal file writer error");
        }
    }
    // ---------------------------------
    // End write each event in the track
    // ---------------------------------
    // Build Track header now that we know length
    thdos.writeInt(MTrk_MAGIC);
    thdos.writeInt(bytesWritten);
    bytesWritten += 8;
    // Now sequence them
    tdbis = new ByteArrayInputStream(tdbos.toByteArray());
    fStream = new SequenceInputStream(thpis, tdbis);
    thdos.close();
    tddos.close();
    return fStream;
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) DataOutputStream(java.io.DataOutputStream) MidiEvent(javax.sound.midi.MidiEvent) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ShortMessage(javax.sound.midi.ShortMessage) SysexMessage(javax.sound.midi.SysexMessage) MetaMessage(javax.sound.midi.MetaMessage)

Example 74 with SequenceInputStream

use of java.io.SequenceInputStream in project webservices-axiom by apache.

the class TestGetTextAsStreamWithoutCaching method runTest.

@Override
protected void runTest() throws Throwable {
    Charset charset = Charset.forName("ascii");
    OMFactory factory = metaFactory.getOMFactory();
    DataSource ds = new RandomDataSource(654321, 64, 128, 20000000);
    Vector<InputStream> v = new Vector<InputStream>();
    v.add(new ByteArrayInputStream("<root><a>".getBytes(charset)));
    v.add(ds.getInputStream());
    v.add(new ByteArrayInputStream("</a><b/></root>".getBytes(charset)));
    OMElement root = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.NON_COALESCING, new SequenceInputStream(v.elements()), "ascii").getDocumentElement();
    OMElement child = (OMElement) root.getFirstOMChild();
    Reader in = child.getTextAsStream(false);
    IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), charset), "expected", in, "actual");
    in.close();
    // No try to access subsequent nodes
    child = (OMElement) child.getNextOMSibling();
    assertThat(child.getLocalName()).isEqualTo("b");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) SequenceInputStream(java.io.SequenceInputStream) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OMElement(org.apache.axiom.om.OMElement) Vector(java.util.Vector) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 75 with SequenceInputStream

use of java.io.SequenceInputStream in project checker-framework by typetools.

the class GZIPInputStream method readTrailer.

/*
     * Reads GZIP member trailer and returns true if the eos
     * reached, false if there are more (concatenated gzip
     * data set)
     */
private boolean readTrailer() throws IOException {
    InputStream in = this.in;
    int n = inf.getRemaining();
    if (n > 0) {
        in = new SequenceInputStream(new ByteArrayInputStream(buf, len - n, n), in);
    }
    // Uses left-to-right evaluation order
    if ((readUInt(in) != crc.getValue()) || // rfc1952; ISIZE is the input size modulo 2^32
    (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
        throw new ZipException("Corrupt GZIP trailer");
    // try concatenated case
    if (this.in.available() > 0 || n > 26) {
        // this.trailer
        int m = 8;
        try {
            // next.header
            m += readHeader(in);
        } catch (IOException ze) {
            // ignore any malformed, do nothing
            return true;
        }
        inf.reset();
        if (n > m)
            inf.setInput(buf, len - n + m, n - m);
        return false;
    }
    return true;
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

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