Commit 35054f8f by eguven Committed by Oliver Woodman

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
parent 1a62dad9
/*
* 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());
}
}
/*
* 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;
}
}
......@@ -841,6 +841,19 @@ public final class Util {
}
/**
* 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"
* order.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment