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;
}
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");
}
}
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");
}
}
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");
}
}
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;
}
Aggregations