Suppress resource warnings with Java 7

For streams that should not be closed, i.e. don't own an underlying
stream, and in-memory streams that do not need to be closed we just
suppress the warning. This mostly apply to test cases. GC is enough.

For streams with external resources (i.e. files) we add the necessary
call to close().

Change-Id: I4d883ba2e7d07f199fe57ccb3459ece00441a570
This commit is contained in:
Robin Rosenberg 2012-10-10 08:02:30 +02:00 committed by Shawn O. Pearce
parent bc8794eb21
commit 81fa566295
13 changed files with 52 additions and 19 deletions

View File

@ -297,6 +297,7 @@ private static boolean isReceivePackSideBand(HttpServletRequest req) {
private static void writeSideBand(OutputStream out, String textForGit) private static void writeSideBand(OutputStream out, String textForGit)
throws IOException { throws IOException {
@SuppressWarnings("resource" /* java 7 */)
OutputStream msg = new SideBandOutputStream(CH_ERROR, SMALL_BUF, out); OutputStream msg = new SideBandOutputStream(CH_ERROR, SMALL_BUF, out);
msg.write(Constants.encode("error: " + textForGit)); msg.write(Constants.encode("error: " + textForGit));
msg.flush(); msg.flush();

View File

@ -119,6 +119,7 @@ public void select(ObjectToPack otp, StoredObjectRepresentation next) {
ptr++; ptr++;
ptr++; ptr++;
@SuppressWarnings("resource" /* java 7 */)
TemporaryBuffer.Heap raw = new TemporaryBuffer.Heap(bufArray.length); TemporaryBuffer.Heap raw = new TemporaryBuffer.Heap(bufArray.length);
InflaterInputStream inf = new InflaterInputStream( InflaterInputStream inf = new InflaterInputStream(
new ByteArrayInputStream(bufArray, ptr, bufArray.length)); new ByteArrayInputStream(bufArray, ptr, bufArray.length));

View File

@ -162,8 +162,9 @@ public void testCheckoutWithNonDeletedFiles() throws Exception {
} catch (IOException e) { } catch (IOException e) {
// the test makes only sense if deletion of // the test makes only sense if deletion of
// a file with open stream fails // a file with open stream fails
} finally {
fis.close();
} }
fis.close();
FileUtils.delete(testFile); FileUtils.delete(testFile);
CheckoutCommand co = git.checkout(); CheckoutCommand co = git.checkout();
// delete Test.txt in branch test // delete Test.txt in branch test

View File

@ -112,6 +112,7 @@ public void unknownRepositoryFormatVersion() throws Exception {
} }
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void absoluteGitDirRef() throws Exception { public void absoluteGitDirRef() throws Exception {
FileRepository repo1 = createWorkRepository(); FileRepository repo1 = createWorkRepository();
@ -129,6 +130,7 @@ public void absoluteGitDirRef() throws Exception {
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void relativeGitDirRef() throws Exception { public void relativeGitDirRef() throws Exception {
FileRepository repo1 = createWorkRepository(); FileRepository repo1 = createWorkRepository();
@ -147,6 +149,7 @@ public void relativeGitDirRef() throws Exception {
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void scanWithGitDirRef() throws Exception { public void scanWithGitDirRef() throws Exception {
FileRepository repo1 = createWorkRepository(); FileRepository repo1 = createWorkRepository();

View File

@ -128,6 +128,7 @@ public void apply(DirCacheEntry ent) {
assertFalse(gen.next()); assertFalse(gen.next());
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void repositoryWithRootLevelSubmoduleAbsoluteRef() public void repositoryWithRootLevelSubmoduleAbsoluteRef()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
@ -176,6 +177,7 @@ public void apply(DirCacheEntry ent) {
assertFalse(gen.next()); assertFalse(gen.next());
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void repositoryWithRootLevelSubmoduleRelativeRef() public void repositoryWithRootLevelSubmoduleRelativeRef()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {

View File

@ -78,8 +78,9 @@ public void setUp() throws Exception {
@Test @Test
public void testWrite_CH_DATA() throws IOException { public void testWrite_CH_DATA() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
SMALL_BUF, rawOut);
out.write(new byte[] { 'a', 'b', 'c' }); out.write(new byte[] { 'a', 'b', 'c' });
out.flush(); out.flush();
assertBuffer("0008\001abc"); assertBuffer("0008\001abc");
@ -87,8 +88,9 @@ public void testWrite_CH_DATA() throws IOException {
@Test @Test
public void testWrite_CH_PROGRESS() throws IOException { public void testWrite_CH_PROGRESS() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_PROGRESS, SMALL_BUF, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_PROGRESS,
SMALL_BUF, rawOut);
out.write(new byte[] { 'a', 'b', 'c' }); out.write(new byte[] { 'a', 'b', 'c' });
out.flush(); out.flush();
assertBuffer("0008\002abc"); assertBuffer("0008\002abc");
@ -96,8 +98,9 @@ public void testWrite_CH_PROGRESS() throws IOException {
@Test @Test
public void testWrite_CH_ERROR() throws IOException { public void testWrite_CH_ERROR() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_ERROR, SMALL_BUF, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_ERROR,
SMALL_BUF, rawOut);
out.write(new byte[] { 'a', 'b', 'c' }); out.write(new byte[] { 'a', 'b', 'c' });
out.flush(); out.flush();
assertBuffer("0008\003abc"); assertBuffer("0008\003abc");
@ -105,8 +108,9 @@ public void testWrite_CH_ERROR() throws IOException {
@Test @Test
public void testWrite_Small() throws IOException { public void testWrite_Small() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
SMALL_BUF, rawOut);
out.write('a'); out.write('a');
out.write('b'); out.write('b');
out.write('c'); out.write('c');
@ -116,8 +120,9 @@ public void testWrite_Small() throws IOException {
@Test @Test
public void testWrite_SmallBlocks1() throws IOException { public void testWrite_SmallBlocks1() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, 6, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
rawOut);
out.write('a'); out.write('a');
out.write('b'); out.write('b');
out.write('c'); out.write('c');
@ -127,8 +132,9 @@ public void testWrite_SmallBlocks1() throws IOException {
@Test @Test
public void testWrite_SmallBlocks2() throws IOException { public void testWrite_SmallBlocks2() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, 6, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
rawOut);
out.write(new byte[] { 'a', 'b', 'c' }); out.write(new byte[] { 'a', 'b', 'c' });
out.flush(); out.flush();
assertBuffer("0006\001a0006\001b0006\001c"); assertBuffer("0006\001a0006\001b0006\001c");
@ -136,8 +142,9 @@ public void testWrite_SmallBlocks2() throws IOException {
@Test @Test
public void testWrite_SmallBlocks3() throws IOException { public void testWrite_SmallBlocks3() throws IOException {
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, 7, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 7,
rawOut);
out.write('a'); out.write('a');
out.write(new byte[] { 'b', 'c' }); out.write(new byte[] { 'b', 'c' });
out.flush(); out.flush();
@ -152,8 +159,9 @@ public void testWrite_Large() throws IOException {
buf[i] = (byte) i; buf[i] = (byte) i;
} }
final SideBandOutputStream out; @SuppressWarnings("resource" /* java 7 */)
out = new SideBandOutputStream(CH_DATA, MAX_BUF, rawOut); final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
MAX_BUF, rawOut);
out.write(buf); out.write(buf);
out.flush(); out.flush();
@ -167,6 +175,7 @@ public void testWrite_Large() throws IOException {
} }
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void testFlush() throws IOException { public void testFlush() throws IOException {
final int[] flushCnt = new int[1]; final int[] flushCnt = new int[1];

View File

@ -353,6 +353,7 @@ public void testInCoreLimit_SwitchOnCopy() throws IOException {
@Test @Test
public void testDestroyWhileOpen() throws IOException { public void testDestroyWhileOpen() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final TemporaryBuffer b = new TemporaryBuffer.LocalFile(); final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
try { try {
b.write(new TestRng(getName()) b.write(new TestRng(getName())
@ -411,6 +412,7 @@ public void testRandomWrites() throws IOException {
@Test @Test
public void testHeap() throws IOException { public void testHeap() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024); final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024);
final byte[] r = new byte[8 * 1024]; final byte[] r = new byte[8 * 1024];
b.write(r); b.write(r);

View File

@ -121,7 +121,9 @@ private void test(byte[] input, byte[] expected, boolean detectBinary) throws IO
} }
assertEquals(expected.length, read); assertEquals(expected.length, read);
cis2.close();
} }
cis1.close();
} }
private static byte[] asBytes(String in) { private static byte[] asBytes(String in) {

View File

@ -69,6 +69,7 @@ public void testEmptyStream() throws IOException {
@Test @Test
public void testReadSingleBytes() throws IOException { public void testReadSingleBytes() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(); final UnionInputStream u = new UnionInputStream();
assertTrue(u.isEmpty()); assertTrue(u.isEmpty());
@ -101,6 +102,7 @@ public void testReadSingleBytes() throws IOException {
@Test @Test
public void testReadByteBlocks() throws IOException { public void testReadByteBlocks() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(); final UnionInputStream u = new UnionInputStream();
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 })); u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 })); u.add(new ByteArrayInputStream(new byte[] { 3 }));
@ -124,6 +126,7 @@ private static byte[] slice(byte[] in, int len) {
@Test @Test
public void testArrayConstructor() throws IOException { public void testArrayConstructor() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream( final UnionInputStream u = new UnionInputStream(
new ByteArrayInputStream(new byte[] { 1, 0, 2 }), new ByteArrayInputStream(new byte[] { 1, 0, 2 }),
new ByteArrayInputStream(new byte[] { 3 }), new ByteArrayInputStream(new byte[] { 3 }),
@ -141,6 +144,7 @@ public void testArrayConstructor() throws IOException {
@Test @Test
public void testMarkSupported() { public void testMarkSupported() {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(); final UnionInputStream u = new UnionInputStream();
assertFalse(u.markSupported()); assertFalse(u.markSupported());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 })); u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
@ -149,6 +153,7 @@ public void testMarkSupported() {
@Test @Test
public void testSkip() throws IOException { public void testSkip() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(); final UnionInputStream u = new UnionInputStream();
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 })); u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 })); u.add(new ByteArrayInputStream(new byte[] { 3 }));
@ -171,6 +176,7 @@ public long skip(long n) {
@Test @Test
public void testAutoCloseDuringRead() throws IOException { public void testAutoCloseDuringRead() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(); final UnionInputStream u = new UnionInputStream();
final boolean closed[] = new boolean[2]; final boolean closed[] = new boolean[2];
u.add(new ByteArrayInputStream(new byte[] { 1 }) { u.add(new ByteArrayInputStream(new byte[] { 1 }) {
@ -248,6 +254,7 @@ public int read() throws IOException {
throw new IOException("Expected"); throw new IOException("Expected");
} }
}; };
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream( final UnionInputStream u = new UnionInputStream(
new ByteArrayInputStream(new byte[]{1,2,3}), new ByteArrayInputStream(new byte[]{1,2,3}),
errorReadStream); errorReadStream);

View File

@ -662,6 +662,7 @@ private PackFile writePack(Set<? extends ObjectId> want,
JGitText.get().cannotCreateIndexfile, tmpIdx.getPath())); JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));
// write the packfile // write the packfile
@SuppressWarnings("resource" /* java 7 */)
FileChannel channel = new FileOutputStream(tmpPack).getChannel(); FileChannel channel = new FileOutputStream(tmpPack).getChannel();
OutputStream channelStream = Channels.newOutputStream(channel); OutputStream channelStream = Channels.newOutputStream(channel);
try { try {
@ -673,6 +674,7 @@ private PackFile writePack(Set<? extends ObjectId> want,
} }
// write the packindex // write the packindex
@SuppressWarnings("resource")
FileChannel idxChannel = new FileOutputStream(tmpIdx).getChannel(); FileChannel idxChannel = new FileOutputStream(tmpIdx).getChannel();
OutputStream idxStream = Channels.newOutputStream(idxChannel); OutputStream idxStream = Channels.newOutputStream(idxChannel);
try { try {

View File

@ -146,6 +146,7 @@ public void release() {
} }
} }
@SuppressWarnings("resource" /* java 7 */)
private File toTemp(final MessageDigest md, final int type, long len, private File toTemp(final MessageDigest md, final int type, long len,
final InputStream is) throws IOException, FileNotFoundException, final InputStream is) throws IOException, FileNotFoundException,
Error { Error {
@ -185,6 +186,7 @@ private File toTemp(final MessageDigest md, final int type, long len,
} }
} }
@SuppressWarnings("resource" /* java 7 */)
private File toTemp(final int type, final byte[] buf, final int pos, private File toTemp(final int type, final byte[] buf, final int pos,
final int len) throws IOException, FileNotFoundException { final int len) throws IOException, FileNotFoundException {
boolean delete = true; boolean delete = true;

View File

@ -1079,7 +1079,7 @@ private void sendPack() throws IOException {
private boolean reportInternalServerErrorOverSideband() { private boolean reportInternalServerErrorOverSideband() {
try { try {
@SuppressWarnings("resource") @SuppressWarnings("resource" /* java 7 */)
SideBandOutputStream err = new SideBandOutputStream( SideBandOutputStream err = new SideBandOutputStream(
SideBandOutputStream.CH_ERROR, SideBandOutputStream.CH_ERROR,
SideBandOutputStream.SMALL_BUF, SideBandOutputStream.SMALL_BUF,
@ -1121,7 +1121,7 @@ private void sendPack(final boolean sideband) throws IOException {
} catch (ServiceMayNotContinueException noPack) { } catch (ServiceMayNotContinueException noPack) {
if (sideband && noPack.getMessage() != null) { if (sideband && noPack.getMessage() != null) {
noPack.setOutput(); noPack.setOutput();
@SuppressWarnings("resource") @SuppressWarnings("resource" /* java 7 */)
SideBandOutputStream err = new SideBandOutputStream( SideBandOutputStream err = new SideBandOutputStream(
SideBandOutputStream.CH_ERROR, SideBandOutputStream.CH_ERROR,
SideBandOutputStream.SMALL_BUF, rawOut); SideBandOutputStream.SMALL_BUF, rawOut);

View File

@ -216,6 +216,7 @@ public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
if (last < 0) if (last < 0)
return ByteBuffer.wrap(out, 0, pos); return ByteBuffer.wrap(out, 0, pos);
@SuppressWarnings("resource" /* java 7 */)
TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(Integer.MAX_VALUE); TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
tmp.write(out); tmp.write(out);
tmp.write(last); tmp.write(last);