Search in sources :

Example 46 with PipedInputStream

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

the class StandardMidiFileWriter method getFileStream.

//=================================================================================
private InputStream getFileStream(int type, Sequence sequence) throws IOException {
    Track[] tracks = sequence.getTracks();
    int bytesBuilt = 0;
    int headerLength = 14;
    int length = 0;
    int timeFormat;
    float divtype;
    PipedOutputStream hpos = null;
    DataOutputStream hdos = null;
    PipedInputStream headerStream = null;
    InputStream[] trackStreams = null;
    InputStream trackStream = null;
    InputStream fStream = null;
    // Determine the filetype to write
    if (type == MIDI_TYPE_0) {
        if (tracks.length != 1) {
            return null;
        }
    } else if (type == MIDI_TYPE_1) {
        if (tracks.length < 1) {
            // $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
            return null;
        }
    } else {
        if (tracks.length == 1) {
            type = MIDI_TYPE_0;
        } else if (tracks.length > 1) {
            type = MIDI_TYPE_1;
        } else {
            return null;
        }
    }
    // Now build the file one track at a time
    // Note that above we made sure that MIDI_TYPE_0 only happens
    // if tracks.length==1
    trackStreams = new InputStream[tracks.length];
    int trackCount = 0;
    for (int i = 0; i < tracks.length; i++) {
        try {
            trackStreams[trackCount] = writeTrack(tracks[i], type);
            trackCount++;
        } catch (InvalidMidiDataException e) {
            if (Printer.err)
                Printer.err("Exception in write: " + e.getMessage());
        }
    //bytesBuilt += trackStreams[i].getLength();
    }
    // Now seqence the track streams
    if (trackCount == 1) {
        trackStream = trackStreams[0];
    } else if (trackCount > 1) {
        trackStream = trackStreams[0];
        for (int i = 1; i < tracks.length; i++) {
            // don't include failed track streams
            if (trackStreams[i] != null) {
                trackStream = new SequenceInputStream(trackStream, trackStreams[i]);
            }
        }
    } else {
        throw new IllegalArgumentException("invalid MIDI data in sequence");
    }
    // Now build the header...
    hpos = new PipedOutputStream();
    hdos = new DataOutputStream(hpos);
    headerStream = new PipedInputStream(hpos);
    // Write the magic number
    hdos.writeInt(MThd_MAGIC);
    // Write the header length
    hdos.writeInt(headerLength - 8);
    // Write the filetype
    if (type == MIDI_TYPE_0) {
        hdos.writeShort(0);
    } else {
        // MIDI_TYPE_1
        hdos.writeShort(1);
    }
    // Write the number of tracks
    hdos.writeShort((short) trackCount);
    // Determine and write the timing format
    divtype = sequence.getDivisionType();
    if (divtype == Sequence.PPQ) {
        timeFormat = sequence.getResolution();
    } else if (divtype == Sequence.SMPTE_24) {
        timeFormat = (24 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_25) {
        timeFormat = (25 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30DROP) {
        timeFormat = (29 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30) {
        timeFormat = (30 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else {
        // $$jb: 04.08.99: What to really do here?
        return null;
    }
    hdos.writeShort(timeFormat);
    // now construct an InputStream to become the FileStream
    fStream = new SequenceInputStream(headerStream, trackStream);
    hdos.close();
    length = bytesBuilt + headerLength;
    return fStream;
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) SequenceInputStream(java.io.SequenceInputStream) DataOutputStream(java.io.DataOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Track(javax.sound.midi.Track)

Example 47 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class DCCppStreamPortControllerTest method setUp.

// The minimal setup for log4J
@Override
@Before
public void setUp() {
    apps.tests.Log4JFixture.setUp();
    JUnitUtil.resetInstanceManager();
    try {
        PipedInputStream tempPipe;
        tempPipe = new PipedInputStream();
        DataOutputStream ostream = new DataOutputStream(new PipedOutputStream(tempPipe));
        tempPipe = new PipedInputStream();
        DataInputStream istream = new DataInputStream(tempPipe);
        apc = new DCCppStreamPortController(istream, ostream, "Test");
    } catch (java.io.IOException ioe) {
        Assert.fail("IOException creating stream");
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 48 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class XNetStreamPortControllerTest method setUp.

// The minimal setup for log4J
@Override
@Before
public void setUp() {
    apps.tests.Log4JFixture.setUp();
    JUnitUtil.resetInstanceManager();
    try {
        PipedInputStream tempPipe;
        tempPipe = new PipedInputStream();
        DataOutputStream ostream = new DataOutputStream(new PipedOutputStream(tempPipe));
        tempPipe = new PipedInputStream();
        DataInputStream istream = new DataInputStream(tempPipe);
        apc = new XNetStreamPortController(istream, ostream, "Test");
    } catch (java.io.IOException ioe) {
        Assert.fail("IOException creating stream");
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 49 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class Z21XNetStreamPortControllerTest method setUp.

// The minimal setup for log4J
@Override
@Before
public void setUp() {
    apps.tests.Log4JFixture.setUp();
    JUnitUtil.resetInstanceManager();
    try {
        PipedInputStream tempPipe;
        tempPipe = new PipedInputStream();
        DataOutputStream ostream = new DataOutputStream(new PipedOutputStream(tempPipe));
        tempPipe = new PipedInputStream();
        DataInputStream istream = new DataInputStream(tempPipe);
        apc = new Z21XNetStreamPortController(istream, ostream, "Test");
    } catch (java.io.IOException ioe) {
        Assert.fail("IOException creating stream");
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 50 with PipedInputStream

use of java.io.PipedInputStream in project JMRI by JMRI.

the class SimulatorAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    try {
        PipedOutputStream tempPipeI = new PipedOutputStream();
        pout = new DataOutputStream(tempPipeI);
        inpipe = new DataInputStream(new PipedInputStream(tempPipeI));
        PipedOutputStream tempPipeO = new PipedOutputStream();
        outpipe = new DataOutputStream(tempPipeO);
        pin = new DataInputStream(new PipedInputStream(tempPipeO));
    } catch (java.io.IOException e) {
        log.error("init (pipe): Exception: " + e.toString());
    }
    opened = true;
    // indicates OK return
    return null;
}
Also used : DataOutputStream(java.io.DataOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputStream(java.io.DataInputStream)

Aggregations

PipedInputStream (java.io.PipedInputStream)128 PipedOutputStream (java.io.PipedOutputStream)121 IOException (java.io.IOException)42 Test (org.junit.Test)33 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 DataInputStream (java.io.DataInputStream)13 DataOutputStream (java.io.DataOutputStream)13 InputStream (java.io.InputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Before (org.junit.Before)10 OutputStream (java.io.OutputStream)9 PrintStream (java.io.PrintStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5