Skip to content

Commit 7f2ad30

Browse files
committed
Use InputStream.transferTo method.
1 parent a557d73 commit 7f2ad30

File tree

5 files changed

+41
-86
lines changed

5 files changed

+41
-86
lines changed

org.monte.media/src/main/java/org.monte.media/org/monte/media/io/IOStreams.java

-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.io.InputStream;
11-
import java.io.OutputStream;
1211
import java.nio.file.Files;
1312
import java.nio.file.StandardCopyOption;
1413

@@ -29,25 +28,6 @@ public static void copy(File source, File target) throws IOException {
2928
Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
3029
}
3130

32-
/**
33-
* Copies the remainder of the source stream into the provided target
34-
* stream.
35-
*
36-
* @param source the source stream
37-
* @param target the target stream
38-
* @return number of copied bytes
39-
* @throws IOException if an I/O error occurs
40-
*/
41-
public static long copy(InputStream source, OutputStream target) throws IOException {
42-
long n = 0L;
43-
byte[] b = new byte[8192];
44-
for (int count = source.read(b); count != -1; count = source.read(b)) {
45-
target.write(b, 0, count);
46-
n += count;
47-
}
48-
return n;
49-
}
50-
5131
/**
5232
* Copies the remainder of the source stream into the provided target
5333
* stream.

org.monte.media/src/main/java/org.monte.media/org/monte/media/jpeg/CMYKJPEGImageReader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import org.monte.media.image.CMYKImages;
88
import org.monte.media.io.ByteArrayImageInputStream;
9-
import org.monte.media.io.IOStreams;
109
import org.monte.media.io.ImageInputStreamAdapter;
1110
import org.monte.media.jfif.JFIFInputStream;
1211

@@ -225,7 +224,7 @@ public static BufferedImage read(ImageInputStream in, boolean inverseYCCKColors,
225224

226225
// Read Adobe ICC_PROFILE int buffer. The profile is split up over
227226
// multiple APP2 marker segments.
228-
IOStreams.copy(dis, app2ICCProfile);
227+
dis.transferTo(app2ICCProfile);
229228
}
230229
}
231230
} else if (seg.marker == 0xffee) {

org.monte.media/src/main/java/org.monte.media/org/monte/media/mjpg/MJPGImageReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Reads an image in the Motion JPEG (MJPG) format.
2626
* <p>
27-
* . This class can read Motion JPEG files with omitted Huffmann table.
27+
* This class can read Motion JPEG files with omitted Huffmann table.
2828
* <p>
2929
* For more information see: Microsoft Windows Bitmap Format. Multimedia
3030
* Technical Note: JPEG DIB Format. (c) 1993 Microsoft Corporation. All rights

org.monte.media/src/main/java/org.monte.media/org/monte/media/quicktime/QuickTimeOutputStream.java

+18-27
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package org.monte.media.quicktime;
66

77
import org.monte.media.av.Format;
8-
import org.monte.media.av.FormatKeys.MediaType;
9-
import org.monte.media.io.IOStreams;
108
import org.monte.media.io.ImageOutputStreamAdapter;
119
import org.monte.media.math.Rational;
1210

@@ -29,6 +27,7 @@
2927
import static org.monte.media.av.FormatKeys.EncodingKey;
3028
import static org.monte.media.av.FormatKeys.FrameRateKey;
3129
import static org.monte.media.av.FormatKeys.MIME_QUICKTIME;
30+
import static org.monte.media.av.FormatKeys.MediaType;
3231
import static org.monte.media.av.FormatKeys.MediaTypeKey;
3332
import static org.monte.media.av.FormatKeys.MimeTypeKey;
3433
import static org.monte.media.av.codec.audio.AudioFormatKeys.ByteOrderKey;
@@ -350,7 +349,7 @@ public int addAudioTrack(String compressionType, //
350349
* encoded with lossless encoders such as the PNG format. <p> The default
351350
* value is 0.97.
352351
*
353-
* @param newValue
352+
* @param newValue the new value
354353
*/
355354
public void setCompressionQuality(int track, float newValue) {
356355
VideoTrack vt = (VideoTrack) tracks.get(track);
@@ -374,14 +373,14 @@ public float getCompressionQuality(int track) {
374373
* write all samples as sync samples. n = sync every n-th sample.
375374
*/
376375
public void setSyncInterval(int track, int i) {
377-
((VideoTrack) tracks.get(track)).syncInterval = i;
376+
tracks.get(track).syncInterval = i;
378377
}
379378

380379
/**
381380
* Gets the sync interval from the specified video track.
382381
*/
383382
public int getSyncInterval(int track) {
384-
return ((VideoTrack) tracks.get(track)).syncInterval;
383+
return tracks.get(track).syncInterval;
385384
}
386385

387386
/**
@@ -653,7 +652,7 @@ public void writeSample(int track, InputStream in, long duration, boolean isSync
653652
ensureStarted();
654653
long offset = getRelativeStreamPosition();
655654
OutputStream mdatOut = mdatAtom.getOutputStream();
656-
IOStreams.copy(in, mdatOut);
655+
in.transferTo(mdatOut);
657656
long length = getRelativeStreamPosition() - offset;
658657
t.addSample(new Sample(duration, offset, length), 1, isSync);
659658
}
@@ -1018,10 +1017,8 @@ private void writeEpilog() throws IOException {
10181017
}
10191018

10201019
// Optional color table atom
1021-
for (int i = 0, n = tracks.size(); i < n; i++) {
1022-
Track t = tracks.get(i);
1023-
if (t instanceof VideoTrack) {
1024-
VideoTrack vt = (VideoTrack) t;
1020+
for (Track t : tracks) {
1021+
if (t instanceof VideoTrack vt) {
10251022
if (vt.videoColorTable != null) {
10261023
vt.writeColorTableAtom(moovAtom);
10271024
break;
@@ -1158,10 +1155,10 @@ protected void writeTrackAtoms(int trackIndex, CompositeAtom moovAtom, Date modi
11581155
// See Figure 2-8 for an illustration of a matrix structure:
11591156
// http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap2/chapter_3_section_3.html#//apple_ref/doc/uid/TP40000939-CH204-32967
11601157

1161-
d.writeFixed16D16(t.mediaType == MediaType.VIDEO ? ((VideoTrack) t).width : 0); // width
1158+
d.writeFixed16D16(t.mediaType == MediaType.VIDEO ? t.width : 0); // width
11621159
// A 32-bit fixed-point number that specifies the width of this track in pixels.
11631160

1164-
d.writeFixed16D16(t.mediaType == MediaType.VIDEO ? ((VideoTrack) t).height : 0); // height
1161+
d.writeFixed16D16(t.mediaType == MediaType.VIDEO ? t.height : 0); // height
11651162
// A 32-bit fixed-point number that indicates the height of this track in pixels.
11661163

11671164
/* Edit Atom ========= */
@@ -1202,10 +1199,10 @@ protected void writeTrackAtoms(int trackIndex, CompositeAtom moovAtom, Date modi
12021199
d.writeFixed16D16(1); // mediaRate
12031200
} else {
12041201
d.writeUInt(elist.length); // numberOfEntries
1205-
for (int i = 0; i < elist.length; ++i) {
1206-
d.writeUInt(elist[i].trackDuration); // trackDuration
1207-
d.writeUInt(elist[i].mediaTime); // mediaTime
1208-
d.writeUInt(elist[i].mediaRate); // mediaRate
1202+
for (Edit edit : elist) {
1203+
d.writeUInt(edit.trackDuration); // trackDuration
1204+
d.writeUInt(edit.mediaTime); // mediaTime
1205+
d.writeUInt(edit.mediaRate); // mediaRate
12091206
}
12101207
}
12111208

@@ -1265,7 +1262,7 @@ protected void writeTrackAtoms(int trackIndex, CompositeAtom moovAtom, Date modi
12651262
// A 16-bit integer that specifies the media’s playback quality—that is,
12661263
// its suitability for playback in a given environment.
12671264

1268-
/**
1265+
/*
12691266
* Media Handler Reference Atom -------
12701267
*/
12711268
leaf = new DataAtom("hdlr");
@@ -1336,14 +1333,9 @@ protected void writeMediaInformationAtoms(int trackIndex, CompositeAtom mdiaAtom
13361333

13371334
/* Video or Audio media information atom -------- */
13381335
switch (t.mediaType) {
1339-
case VIDEO:
1340-
writeVideoMediaInformationHeaderAtom(trackIndex, minfAtom);
1341-
break;
1342-
case AUDIO:
1343-
writeSoundMediaInformationHeaderAtom(trackIndex, minfAtom);
1344-
break;
1345-
default:
1346-
throw new UnsupportedOperationException("Media type " + t.mediaType + " not supported yet.");
1336+
case VIDEO -> writeVideoMediaInformationHeaderAtom(trackIndex, minfAtom);
1337+
case AUDIO -> writeSoundMediaInformationHeaderAtom(trackIndex, minfAtom);
1338+
default -> throw new UnsupportedOperationException("Media type " + t.mediaType + " not supported yet.");
13471339
}
13481340

13491341

@@ -1920,7 +1912,7 @@ public void toWebOptimizedMovie(File outputFile, boolean compressHeader) throws
19201912
}
19211913
}
19221914

1923-
if (maxIteration < 0 || buf.size() == 0) {
1915+
if (buf.size() == 0) {
19241916
compressHeader = false;
19251917
System.err.println("WARNING QuickTimeWriter failed to compress header.");
19261918
} else {
@@ -1952,7 +1944,6 @@ public void toWebOptimizedMovie(File outputFile, boolean compressHeader) throws
19521944
daos.write(0);
19531945
}
19541946
}
1955-
19561947
}
19571948
if (!compressHeader) {
19581949
out = new FileImageOutputStream(outputFile);

org.monte.media/src/main/java/org.monte.media/org/monte/media/quicktime/QuickTimeWriter.java

+21-36
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ public int addTrack(Format fmt) throws IOException {
264264
* @throws IllegalArgumentException if the width or the height is smaller
265265
* than 1.
266266
*/
267-
@Deprecated
268267
public int addVideoTrack(Format format, long timeScale, int width, int height) throws IOException {
269268
return addVideoTrack(format.get(EncodingKey), format.get(CompressorNameKey), timeScale, width, height, 24, 30);
270269
}
@@ -279,7 +278,6 @@ public int addVideoTrack(Format format, long timeScale, int width, int height) t
279278
* @throws IllegalArgumentException if the width or the height is smaller
280279
* than 1.
281280
*/
282-
@Deprecated
283281
public int addVideoTrack(Format format, int width, int height, int depth, int syncInterval) throws IOException {
284282
return addVideoTrack(format.get(EncodingKey), format.get(CompressorNameKey), format.get(FrameRateKey).getDenominator() * format.get(FrameRateKey).getNumerator(), width, height, depth, syncInterval);
285283
}
@@ -294,7 +292,6 @@ public int addVideoTrack(Format format, int width, int height, int depth, int sy
294292
* @param format The javax.sound audio format.
295293
* @return Returns the track index.
296294
*/
297-
@Deprecated
298295
public int addAudioTrack(javax.sound.sampled.AudioFormat format) throws IOException {
299296
ensureStarted();
300297
String qtAudioFormat;
@@ -314,45 +311,33 @@ public int addAudioTrack(javax.sound.sampled.AudioFormat format) throws IOExcept
314311
throw new IllegalArgumentException("Sample size of 8 for ALAW required:" + sampleSizeInBits);
315312
}
316313
} else if (javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED.equals(enc)) {
317-
switch (sampleSizeInBits) {
318-
case 8:// Requires conversion to PCM_UNSIGNED!
319-
qtAudioFormat = "raw ";
320-
break;
321-
case 16:
322-
qtAudioFormat = (byteOrder == ByteOrder.BIG_ENDIAN) ? "twos" : "sowt";
323-
break;
324-
case 24:
325-
qtAudioFormat = "in24";
326-
break;
327-
case 32:
328-
qtAudioFormat = "in32";
329-
break;
330-
default:
331-
throw new IllegalArgumentException("Unsupported sample size for PCM_SIGNED:" + sampleSizeInBits);
332-
}
314+
qtAudioFormat = switch (sampleSizeInBits) {
315+
case 8 ->// Requires conversion to PCM_UNSIGNED!
316+
"raw ";
317+
case 16 -> (byteOrder == ByteOrder.BIG_ENDIAN) ? "twos" : "sowt";
318+
case 24 -> "in24";
319+
case 32 -> "in32";
320+
default ->
321+
throw new IllegalArgumentException("Unsupported sample size for PCM_SIGNED:" + sampleSizeInBits);
322+
};
333323
} else if (javax.sound.sampled.AudioFormat.Encoding.PCM_UNSIGNED.equals(enc)) {
334-
switch (sampleSizeInBits) {
335-
case 8:
336-
qtAudioFormat = "raw ";
337-
break;
338-
case 16:// Requires conversion to PCM_SIGNED!
339-
qtAudioFormat = (byteOrder == ByteOrder.BIG_ENDIAN) ? "twos" : "sowt";
340-
break;
341-
case 24:// Requires conversion to PCM_SIGNED!
342-
qtAudioFormat = "in24";
343-
break;
344-
case 32:// Requires conversion to PCM_SIGNED!
345-
qtAudioFormat = "in32";
346-
break;
347-
default:
348-
throw new IllegalArgumentException("Unsupported sample size for PCM_UNSIGNED:" + sampleSizeInBits);
349-
}
324+
qtAudioFormat = switch (sampleSizeInBits) {
325+
case 8 -> "raw ";
326+
case 16 ->// Requires conversion to PCM_SIGNED!
327+
(byteOrder == ByteOrder.BIG_ENDIAN) ? "twos" : "sowt";
328+
case 24 ->// Requires conversion to PCM_SIGNED!
329+
"in24";
330+
case 32 ->// Requires conversion to PCM_SIGNED!
331+
"in32";
332+
default ->
333+
throw new IllegalArgumentException("Unsupported sample size for PCM_UNSIGNED:" + sampleSizeInBits);
334+
};
350335
} else if (javax.sound.sampled.AudioFormat.Encoding.ULAW.equals(enc)) {
351336
if (sampleSizeInBits != 8) {
352337
throw new IllegalArgumentException("Sample size of 8 for ULAW required:" + sampleSizeInBits);
353338
}
354339
qtAudioFormat = "ulaw";
355-
} else if ("MP3".equals(enc == null ? null : enc.toString())) {
340+
} else if ("MP3".equals(enc.toString())) {
356341
qtAudioFormat = ".mp3";
357342
} else {
358343
qtAudioFormat = format.getEncoding().toString();

0 commit comments

Comments
 (0)