Use final where possible

This commit is contained in:
wb9688 2020-08-16 10:24:58 +02:00
parent d306513319
commit 87228673b4
132 changed files with 1024 additions and 1005 deletions

View file

@ -70,7 +70,7 @@ public class DataReader {
}
public long readUnsignedInt() throws IOException {
long value = readInt();
final long value = readInt();
return value & 0xffffffffL;
}
@ -82,8 +82,9 @@ public class DataReader {
public long readLong() throws IOException {
primitiveRead(LONG_SIZE);
long high = primitive[0] << 24 | primitive[1] << 16 | primitive[2] << 8 | primitive[3];
long low = primitive[4] << 24 | primitive[5] << 16 | primitive[6] << 8 | primitive[7];
final long high
= primitive[0] << 24 | primitive[1] << 16 | primitive[2] << 8 | primitive[3];
final long low = primitive[4] << 24 | primitive[5] << 16 | primitive[6] << 8 | primitive[7];
return high << 32 | low;
}
@ -114,7 +115,7 @@ public class DataReader {
total += Math.max(stream.read(buffer, offset, count), 0);
} else {
while (count > 0 && !fillBuffer()) {
int read = Math.min(readCount, count);
final int read = Math.min(readCount, count);
System.arraycopy(readBuffer, readOffset, buffer, offset, read);
readOffset += read;
@ -169,7 +170,7 @@ public class DataReader {
if (viewSize < 1) {
return -1;
}
int res = DataReader.this.read();
final int res = DataReader.this.read();
if (res > 0) {
viewSize--;
}
@ -188,7 +189,7 @@ public class DataReader {
return -1;
}
int res = DataReader.this.read(buffer, offset, Math.min(viewSize, count));
final int res = DataReader.this.read(buffer, offset, Math.min(viewSize, count));
viewSize -= res;
return res;
@ -199,7 +200,7 @@ public class DataReader {
if (viewSize < 1) {
return 0;
}
int res = (int) DataReader.this.skipBytes(Math.min(amount, viewSize));
final int res = (int) DataReader.this.skipBytes(Math.min(amount, viewSize));
viewSize -= res;
return res;
@ -230,8 +231,8 @@ public class DataReader {
private final short[] primitive = new short[LONG_SIZE];
private void primitiveRead(final int amount) throws IOException {
byte[] buffer = new byte[amount];
int read = read(buffer, 0, amount);
final byte[] buffer = new byte[amount];
final int read = read(buffer, 0, amount);
if (read != amount) {
throw new EOFException("Truncated stream, missing "

View file

@ -116,7 +116,7 @@ public class Mp4DashReader {
tracks[i].trak = moov.trak[i];
if (moov.mvexTrex != null) {
for (Trex mvexTrex : moov.mvexTrex) {
for (final Trex mvexTrex : moov.mvexTrex) {
if (tracks[i].trak.tkhd.trackId == mvexTrex.trackId) {
tracks[i].trex = mvexTrex;
}
@ -174,7 +174,7 @@ public class Mp4DashReader {
}
public Mp4DashChunk getNextChunk(final boolean infoOnly) throws IOException {
Mp4Track track = tracks[selectedTrack];
final Mp4Track track = tracks[selectedTrack];
while (stream.available()) {
@ -233,7 +233,7 @@ public class Mp4DashReader {
continue; // find another chunk
}
Mp4DashChunk chunk = new Mp4DashChunk();
final Mp4DashChunk chunk = new Mp4DashChunk();
chunk.moof = moof;
if (!infoOnly) {
chunk.data = stream.getView(moof.traf.trun.chunkSize);
@ -261,13 +261,13 @@ public class Mp4DashReader {
private String boxName(final int type) {
try {
return new String(ByteBuffer.allocate(4).putInt(type).array(), "UTF-8");
} catch (UnsupportedEncodingException e) {
} catch (final UnsupportedEncodingException e) {
return "0x" + Integer.toHexString(type);
}
}
private Box readBox() throws IOException {
Box b = new Box();
final Box b = new Box();
b.offset = stream.position();
b.size = stream.readUnsignedInt();
b.type = stream.readInt();
@ -280,7 +280,7 @@ public class Mp4DashReader {
}
private Box readBox(final int expected) throws IOException {
Box b = readBox();
final Box b = readBox();
if (b.type != expected) {
throw new NoSuchElementException("expected " + boxName(expected)
+ " found " + boxName(b));
@ -290,13 +290,13 @@ public class Mp4DashReader {
private byte[] readFullBox(final Box ref) throws IOException {
// full box reading is limited to 2 GiB, and should be enough
int size = (int) ref.size;
final int size = (int) ref.size;
ByteBuffer buffer = ByteBuffer.allocate(size);
final ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.putInt(size);
buffer.putInt(ref.type);
int read = size - 8;
final int read = size - 8;
if (stream.read(buffer.array(), 8, read) != read) {
throw new EOFException(String.format("EOF reached in box: type=%s offset=%s size=%s",
@ -307,7 +307,7 @@ public class Mp4DashReader {
}
private void ensure(final Box ref) throws IOException {
long skip = ref.offset + ref.size - stream.position();
final long skip = ref.offset + ref.size - stream.position();
if (skip == 0) {
return;
@ -325,7 +325,7 @@ public class Mp4DashReader {
Box b;
while (stream.position() < (ref.offset + ref.size)) {
b = readBox();
for (int type : expected) {
for (final int type : expected) {
if (b.type == type) {
return b;
}
@ -345,7 +345,7 @@ public class Mp4DashReader {
}
private Moof parseMoof(final Box ref, final int trackId) throws IOException {
Moof obj = new Moof();
final Moof obj = new Moof();
Box b = readBox(ATOM_MFHD);
obj.mfhdSequenceNumber = parseMfhd();
@ -372,7 +372,7 @@ public class Mp4DashReader {
}
private Traf parseTraf(final Box ref, final int trackId) throws IOException {
Traf traf = new Traf();
final Traf traf = new Traf();
Box b = readBox(ATOM_TFHD);
traf.tfhd = parseTfhd(trackId);
@ -397,7 +397,7 @@ public class Mp4DashReader {
}
private Tfhd parseTfhd(final int trackId) throws IOException {
Tfhd obj = new Tfhd();
final Tfhd obj = new Tfhd();
obj.bFlags = stream.readInt();
obj.trackId = stream.readInt();
@ -426,13 +426,13 @@ public class Mp4DashReader {
}
private long parseTfdt() throws IOException {
int version = stream.read();
final int version = stream.read();
stream.skipBytes(3); // flags
return version == 0 ? stream.readUnsignedInt() : stream.readLong();
}
private Trun parseTrun() throws IOException {
Trun obj = new Trun();
final Trun obj = new Trun();
obj.bFlags = stream.readInt();
obj.entryCount = stream.readInt(); // unsigned int
@ -461,7 +461,7 @@ public class Mp4DashReader {
stream.read(obj.bEntries);
for (int i = 0; i < obj.entryCount; i++) {
TrunEntry entry = obj.getEntry(i);
final TrunEntry entry = obj.getEntry(i);
if (hasFlag(obj.bFlags, 0x0100)) {
obj.chunkDuration += entry.sampleDuration;
}
@ -480,7 +480,7 @@ public class Mp4DashReader {
private int[] parseFtyp(final Box ref) throws IOException {
int i = 0;
int[] list = new int[(int) ((ref.offset + ref.size - stream.position() - 4) / 4)];
final int[] list = new int[(int) ((ref.offset + ref.size - stream.position() - 4) / 4)];
list[i++] = stream.readInt(); // major brand
@ -494,14 +494,14 @@ public class Mp4DashReader {
}
private Mvhd parseMvhd() throws IOException {
int version = stream.read();
final int version = stream.read();
stream.skipBytes(3); // flags
// creation entries_time
// modification entries_time
stream.skipBytes(2 * (version == 0 ? 4 : 8));
Mvhd obj = new Mvhd();
final Mvhd obj = new Mvhd();
obj.timeScale = stream.readUnsignedInt();
// chunkDuration
@ -520,9 +520,9 @@ public class Mp4DashReader {
}
private Tkhd parseTkhd() throws IOException {
int version = stream.read();
final int version = stream.read();
Tkhd obj = new Tkhd();
final Tkhd obj = new Tkhd();
// flags
// creation entries_time
@ -553,7 +553,7 @@ public class Mp4DashReader {
}
private Trak parseTrak(final Box ref) throws IOException {
Trak trak = new Trak();
final Trak trak = new Trak();
Box b = readBox(ATOM_TKHD);
trak.tkhd = parseTkhd();
@ -576,7 +576,7 @@ public class Mp4DashReader {
}
private Mdia parseMdia(final Box ref) throws IOException {
Mdia obj = new Mdia();
final Mdia obj = new Mdia();
Box b;
while ((b = untilBox(ref, ATOM_MDHD, ATOM_HDLR, ATOM_MINF)) != null) {
@ -585,8 +585,8 @@ public class Mp4DashReader {
obj.mdhd = readFullBox(b);
// read time scale
ByteBuffer buffer = ByteBuffer.wrap(obj.mdhd);
byte version = buffer.get(8);
final ByteBuffer buffer = ByteBuffer.wrap(obj.mdhd);
final byte version = buffer.get(8);
buffer.position(12 + ((version == 0 ? 4 : 8) * 2));
obj.mdhdTimeScale = buffer.getInt();
break;
@ -608,7 +608,7 @@ public class Mp4DashReader {
// flags
stream.skipBytes(4);
Hdlr obj = new Hdlr();
final Hdlr obj = new Hdlr();
obj.bReserved = new byte[12];
obj.type = stream.readInt();
@ -623,11 +623,11 @@ public class Mp4DashReader {
private Moov parseMoov(final Box ref) throws IOException {
Box b = readBox(ATOM_MVHD);
Moov moov = new Moov();
final Moov moov = new Moov();
moov.mvhd = parseMvhd();
ensure(b);
ArrayList<Trak> tmp = new ArrayList<>((int) moov.mvhd.nextTrackId);
final ArrayList<Trak> tmp = new ArrayList<>((int) moov.mvhd.nextTrackId);
while ((b = untilBox(ref, ATOM_TRAK, ATOM_MVEX)) != null) {
switch (b.type) {
@ -648,7 +648,7 @@ public class Mp4DashReader {
}
private Trex[] parseMvex(final Box ref, final int possibleTrackCount) throws IOException {
ArrayList<Trex> tmp = new ArrayList<>(possibleTrackCount);
final ArrayList<Trex> tmp = new ArrayList<>(possibleTrackCount);
Box b;
while ((b = untilBox(ref, ATOM_TREX)) != null) {
@ -664,7 +664,7 @@ public class Mp4DashReader {
// flags
stream.skipBytes(4);
Trex obj = new Trex();
final Trex obj = new Trex();
obj.trackId = stream.readInt();
obj.defaultSampleDescriptionIndex = stream.readInt();
obj.defaultSampleDuration = stream.readInt();
@ -675,17 +675,17 @@ public class Mp4DashReader {
}
private Elst parseEdts(final Box ref) throws IOException {
Box b = untilBox(ref, ATOM_ELST);
final Box b = untilBox(ref, ATOM_ELST);
if (b == null) {
return null;
}
Elst obj = new Elst();
final Elst obj = new Elst();
boolean v1 = stream.read() == 1;
final boolean v1 = stream.read() == 1;
stream.skipBytes(3); // flags
int entryCount = stream.readInt();
final int entryCount = stream.readInt();
if (entryCount < 1) {
obj.bMediaRate = 0x00010000; // default media rate (1.0)
return obj;
@ -707,7 +707,7 @@ public class Mp4DashReader {
}
private Minf parseMinf(final Box ref) throws IOException {
Minf obj = new Minf();
final Minf obj = new Minf();
Box b;
while ((b = untilAnyBox(ref)) != null) {
@ -738,7 +738,7 @@ public class Mp4DashReader {
* @return stsd box inside
*/
private byte[] parseStbl(final Box ref) throws IOException {
Box b = untilBox(ref, ATOM_STSD);
final Box b = untilBox(ref, ATOM_STSD);
if (b == null) {
return new byte[0]; // this never should happens (missing codec startup data)
@ -796,8 +796,8 @@ public class Mp4DashReader {
int entriesRowSize;
public TrunEntry getEntry(final int i) {
ByteBuffer buffer = ByteBuffer.wrap(bEntries, i * entriesRowSize, entriesRowSize);
TrunEntry entry = new TrunEntry();
final ByteBuffer buffer = ByteBuffer.wrap(bEntries, i * entriesRowSize, entriesRowSize);
final TrunEntry entry = new TrunEntry();
if (hasFlag(bFlags, 0x0100)) {
entry.sampleDuration = buffer.getInt();
@ -819,7 +819,7 @@ public class Mp4DashReader {
}
public TrunEntry getAbsoluteEntry(final int i, final Tfhd header) {
TrunEntry entry = getEntry(i);
final TrunEntry entry = getEntry(i);
if (!hasFlag(bFlags, 0x0100) && hasFlag(header.bFlags, 0x20)) {
entry.sampleFlags = header.defaultSampleFlags;
@ -928,7 +928,7 @@ public class Mp4DashReader {
return null;
}
Mp4DashSample sample = new Mp4DashSample();
final Mp4DashSample sample = new Mp4DashSample();
sample.info = moof.traf.trun.getAbsoluteEntry(i++, moof.traf.tfhd);
sample.data = new byte[sample.info.sampleSize];

View file

@ -51,7 +51,7 @@ public class Mp4FromDashWriter {
private final ArrayList<Integer> compatibleBrands = new ArrayList<>(5);
public Mp4FromDashWriter(final SharpStream... sources) throws IOException {
for (SharpStream src : sources) {
for (final SharpStream src : sources) {
if (!src.canRewind() && !src.canRead()) {
throw new IOException("All sources must be readable and allow rewind");
}
@ -128,7 +128,7 @@ public class Mp4FromDashWriter {
done = true;
parsed = true;
for (SharpStream src : sourceTracks) {
for (final SharpStream src : sourceTracks) {
src.close();
}
@ -157,17 +157,17 @@ public class Mp4FromDashWriter {
outStream = output;
long read = 8; // mdat box header size
long totalSampleSize = 0;
int[] sampleExtra = new int[readers.length];
int[] defaultMediaTime = new int[readers.length];
int[] defaultSampleDuration = new int[readers.length];
int[] sampleCount = new int[readers.length];
final int[] sampleExtra = new int[readers.length];
final int[] defaultMediaTime = new int[readers.length];
final int[] defaultSampleDuration = new int[readers.length];
final int[] sampleCount = new int[readers.length];
TablesInfo[] tablesInfo = new TablesInfo[tracks.length];
final TablesInfo[] tablesInfo = new TablesInfo[tracks.length];
for (int i = 0; i < tablesInfo.length; i++) {
tablesInfo[i] = new TablesInfo();
}
int singleSampleBuffer;
final int singleSampleBuffer;
if (tracks.length == 1 && tracks[0].kind == TrackKind.Audio) {
// near 1 second of audio data per chunk, avoid split the audio stream in large chunks
singleSampleBuffer = tracks[0].trak.mdia.mdhdTimeScale / 1000;
@ -250,10 +250,10 @@ public class Mp4FromDashWriter {
}
boolean is64 = read > THRESHOLD_FOR_CO64;
final boolean is64 = read > THRESHOLD_FOR_CO64;
// calculate the moov size
int auxSize = makeMoov(defaultMediaTime, tablesInfo, is64);
final int auxSize = makeMoov(defaultMediaTime, tablesInfo, is64);
if (auxSize < THRESHOLD_MOOV_LENGTH) {
auxBuffer = ByteBuffer.allocate(auxSize); // cache moov in the memory
@ -267,9 +267,9 @@ public class Mp4FromDashWriter {
// reserve moov space in the output stream
if (auxSize > 0) {
int length = auxSize;
byte[] buffer = new byte[64 * 1024]; // 64 KiB
final byte[] buffer = new byte[64 * 1024]; // 64 KiB
while (length > 0) {
int count = Math.min(length, buffer.length);
final int count = Math.min(length, buffer.length);
outWrite(buffer, count);
length -= count;
}
@ -305,9 +305,10 @@ public class Mp4FromDashWriter {
outWrite(makeMdat(totalSampleSize, is64));
int[] sampleIndex = new int[readers.length];
int[] sizes = new int[singleSampleBuffer > 0 ? singleSampleBuffer : SAMPLES_PER_CHUNK];
int[] sync = new int[singleSampleBuffer > 0 ? singleSampleBuffer : SAMPLES_PER_CHUNK];
final int[] sampleIndex = new int[readers.length];
final int[] sizes
= new int[singleSampleBuffer > 0 ? singleSampleBuffer : SAMPLES_PER_CHUNK];
final int[] sync = new int[singleSampleBuffer > 0 ? singleSampleBuffer : SAMPLES_PER_CHUNK];
int written = readers.length;
while (written > 0) {
@ -318,9 +319,9 @@ public class Mp4FromDashWriter {
continue; // track is done
}
long chunkOffset = writeOffset;
final long chunkOffset = writeOffset;
int syncCount = 0;
int limit;
final int limit;
if (singleSampleBuffer > 0) {
limit = singleSampleBuffer;
} else {
@ -329,7 +330,7 @@ public class Mp4FromDashWriter {
int j = 0;
for (; j < limit; j++) {
Mp4DashSample sample = getNextSample(i);
final Mp4DashSample sample = getNextSample(i);
if (sample == null) {
if (tablesInfo[i].ctts > 0 && sampleExtra[i] >= 0) {
@ -409,7 +410,7 @@ public class Mp4FromDashWriter {
}
}
Mp4DashSample sample = readersChunks[track].getNextSample();
final Mp4DashSample sample = readersChunks[track].getNextSample();
if (sample == null) {
readersChunks[track] = null;
return getNextSample(track);
@ -434,8 +435,8 @@ public class Mp4FromDashWriter {
auxSeek(offset);
int size = count * 4;
ByteBuffer buffer = ByteBuffer.allocate(size);
final int size = count * 4;
final ByteBuffer buffer = ByteBuffer.allocate(size);
for (int i = 0; i < count; i++) {
buffer.putInt(values[i]);
@ -466,10 +467,10 @@ public class Mp4FromDashWriter {
private void initChunkTables(final TablesInfo tables, final int firstCount,
final int successiveCount) {
// tables.stsz holds amount of samples of the track (total)
int totalSamples = (tables.stsz - firstCount);
float chunkAmount = totalSamples / (float) successiveCount;
int remainChunkOffset = (int) Math.ceil(chunkAmount);
boolean remain = remainChunkOffset != (int) chunkAmount;
final int totalSamples = (tables.stsz - firstCount);
final float chunkAmount = totalSamples / (float) successiveCount;
final int remainChunkOffset = (int) Math.ceil(chunkAmount);
final boolean remain = remainChunkOffset != (int) chunkAmount;
int index = 0;
tables.stsc = 1;
@ -529,7 +530,7 @@ public class Mp4FromDashWriter {
}
private int lengthFor(final int offset) throws IOException {
int size = auxOffset() - offset;
final int size = auxOffset() - offset;
if (moovSimulation) {
return size;
@ -545,7 +546,7 @@ public class Mp4FromDashWriter {
private int make(final int type, final int extra, final int columns, final int rows)
throws IOException {
final byte base = 16;
int size = columns * rows * 4;
final int size = columns * rows * 4;
int total = size + base;
int offset = auxOffset();
@ -618,7 +619,7 @@ public class Mp4FromDashWriter {
size += 4;
}
ByteBuffer buffer = ByteBuffer.allocate(size);
final ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.putInt(size);
buffer.putInt(0x66747970); // "ftyp"
@ -631,7 +632,7 @@ public class Mp4FromDashWriter {
buffer.putInt(0x6D703432); // "mp42" compatible brand
}
for (Integer brand : compatibleBrands) {
for (final Integer brand : compatibleBrands) {
buffer.putInt(brand); // compatible brand
}
@ -648,7 +649,7 @@ public class Mp4FromDashWriter {
size += 8;
}
ByteBuffer buffer = ByteBuffer.allocate(is64 ? 16 : 8)
final ByteBuffer buffer = ByteBuffer.allocate(is64 ? 16 : 8)
.putInt(is64 ? 0x01 : (int) size)
.putInt(0x6D646174); // mdat
@ -689,14 +690,14 @@ public class Mp4FromDashWriter {
private int makeMoov(final int[] defaultMediaTime, final TablesInfo[] tablesInfo,
final boolean is64) throws RuntimeException, IOException {
int start = auxOffset();
final int start = auxOffset();
auxWrite(new byte[]{
0x00, 0x00, 0x00, 0x00, 0x6D, 0x6F, 0x6F, 0x76
});
long longestTrack = 0;
long[] durations = new long[tracks.length];
final long[] durations = new long[tracks.length];
for (int i = 0; i < durations.length; i++) {
durations[i] = (long) Math.ceil(
@ -723,7 +724,7 @@ public class Mp4FromDashWriter {
private void makeTrak(final int index, final long duration, final int defaultMediaTime,
final TablesInfo tables, final boolean is64) throws IOException {
int start = auxOffset();
final int start = auxOffset();
auxWrite(new byte[]{
// trak header
@ -732,7 +733,7 @@ public class Mp4FromDashWriter {
0x00, 0x00, 0x00, 0x68, 0x74, 0x6B, 0x68, 0x64, 0x01, 0x00, 0x00, 0x03
});
ByteBuffer buffer = ByteBuffer.allocate(48);
final ByteBuffer buffer = ByteBuffer.allocate(48);
buffer.putLong(time);
buffer.putLong(time);
buffer.putInt(index + 1);
@ -757,8 +758,8 @@ public class Mp4FromDashWriter {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 // elst header
});
int bMediaRate;
int mediaTime;
final int bMediaRate;
final int mediaTime;
if (tracks[index].trak.edstElst == null) {
// is a audio track ¿is edst/elst optional for audio tracks?
@ -784,17 +785,17 @@ public class Mp4FromDashWriter {
private void makeMdia(final Mdia mdia, final TablesInfo tablesInfo, final boolean is64,
final boolean isAudio) throws IOException {
int startMdia = auxOffset();
final int startMdia = auxOffset();
auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x69, 0x61}); // mdia
auxWrite(mdia.mdhd);
auxWrite(makeHdlr(mdia.hdlr));
int startMinf = auxOffset();
final int startMinf = auxOffset();
auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x6D, 0x69, 0x6E, 0x66}); // minf
auxWrite(mdia.minf.mhd);
auxWrite(mdia.minf.dinf);
int startStbl = auxOffset();
final int startStbl = auxOffset();
auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x73, 0x74, 0x62, 0x6C}); // stbl
auxWrite(mdia.minf.stblStsd);
@ -838,7 +839,7 @@ public class Mp4FromDashWriter {
}
private byte[] makeHdlr(final Hdlr hdlr) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[]{
final ByteBuffer buffer = ByteBuffer.wrap(new byte[]{
0x00, 0x00, 0x00, 0x21, 0x68, 0x64, 0x6C, 0x72, // hdlr
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -854,7 +855,7 @@ public class Mp4FromDashWriter {
}
private int makeSbgp() throws IOException {
int offset = auxOffset();
final int offset = auxOffset();
auxWrite(new byte[] {
0x00, 0x00, 0x00, 0x1C, // box size
@ -883,7 +884,7 @@ public class Mp4FromDashWriter {
* most of m4a encoders and ffmpeg uses this box with dummy values (same values)
*/
ByteBuffer buffer = ByteBuffer.wrap(new byte[] {
final ByteBuffer buffer = ByteBuffer.wrap(new byte[] {
0x00, 0x00, 0x00, 0x1A, // box size
0x73, 0x67, 0x70, 0x64, // "sgpd"
0x01, 0x00, 0x00, 0x00, // box flags (unknown flag sets)

View file

@ -145,10 +145,10 @@ public class OggFromWebMWriter implements Closeable {
}
public void build() throws IOException {
float resolution;
final float resolution;
SimpleBlock bloq;
ByteBuffer header = ByteBuffer.allocate(27 + (255 * 255));
ByteBuffer page = ByteBuffer.allocate(64 * 1024);
final ByteBuffer header = ByteBuffer.allocate(27 + (255 * 255));
final ByteBuffer page = ByteBuffer.allocate(64 * 1024);
header.order(ByteOrder.LITTLE_ENDIAN);
@ -181,7 +181,7 @@ public class OggFromWebMWriter implements Closeable {
}
/* step 3: create packet with metadata */
byte[] buffer = makeMetadata();
final byte[] buffer = makeMetadata();
if (buffer != null) {
addPacketSegment(buffer.length);
makePacketheader(0x00, header, buffer);
@ -194,7 +194,7 @@ public class OggFromWebMWriter implements Closeable {
bloq = getNextBlock();
if (bloq != null && addPacketSegment(bloq)) {
int pos = page.position();
final int pos = page.position();
//noinspection ResultOfMethodCallIgnored
bloq.data.read(page.array(), pos, bloq.dataSize);
page.position(pos + bloq.dataSize);
@ -334,10 +334,10 @@ public class OggFromWebMWriter implements Closeable {
private float getSampleFrequencyFromTrack(final byte[] bMetadata) {
// hardcoded way
ByteBuffer buffer = ByteBuffer.wrap(bMetadata);
final ByteBuffer buffer = ByteBuffer.wrap(bMetadata);
while (buffer.remaining() >= 6) {
int id = buffer.getShort() & 0xFFFF;
final int id = buffer.getShort() & 0xFFFF;
if (id == 0x0000B584) {
return buffer.getFloat();
}
@ -353,7 +353,7 @@ public class OggFromWebMWriter implements Closeable {
}
private boolean addPacketSegment(final SimpleBlock block) {
long timestamp = block.absoluteTimeCodeNs + webmTrack.codecDelay;
final long timestamp = block.absoluteTimeCodeNs + webmTrack.codecDelay;
if (timestamp >= segmentTableNextTimestamp) {
return false;
@ -368,7 +368,7 @@ public class OggFromWebMWriter implements Closeable {
}
int available = (segmentTable.length - segmentTableSize) * 255;
boolean extra = (size % 255) == 0;
final boolean extra = (size % 255) == 0;
if (extra) {
// add a zero byte entry in the table
@ -396,7 +396,7 @@ public class OggFromWebMWriter implements Closeable {
for (int i = 0; i < 0x100; i++) {
int crc = i << 24;
for (int j = 0; j < 8; j++) {
long b = crc >>> 31;
final long b = crc >>> 31;
crc <<= 1;
crc ^= (int) (0x100000000L - b) & 0x04c11db7;
}
@ -407,7 +407,7 @@ public class OggFromWebMWriter implements Closeable {
private int calcCrc32(final int initialCrc, final byte[] buffer, final int size) {
int crc = initialCrc;
for (int i = 0; i < size; i++) {
int reg = (crc >>> 24) & 0xff;
final int reg = (crc >>> 24) & 0xff;
crc = (crc << 8) ^ crc32Table[reg ^ (buffer[i] & 0xff)];
}

View file

@ -65,23 +65,23 @@ public class SrtFromTtmlWriter {
*/
// parse XML
byte[] buffer = new byte[(int) ttml.available()];
final byte[] buffer = new byte[(int) ttml.available()];
ttml.read(buffer);
Document doc = Jsoup.parse(new ByteArrayInputStream(buffer), "UTF-8", "",
final Document doc = Jsoup.parse(new ByteArrayInputStream(buffer), "UTF-8", "",
Parser.xmlParser());
StringBuilder text = new StringBuilder(128);
Elements paragraphList = doc.select("body > div > p");
final StringBuilder text = new StringBuilder(128);
final Elements paragraphList = doc.select("body > div > p");
// check if has frames
if (paragraphList.size() < 1) {
return;
}
for (Element paragraph : paragraphList) {
for (final Element paragraph : paragraphList) {
text.setLength(0);
for (Node children : paragraph.childNodes()) {
for (final Node children : paragraph.childNodes()) {
if (children instanceof TextNode) {
text.append(((TextNode) children).text());
} else if (children instanceof Element
@ -94,8 +94,8 @@ public class SrtFromTtmlWriter {
continue;
}
String begin = getTimestamp(paragraph, "begin");
String end = getTimestamp(paragraph, "end");
final String begin = getTimestamp(paragraph, "begin");
final String end = getTimestamp(paragraph, "end");
writeFrame(begin, end, text);
}

View file

@ -99,7 +99,7 @@ public class WebMReader {
ensure(segment.ref);
// WARNING: track cannot be the same or have different index in new segments
Element elem = untilElement(null, ID_SEGMENT);
final Element elem = untilElement(null, ID_SEGMENT);
if (elem == null) {
done = true;
return null;
@ -113,7 +113,7 @@ public class WebMReader {
int length = (int) parent.contentSize;
long value = 0;
while (length-- > 0) {
int read = stream.read();
final int read = stream.read();
if (read == -1) {
throw new EOFException();
}
@ -127,9 +127,9 @@ public class WebMReader {
}
private byte[] readBlob(final Element parent) throws IOException {
long length = parent.contentSize;
byte[] buffer = new byte[(int) length];
int read = stream.read(buffer);
final long length = parent.contentSize;
final byte[] buffer = new byte[(int) length];
final int read = stream.read(buffer);
if (read < length) {
throw new EOFException();
}
@ -168,7 +168,7 @@ public class WebMReader {
}
private Element readElement() throws IOException {
Element elem = new Element();
final Element elem = new Element();
elem.offset = stream.position();
elem.type = (int) readEncodedNumber();
elem.contentSize = readEncodedNumber();
@ -178,7 +178,7 @@ public class WebMReader {
}
private Element readElement(final int expected) throws IOException {
Element elem = readElement();
final Element elem = readElement();
if (expected != 0 && elem.type != expected) {
throw new NoSuchElementException("expected " + elementID(expected)
+ " found " + elementID(elem.type));
@ -194,7 +194,7 @@ public class WebMReader {
if (expected.length < 1) {
return elem;
}
for (int type : expected) {
for (final int type : expected) {
if (elem.type == type) {
return elem;
}
@ -211,7 +211,7 @@ public class WebMReader {
}
private void ensure(final Element ref) throws IOException {
long skip = (ref.offset + ref.size) - stream.position();
final long skip = (ref.offset + ref.size) - stream.position();
if (skip == 0) {
return;
@ -249,7 +249,7 @@ public class WebMReader {
private Info readInfo(final Element ref) throws IOException {
Element elem;
Info info = new Info();
final Info info = new Info();
while ((elem = untilElement(ref, ID_TIMECODE_SCALE, ID_DURATION)) != null) {
switch (elem.type) {
@ -272,7 +272,7 @@ public class WebMReader {
private Segment readSegment(final Element ref, final int trackLacingExpected,
final boolean metadataExpected) throws IOException {
Segment obj = new Segment(ref);
final Segment obj = new Segment(ref);
Element elem;
while ((elem = untilElement(ref, ID_INFO, ID_TRACKS, ID_CLUSTER)) != null) {
if (elem.type == ID_CLUSTER) {
@ -300,11 +300,11 @@ public class WebMReader {
}
private WebMTrack[] readTracks(final Element ref, final int lacingExpected) throws IOException {
ArrayList<WebMTrack> trackEntries = new ArrayList<>(2);
final ArrayList<WebMTrack> trackEntries = new ArrayList<>(2);
Element elemTrackEntry;
while ((elemTrackEntry = untilElement(ref, ID_TRACK_ENTRY)) != null) {
WebMTrack entry = new WebMTrack();
final WebMTrack entry = new WebMTrack();
boolean drop = false;
Element elem;
while ((elem = untilElement(elemTrackEntry)) != null) {
@ -348,10 +348,10 @@ public class WebMReader {
ensure(elemTrackEntry);
}
WebMTrack[] entries = new WebMTrack[trackEntries.size()];
final WebMTrack[] entries = new WebMTrack[trackEntries.size()];
trackEntries.toArray(entries);
for (WebMTrack entry : entries) {
for (final WebMTrack entry : entries) {
switch (entry.trackType) {
case 1:
entry.kind = TrackKind.Video;
@ -369,7 +369,7 @@ public class WebMReader {
}
private SimpleBlock readSimpleBlock(final Element ref) throws IOException {
SimpleBlock obj = new SimpleBlock(ref);
final SimpleBlock obj = new SimpleBlock(ref);
obj.trackNumber = readEncodedNumber();
obj.relativeTimeCode = stream.readShort();
obj.flags = (byte) stream.read();
@ -385,9 +385,9 @@ public class WebMReader {
}
private Cluster readCluster(final Element ref) throws IOException {
Cluster obj = new Cluster(ref);
final Cluster obj = new Cluster(ref);
Element elem = untilElement(ref, ID_TIMECODE);
final Element elem = untilElement(ref, ID_TIMECODE);
if (elem == null) {
throw new NoSuchElementException("Cluster at " + String.valueOf(ref.offset)
+ " without Timecode element");
@ -443,7 +443,7 @@ public class WebMReader {
}
ensure(segment.currentCluster);
Element elem = untilElement(segment.ref, ID_CLUSTER);
final Element elem = untilElement(segment.ref, ID_CLUSTER);
if (elem == null) {
return null;
}

View file

@ -107,7 +107,7 @@ public class WebMWriter implements Closeable {
done = true;
parsed = true;
for (SharpStream src : sourceTracks) {
for (final SharpStream src : sourceTracks) {
src.close();
}
@ -128,12 +128,12 @@ public class WebMWriter implements Closeable {
makeEBML(out);
long offsetSegmentSizeSet = written + 5;
long offsetInfoDurationSet = written + 94;
long offsetClusterSet = written + 58;
long offsetCuesSet = written + 75;
final long offsetSegmentSizeSet = written + 5;
final long offsetInfoDurationSet = written + 94;
final long offsetClusterSet = written + 58;
final long offsetCuesSet = written + 75;
ArrayList<byte[]> listBuffer = new ArrayList<>(4);
final ArrayList<byte[]> listBuffer = new ArrayList<>(4);
/* segment */
listBuffer.add(new byte[]{
@ -141,7 +141,7 @@ public class WebMWriter implements Closeable {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00// segment content size
});
long segmentOffset = written + listBuffer.get(0).length;
final long segmentOffset = written + listBuffer.get(0).length;
/* seek head */
listBuffer.add(new byte[]{
@ -177,11 +177,11 @@ public class WebMWriter implements Closeable {
dump(listBuffer, out);
// reserve space for Cues element
long cueOffset = written;
final long cueOffset = written;
makeEbmlVoid(out, CUE_RESERVE_SIZE, true);
int[] defaultSampleDuration = new int[infoTracks.length];
long[] duration = new long[infoTracks.length];
final int[] defaultSampleDuration = new int[infoTracks.length];
final long[] duration = new long[infoTracks.length];
for (int i = 0; i < infoTracks.length; i++) {
if (infoTracks[i].defaultDuration < 0) {
@ -194,9 +194,9 @@ public class WebMWriter implements Closeable {
}
// Select a track for the cue
int cuesForTrackId = selectTrackForCue();
final int cuesForTrackId = selectTrackForCue();
long nextCueTime = infoTracks[cuesForTrackId].trackType == 1 ? -1 : 0;
ArrayList<KeyFrame> keyFrames = new ArrayList<>(32);
final ArrayList<KeyFrame> keyFrames = new ArrayList<>(32);
int firstClusterOffset = (int) written;
long currentClusterOffset = makeCluster(out, 0, 0, true);
@ -213,7 +213,7 @@ public class WebMWriter implements Closeable {
blockWritten = 0;
int i = 0;
while (i < readers.length) {
Block bloq = getNextBlockFrom(i);
final Block bloq = getNextBlockFrom(i);
if (bloq == null) {
i++;
continue;
@ -272,7 +272,7 @@ public class WebMWriter implements Closeable {
makeCluster(out, -1, currentClusterOffset, false);
long segmentSize = written - offsetSegmentSizeSet - 7;
final long segmentSize = written - offsetSegmentSizeSet - 7;
/* Segment size */
seekTo(out, offsetSegmentSizeSet);
@ -303,8 +303,8 @@ public class WebMWriter implements Closeable {
short cueSize = 0;
dump(new byte[]{0x1c, 0x53, (byte) 0xbb, 0x6b, 0x20, 0x00, 0x00}, out); // header size is 7
for (KeyFrame keyFrame : keyFrames) {
int size = makeCuePoint(cuesForTrackId, keyFrame, outBuffer);
for (final KeyFrame keyFrame : keyFrames) {
final int size = makeCuePoint(cuesForTrackId, keyFrame, outBuffer);
if ((cueSize + size + 7 + MINIMUM_EBML_VOID_SIZE) > CUE_RESERVE_SIZE) {
break; // no space left
@ -323,7 +323,7 @@ public class WebMWriter implements Closeable {
/* seek head, seek for cues element */
writeInt(out, offsetCuesSet, (int) (cueOffset - segmentOffset));
for (ClusterInfo cluster : clustersOffsetsSizes) {
for (final ClusterInfo cluster : clustersOffsetsSizes) {
writeInt(out, cluster.offset, cluster.size | 0x10000000);
}
}
@ -344,13 +344,13 @@ public class WebMWriter implements Closeable {
}
}
SimpleBlock res = readersCluster[internalTrackId].getNextSimpleBlock();
final SimpleBlock res = readersCluster[internalTrackId].getNextSimpleBlock();
if (res == null) {
readersCluster[internalTrackId] = null;
return new Block(); // fake block to indicate the end of the cluster
}
Block bloq = new Block();
final Block bloq = new Block();
bloq.data = res.data;
bloq.dataSize = res.dataSize;
bloq.trackNumber = internalTrackId;
@ -384,13 +384,13 @@ public class WebMWriter implements Closeable {
private void writeBlock(final SharpStream stream, final Block bloq, final long clusterTimecode)
throws IOException {
long relativeTimeCode = bloq.absoluteTimecode - clusterTimecode;
final long relativeTimeCode = bloq.absoluteTimecode - clusterTimecode;
if (relativeTimeCode < Short.MIN_VALUE || relativeTimeCode > Short.MAX_VALUE) {
throw new IndexOutOfBoundsException("SimpleBlock timecode overflow.");
}
ArrayList<byte[]> listBuffer = new ArrayList<>(5);
final ArrayList<byte[]> listBuffer = new ArrayList<>(5);
listBuffer.add(new byte[]{(byte) 0xa3});
listBuffer.add(null); // block size
listBuffer.add(encode(bloq.trackNumber + 1, false));
@ -458,7 +458,7 @@ public class WebMWriter implements Closeable {
}
private ArrayList<byte[]> makeTracks() {
ArrayList<byte[]> buffer = new ArrayList<>(1);
final ArrayList<byte[]> buffer = new ArrayList<>(1);
buffer.add(new byte[]{0x16, 0x54, (byte) 0xae, 0x6b});
buffer.add(null);
@ -470,8 +470,8 @@ public class WebMWriter implements Closeable {
}
private ArrayList<byte[]> makeTrackEntry(final int internalTrackId, final WebMTrack track) {
byte[] id = encode(internalTrackId + 1, true);
ArrayList<byte[]> buffer = new ArrayList<>(12);
final byte[] id = encode(internalTrackId + 1, true);
final ArrayList<byte[]> buffer = new ArrayList<>(12);
/* track */
buffer.add(new byte[]{(byte) 0xae});
@ -536,7 +536,7 @@ public class WebMWriter implements Closeable {
private int makeCuePoint(final int internalTrackId, final KeyFrame keyFrame,
final byte[] buffer) {
ArrayList<byte[]> cue = new ArrayList<>(5);
final ArrayList<byte[]> cue = new ArrayList<>(5);
/* CuePoint */
cue.add(new byte[]{(byte) 0xbb});
@ -552,7 +552,7 @@ public class WebMWriter implements Closeable {
int size = 0;
lengthFor(cue);
for (byte[] buff : cue) {
for (final byte[] buff : cue) {
System.arraycopy(buff, 0, buffer, size, buff.length);
size += buff.length;
}
@ -562,7 +562,7 @@ public class WebMWriter implements Closeable {
private ArrayList<byte[]> makeCueTrackPosition(final int internalTrackId,
final KeyFrame keyFrame) {
ArrayList<byte[]> buffer = new ArrayList<>(8);
final ArrayList<byte[]> buffer = new ArrayList<>(8);
/* CueTrackPositions */
buffer.add(new byte[]{(byte) 0xb7});
@ -598,7 +598,7 @@ public class WebMWriter implements Closeable {
if (wipe) {
size -= 4;
while (size > 0) {
int write = Math.min(size, outBuffer.length);
final int write = Math.min(size, outBuffer.length);
dump(outBuffer, write, out);
size -= write;
}
@ -617,7 +617,7 @@ public class WebMWriter implements Closeable {
private void dump(final ArrayList<byte[]> buffers, final SharpStream stream)
throws IOException {
for (byte[] buffer : buffers) {
for (final byte[] buffer : buffers) {
stream.write(buffer);
written += buffer.length;
}
@ -649,9 +649,9 @@ public class WebMWriter implements Closeable {
length++;
}
int offset = withLength ? 1 : 0;
byte[] buffer = new byte[offset + length];
long marker = (long) Math.floor((length - 1f) / 8f);
final int offset = withLength ? 1 : 0;
final byte[] buffer = new byte[offset + length];
final long marker = (long) Math.floor((length - 1f) / 8f);
int shift = 0;
for (int i = length - 1; i >= 0; i--, shift += 8) {
@ -670,10 +670,9 @@ public class WebMWriter implements Closeable {
}
private ArrayList<byte[]> encode(final String value) {
byte[] str;
str = value.getBytes(StandardCharsets.UTF_8); // or use "utf-8"
final byte[] str = value.getBytes(StandardCharsets.UTF_8); // or use "utf-8"
ArrayList<byte[]> buffer = new ArrayList<>(2);
final ArrayList<byte[]> buffer = new ArrayList<>(2);
buffer.add(encode(str.length, false));
buffer.add(str);
@ -700,7 +699,7 @@ public class WebMWriter implements Closeable {
}
}
int kind;
final int kind;
if (audioTracks == infoTracks.length) {
kind = 2;
} else if (videoTracks == infoTracks.length) {