use of java.io.SequenceInputStream in project jdk8u_jdk by JetBrains.
the class AuFileWriter method getFileStream.
private InputStream getFileStream(AuFileFormat auFileFormat, InputStream audioStream) throws IOException {
// private method ... assumes auFileFormat is a supported file type
AudioFormat format = auFileFormat.getFormat();
int magic = AuFileFormat.AU_SUN_MAGIC;
int headerSize = AuFileFormat.AU_HEADERSIZE;
long dataSize = auFileFormat.getFrameLength();
//$$fb fix for Bug 4351296
//int dataSizeInBytes = dataSize * format.getFrameSize();
long dataSizeInBytes = (dataSize == AudioSystem.NOT_SPECIFIED) ? UNKNOWN_SIZE : dataSize * format.getFrameSize();
if (dataSizeInBytes > 0x7FFFFFFFl) {
dataSizeInBytes = UNKNOWN_SIZE;
}
int encoding_local = auFileFormat.getAuType();
int sampleRate = (int) format.getSampleRate();
int channels = format.getChannels();
//$$fb below is the fix for 4297100.
//boolean bigendian = format.isBigEndian();
// force bigendian
boolean bigendian = true;
byte[] header = null;
ByteArrayInputStream headerStream = null;
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
SequenceInputStream auStream = null;
AudioFormat audioStreamFormat = null;
AudioFormat.Encoding encoding = null;
InputStream codedAudioStream = audioStream;
// if we need to do any format conversion, do it here.
codedAudioStream = audioStream;
if (audioStream instanceof AudioInputStream) {
audioStreamFormat = ((AudioInputStream) audioStream).getFormat();
encoding = audioStreamFormat.getEncoding();
//$$ fb 2001-07-13: Bug 4391108
if ((AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) || (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && bigendian != audioStreamFormat.isBigEndian())) {
// plug in the transcoder to convert to PCM_SIGNED, bigendian
// NOTE: little endian AU is not common, so we're always converting
// to big endian unless the passed in audioFileFormat is little.
// $$fb this NOTE is superseded. We always write big endian au files, this is by far the standard.
codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioStreamFormat.getSampleRate(), audioStreamFormat.getSampleSizeInBits(), audioStreamFormat.getChannels(), audioStreamFormat.getFrameSize(), audioStreamFormat.getFrameRate(), bigendian), (AudioInputStream) audioStream);
}
}
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
if (bigendian) {
dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
dos.writeInt(headerSize);
dos.writeInt((int) dataSizeInBytes);
dos.writeInt(encoding_local);
dos.writeInt(sampleRate);
dos.writeInt(channels);
} else {
dos.writeInt(AuFileFormat.AU_SUN_INV_MAGIC);
dos.writeInt(big2little(headerSize));
dos.writeInt(big2little((int) dataSizeInBytes));
dos.writeInt(big2little(encoding_local));
dos.writeInt(big2little(sampleRate));
dos.writeInt(big2little(channels));
}
// Now create a new InputStream from headerStream and the InputStream
// in audioStream
dos.close();
header = baos.toByteArray();
headerStream = new ByteArrayInputStream(header);
auStream = new SequenceInputStream(headerStream, new NoCloseInputStream(codedAudioStream));
return auStream;
}
use of java.io.SequenceInputStream in project jdk8u_jdk by JetBrains.
the class AiffFileWriter method getFileStream.
private InputStream getFileStream(AiffFileFormat aiffFileFormat, InputStream audioStream) throws IOException {
// private method ... assumes aiffFileFormat is a supported file format
AudioFormat format = aiffFileFormat.getFormat();
AudioFormat streamFormat = null;
AudioFormat.Encoding encoding = null;
//$$fb a little bit nicer handling of constants
//int headerSize = 54;
int headerSize = aiffFileFormat.getHeaderSize();
//int fverChunkSize = 0;
int fverChunkSize = aiffFileFormat.getFverChunkSize();
//int commChunkSize = 26;
int commChunkSize = aiffFileFormat.getCommChunkSize();
int aiffLength = -1;
int ssndChunkSize = -1;
//int ssndOffset = headerSize - 16;
int ssndOffset = aiffFileFormat.getSsndChunkOffset();
short channels = (short) format.getChannels();
short sampleSize = (short) format.getSampleSizeInBits();
int ssndBlockSize = (channels * sampleSize);
int numFrames = aiffFileFormat.getFrameLength();
long dataSize = -1;
if (numFrames != AudioSystem.NOT_SPECIFIED) {
dataSize = (long) numFrames * ssndBlockSize / 8;
ssndChunkSize = (int) dataSize + 16;
aiffLength = (int) dataSize + headerSize;
}
float sampleFramesPerSecond = format.getSampleRate();
int compCode = AiffFileFormat.AIFC_PCM;
byte[] header = null;
ByteArrayInputStream headerStream = null;
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
SequenceInputStream aiffStream = null;
InputStream codedAudioStream = audioStream;
if (audioStream instanceof AudioInputStream) {
streamFormat = ((AudioInputStream) audioStream).getFormat();
encoding = streamFormat.getEncoding();
// $$jb: Note that AIFF samples are ALWAYS signed
if ((AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) || ((AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian())) {
// plug in the transcoder to convert to PCM_SIGNED. big endian
codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, streamFormat.getSampleRate(), streamFormat.getSampleSizeInBits(), streamFormat.getChannels(), streamFormat.getFrameSize(), streamFormat.getFrameRate(), true), (AudioInputStream) audioStream);
} else if ((AudioFormat.Encoding.ULAW.equals(encoding)) || (AudioFormat.Encoding.ALAW.equals(encoding))) {
if (streamFormat.getSampleSizeInBits() != 8) {
throw new IllegalArgumentException("unsupported encoding");
}
//$$fb 2001-07-13: this is probably not what we want:
// writing PCM when ULAW/ALAW is requested. AIFC is able to write ULAW !
// plug in the transcoder to convert to PCM_SIGNED_BIG_ENDIAN
codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, streamFormat.getSampleRate(), streamFormat.getSampleSizeInBits() * 2, streamFormat.getChannels(), streamFormat.getFrameSize() * 2, streamFormat.getFrameRate(), true), (AudioInputStream) audioStream);
}
}
// Now create an AIFF stream header...
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
// Write the outer FORM chunk
dos.writeInt(AiffFileFormat.AIFF_MAGIC);
dos.writeInt((aiffLength - 8));
dos.writeInt(AiffFileFormat.AIFF_MAGIC2);
// Write a FVER chunk - only for AIFC
//dos.writeInt(FVER_MAGIC);
//dos.writeInt( (fverChunkSize-8) );
//dos.writeInt(FVER_TIMESTAMP);
// Write a COMM chunk
dos.writeInt(AiffFileFormat.COMM_MAGIC);
dos.writeInt((commChunkSize - 8));
dos.writeShort(channels);
dos.writeInt(numFrames);
dos.writeShort(sampleSize);
// 10 bytes
write_ieee_extended(dos, sampleFramesPerSecond);
//Only for AIFC
//dos.writeInt(compCode);
//dos.writeInt(compCode);
//dos.writeShort(0);
// Write the SSND chunk header
dos.writeInt(AiffFileFormat.SSND_MAGIC);
dos.writeInt((ssndChunkSize - 8));
// ssndOffset and ssndBlockSize set to 0 upon
// recommendation in "Sound Manager" chapter in
// "Inside Macintosh Sound", pp 2-87 (from Babu)
// ssndOffset
dos.writeInt(0);
// ssndBlockSize
dos.writeInt(0);
// Concat this with the audioStream and return it
dos.close();
header = baos.toByteArray();
headerStream = new ByteArrayInputStream(header);
aiffStream = new SequenceInputStream(headerStream, new NoCloseInputStream(codedAudioStream));
return aiffStream;
}
use of java.io.SequenceInputStream 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.SequenceInputStream in project jdk8u_jdk by JetBrains.
the class PNGImageReader method readImage.
private void readImage(ImageReadParam param) throws IIOException {
readMetadata();
int width = metadata.IHDR_width;
int height = metadata.IHDR_height;
// Init default values
sourceXSubsampling = 1;
sourceYSubsampling = 1;
sourceMinProgressivePass = 0;
sourceMaxProgressivePass = 6;
sourceBands = null;
destinationBands = null;
destinationOffset = new Point(0, 0);
// If an ImageReadParam is available, get values from it
if (param != null) {
sourceXSubsampling = param.getSourceXSubsampling();
sourceYSubsampling = param.getSourceYSubsampling();
sourceMinProgressivePass = Math.max(param.getSourceMinProgressivePass(), 0);
sourceMaxProgressivePass = Math.min(param.getSourceMaxProgressivePass(), 6);
sourceBands = param.getSourceBands();
destinationBands = param.getDestinationBands();
destinationOffset = param.getDestinationOffset();
}
Inflater inf = null;
try {
stream.seek(imageStartPosition);
Enumeration<InputStream> e = new PNGImageDataEnumeration(stream);
InputStream is = new SequenceInputStream(e);
/* InflaterInputStream uses an Inflater instance which consumes
* native (non-GC visible) resources. This is normally implicitly
* freed when the stream is closed. However since the
* InflaterInputStream wraps a client-supplied input stream,
* we cannot close it.
* But the app may depend on GC finalization to close the stream.
* Therefore to ensure timely freeing of native resources we
* explicitly create the Inflater instance and free its resources
* when we are done with the InflaterInputStream by calling
* inf.end();
*/
inf = new Inflater();
is = new InflaterInputStream(is, inf);
is = new BufferedInputStream(is);
this.pixelStream = new DataInputStream(is);
/*
* NB: the PNG spec declares that valid range for width
* and height is [1, 2^31-1], so here we may fail to allocate
* a buffer for destination image due to memory limitation.
*
* However, the recovery strategy for this case should be
* defined on the level of application, so we will not
* try to estimate the required amount of the memory and/or
* handle OOM in any way.
*/
theImage = getDestination(param, getImageTypes(0), width, height);
Rectangle destRegion = new Rectangle(0, 0, 0, 0);
sourceRegion = new Rectangle(0, 0, 0, 0);
computeRegions(param, width, height, theImage, sourceRegion, destRegion);
destinationOffset.setLocation(destRegion.getLocation());
// At this point the header has been read and we know
// how many bands are in the image, so perform checking
// of the read param.
int colorType = metadata.IHDR_colorType;
checkReadParamBandSettings(param, inputBandsForColorType[colorType], theImage.getSampleModel().getNumBands());
processImageStarted(0);
decodeImage();
if (abortRequested()) {
processReadAborted();
} else {
processImageComplete();
}
} catch (IOException e) {
throw new IIOException("Error reading PNG image data", e);
} finally {
if (inf != null) {
inf.end();
}
}
}
use of java.io.SequenceInputStream in project sling by apache.
the class SlingFileUploadHandler method mergeChunks.
/**
* Merge all previous chunks with last chunk's stream into a temporary file
* and return it.
*/
private File mergeChunks(final Resource parentResource, final InputStream lastChunkStream) throws PersistenceException {
OutputStream out = null;
SequenceInputStream mergeStrm = null;
File file = null;
try {
file = File.createTempFile("tmp-", "-mergechunk");
out = new FileOutputStream(file);
String startPattern = SlingPostConstants.CHUNK_NODE_NAME + "_" + "0_";
Iterator<Resource> itr = new FilteringResourceIterator(parentResource.listChildren(), startPattern);
final Set<InputStream> inpStrmSet = new LinkedHashSet<>();
while (itr.hasNext()) {
final Resource rangeResource = itr.next();
if (itr.hasNext()) {
throw new PersistenceException("more than one resource found for pattern: " + startPattern + "*");
}
inpStrmSet.add(rangeResource.adaptTo(InputStream.class));
log.debug("added chunk {} to merge stream", rangeResource.getName());
String[] indexBounds = rangeResource.getName().substring((SlingPostConstants.CHUNK_NODE_NAME + "_").length()).split("_");
startPattern = SlingPostConstants.CHUNK_NODE_NAME + "_" + String.valueOf(Long.valueOf(indexBounds[1]) + 1) + "_";
itr = new FilteringResourceIterator(parentResource.listChildren(), startPattern);
}
inpStrmSet.add(lastChunkStream);
mergeStrm = new SequenceInputStream(Collections.enumeration(inpStrmSet));
IOUtils.copyLarge(mergeStrm, out);
} catch (final IOException e) {
throw new PersistenceException("Exception during chunk merge occured: " + e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(mergeStrm);
}
return file;
}
Aggregations