use of java.io.PipedInputStream 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;
}
use of java.io.PipedInputStream in project jdk8u_jdk by JetBrains.
the class MyNullCipherOutputStream method chainTest.
/**
* Chain CipherInputStream/CipherOutputStream with other stream, encrypt
* the text and decrypt it, recovered text is supposed to be same as
* original text.
* @param useInt true if read byte by byte false if read with buffer.
* @throws IOException any I/O operation failed.
*/
public void chainTest(boolean useInt) throws IOException {
byte[] plainText = TestUtilities.generateBytes(PLAIN_TEXT_LENGTH);
byte[] recoveredText = new byte[plainText.length];
// Do initialization
try (MyNullCipherInputStream ciInput1 = new MyNullCipherInputStream(new ByteArrayInputStream(plainText));
PipedOutputStream piOut = new PipedOutputStream();
MyNullCipherInputStream ciInput2 = new MyNullCipherInputStream(new PipedInputStream(piOut));
MyNullCipherOutputStream ciOut = new MyNullCipherOutputStream(piOut)) {
if (useInt) {
int buffer = ciInput1.read();
while (buffer != -1) {
piOut.write(buffer);
buffer = ciInput1.read();
}
} else {
byte[] buffer = new byte[20];
int len = ciInput1.read(buffer);
while (len != -1) {
ciOut.write(buffer, 0, len);
len = ciInput1.read(buffer);
}
}
ciOut.flush();
piOut.flush();
// Get the output
ciInput2.read(recoveredText);
if (ciInput2.available() > 0) {
throw new RuntimeException("Expected no data from ciInput2, but" + " ciInput2.available() = " + ciInput2.available());
}
}
// Verify output is same to input.
if (!Arrays.equals(plainText, recoveredText)) {
throw new RuntimeException("plainText:" + new String(plainText) + " recoveredText:" + new String(recoveredText) + " Test failed due to result compare fail");
}
}
use of java.io.PipedInputStream in project jdk8u_jdk by JetBrains.
the class JarBackSlash method testJarExtract.
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String[] jarArgs = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
use of java.io.PipedInputStream in project symmetric-ds by JumpMind.
the class InternalTransportManager method getPullTransport.
public IIncomingTransport getPullTransport(Node remote, final Node local, String securityToken, Map<String, String> requestProperties, String registrationUrl) throws IOException {
final PipedOutputStream respOs = new PipedOutputStream();
final PipedInputStream respIs = new PipedInputStream(respOs);
final ChannelMap suspendIgnoreChannels = symmetricEngine.getConfigurationService().getSuspendIgnoreChannelLists(remote.getNodeId());
runAtClient(remote.getSyncUrl(), null, respOs, new IClientRunnable() {
public void run(ISymmetricEngine engine, InputStream is, OutputStream os) throws Exception {
IOutgoingTransport transport = new InternalOutgoingTransport(respOs, suspendIgnoreChannels, IoConstants.ENCODING);
ProcessInfo processInfo = engine.getStatisticManager().newProcessInfo(new ProcessInfoKey(engine.getNodeService().findIdentityNodeId(), local.getNodeId(), ProcessType.PULL_HANDLER));
try {
engine.getDataExtractorService().extract(processInfo, local, transport);
processInfo.setStatus(Status.OK);
} catch (RuntimeException ex) {
processInfo.setStatus(Status.ERROR);
throw ex;
}
transport.close();
}
});
return new InternalIncomingTransport(respIs);
}
use of java.io.PipedInputStream in project symmetric-ds by JumpMind.
the class InternalTransportManager method getRegisterTransport.
public IIncomingTransport getRegisterTransport(final Node client, String registrationUrl) throws IOException {
final PipedOutputStream respOs = new PipedOutputStream();
final PipedInputStream respIs = new PipedInputStream(respOs);
runAtClient(registrationUrl, null, respOs, new IClientRunnable() {
public void run(ISymmetricEngine engine, InputStream is, OutputStream os) throws Exception {
// This should be basically what the registration servlet does
// ...
engine.getRegistrationService().registerNode(client, os, false);
}
});
return new InternalIncomingTransport(respIs);
}
Aggregations