mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Added ReusableBufferedOutputStream.
ReusableBufferedOutputStream is a subclass of BufferedOutputStream with a reset(OutputStream) that allows an instance to be re-used with another underlying output stream. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=139505999
This commit is contained in:
parent
1a62dad980
commit
35054f8f7c
3 changed files with 129 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.exoplayer2.util;
|
||||||
|
|
||||||
|
import android.test.MoreAsserts;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link ReusableBufferedOutputStream}.
|
||||||
|
*/
|
||||||
|
public class ReusableBufferedOutputStreamTest extends TestCase {
|
||||||
|
|
||||||
|
private static final byte[] TEST_DATA_1 = "test data 1".getBytes();
|
||||||
|
private static final byte[] TEST_DATA_2 = "2 test data".getBytes();
|
||||||
|
|
||||||
|
public void testReset() throws Exception {
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream(1000);
|
||||||
|
ReusableBufferedOutputStream outputStream = new ReusableBufferedOutputStream(
|
||||||
|
byteArrayOutputStream1, 1000);
|
||||||
|
outputStream.write(TEST_DATA_1);
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream(1000);
|
||||||
|
outputStream.reset(byteArrayOutputStream2);
|
||||||
|
outputStream.write(TEST_DATA_2);
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
MoreAsserts.assertEquals(TEST_DATA_1, byteArrayOutputStream1.toByteArray());
|
||||||
|
MoreAsserts.assertEquals(TEST_DATA_2, byteArrayOutputStream2.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.exoplayer2.util;
|
||||||
|
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a subclass of {@link BufferedOutputStream} with a {@link #reset(OutputStream)} method
|
||||||
|
* that allows an instance to be re-used with another underlying output stream.
|
||||||
|
*/
|
||||||
|
public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
||||||
|
|
||||||
|
public ReusableBufferedOutputStream(OutputStream out) {
|
||||||
|
super(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReusableBufferedOutputStream(OutputStream out, int size) {
|
||||||
|
super(out, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
Throwable thrown = null;
|
||||||
|
try {
|
||||||
|
flush();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
thrown = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (thrown == null) {
|
||||||
|
thrown = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out = null;
|
||||||
|
|
||||||
|
if (thrown != null) {
|
||||||
|
Util.sneakyThrow(thrown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets this stream and uses the given output stream for writing. This stream must be closed
|
||||||
|
* before resetting.
|
||||||
|
*
|
||||||
|
* @param out New output stream to be used for writing.
|
||||||
|
* @throws IllegalStateException If the stream isn't closed.
|
||||||
|
*/
|
||||||
|
public void reset(OutputStream out) {
|
||||||
|
Assertions.checkState(this.out == null);
|
||||||
|
this.out = out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -840,6 +840,19 @@ public final class Util {
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hacky method that always throws {@code t} even if {@code t} is a checked exception,
|
||||||
|
* and is not declared to be thrown.
|
||||||
|
*/
|
||||||
|
public static void sneakyThrow(Throwable t) {
|
||||||
|
Util.<RuntimeException>sneakyThrowInternal(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private static <T extends Throwable> void sneakyThrowInternal(Throwable t) throws T {
|
||||||
|
throw (T) t;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the result of updating a CRC with the specified bytes in a "most significant bit first"
|
* Returns the result of updating a CRC with the specified bytes in a "most significant bit first"
|
||||||
* order.
|
* order.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue