Add SilentFileInputStream to allow ignoring exceptions raised by close()

There are several cases where a FileInputStream is opened outside of
a try-with-resource because we want to explicitly close it and ignore
any IOException that is raised by the close() method.

Introduce a helper class, SilentFileInputStream, that overrides the
close method and ignores the exceptions. This allows to open the stream
in a try-with-resource block and remove the explicit handling of the
close method.

Change-Id: I8612f948a1a5b3d1031344922ad75ce4492cfc61
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
David Pursehouse 2018-03-13 16:11:40 +09:00
parent aa563091d5
commit 3e1066d0a4
5 changed files with 98 additions and 55 deletions

View File

@ -49,7 +49,6 @@
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -87,6 +86,7 @@
import org.eclipse.jgit.util.MutableInteger; import org.eclipse.jgit.util.MutableInteger;
import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.NB;
import org.eclipse.jgit.util.TemporaryBuffer; import org.eclipse.jgit.util.TemporaryBuffer;
import org.eclipse.jgit.util.io.SilentFileInputStream;
/** /**
* Support for the Git dircache (aka index file). * Support for the Git dircache (aka index file).
@ -429,18 +429,10 @@ public void read() throws IOException, CorruptObjectException {
if (!liveFile.exists()) if (!liveFile.exists())
clear(); clear();
else if (snapshot == null || snapshot.isModified(liveFile)) { else if (snapshot == null || snapshot.isModified(liveFile)) {
try { try (SilentFileInputStream inStream = new SilentFileInputStream(
final FileInputStream inStream = new FileInputStream(liveFile); liveFile)) {
try { clear();
clear(); readFrom(inStream);
readFrom(inStream);
} finally {
try {
inStream.close();
} catch (IOException err2) {
// Ignore any close failures.
}
}
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
if (liveFile.exists()) { if (liveFile.exists()) {
// Panic: the index file exists but we can't read it // Panic: the index file exists but we can't read it

View File

@ -44,7 +44,6 @@
package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.MessageFormat; import java.text.MessageFormat;
@ -53,6 +52,7 @@
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.util.io.SilentFileInputStream;
import com.googlecode.javaewah.EWAHCompressedBitmap; import com.googlecode.javaewah.EWAHCompressedBitmap;
@ -93,19 +93,15 @@ public abstract class PackBitmapIndex {
public static PackBitmapIndex open( public static PackBitmapIndex open(
File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex) File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex)
throws IOException { throws IOException {
final FileInputStream fd = new FileInputStream(idxFile); try (SilentFileInputStream fd = new SilentFileInputStream(
try { idxFile)) {
return read(fd, packIndex, reverseIndex);
} catch (IOException ioe) {
throw new IOException(MessageFormat
.format(JGitText.get().unreadablePackIndex,
idxFile.getAbsolutePath()),
ioe);
} finally {
try { try {
fd.close(); return read(fd, packIndex, reverseIndex);
} catch (IOException err2) { } catch (IOException ioe) {
// ignore throw new IOException(
MessageFormat.format(JGitText.get().unreadablePackIndex,
idxFile.getAbsolutePath()),
ioe);
} }
} }
} }

View File

@ -45,7 +45,6 @@
package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -64,6 +63,7 @@
import org.eclipse.jgit.lib.ObjectIdSet; import org.eclipse.jgit.lib.ObjectIdSet;
import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.NB;
import org.eclipse.jgit.util.io.SilentFileInputStream;
/** /**
* Access path to locate objects by {@link org.eclipse.jgit.lib.ObjectId} in a * Access path to locate objects by {@link org.eclipse.jgit.lib.ObjectId} in a
@ -95,20 +95,14 @@ public abstract class PackIndex
* unrecognized data version, or unexpected data corruption. * unrecognized data version, or unexpected data corruption.
*/ */
public static PackIndex open(final File idxFile) throws IOException { public static PackIndex open(final File idxFile) throws IOException {
final FileInputStream fd = new FileInputStream(idxFile); try (SilentFileInputStream fd = new SilentFileInputStream(
try { idxFile)) {
return read(fd); return read(fd);
} catch (IOException ioe) { } catch (IOException ioe) {
throw new IOException(MessageFormat throw new IOException(
.format(JGitText.get().unreadablePackIndex, MessageFormat.format(JGitText.get().unreadablePackIndex,
idxFile.getAbsolutePath()), idxFile.getAbsolutePath()),
ioe); ioe);
} finally {
try {
fd.close();
} catch (IOException err2) {
// ignore
}
} }
} }

View File

@ -47,7 +47,6 @@
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -59,6 +58,7 @@
import java.util.List; import java.util.List;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.util.io.SilentFileInputStream;
/** /**
* Input/Output utilities * Input/Output utilities
@ -98,8 +98,7 @@ public static final byte[] readFully(final File path)
*/ */
public static final byte[] readSome(final File path, final int limit) public static final byte[] readSome(final File path, final int limit)
throws FileNotFoundException, IOException { throws FileNotFoundException, IOException {
FileInputStream in = new FileInputStream(path); try (SilentFileInputStream in = new SilentFileInputStream(path)) {
try {
byte[] buf = new byte[limit]; byte[] buf = new byte[limit];
int cnt = 0; int cnt = 0;
for (;;) { for (;;) {
@ -113,12 +112,6 @@ public static final byte[] readSome(final File path, final int limit)
byte[] res = new byte[cnt]; byte[] res = new byte[cnt];
System.arraycopy(buf, 0, res, 0, cnt); System.arraycopy(buf, 0, res, 0, cnt);
return res; return res;
} finally {
try {
in.close();
} catch (IOException ignored) {
// do nothing
}
} }
} }
@ -138,8 +131,7 @@ public static final byte[] readSome(final File path, final int limit)
*/ */
public static final byte[] readFully(final File path, final int max) public static final byte[] readFully(final File path, final int max)
throws FileNotFoundException, IOException { throws FileNotFoundException, IOException {
final FileInputStream in = new FileInputStream(path); try (SilentFileInputStream in = new SilentFileInputStream(path)) {
try {
long sz = Math.max(path.length(), 1); long sz = Math.max(path.length(), 1);
if (sz > max) if (sz > max)
throw new IOException(MessageFormat.format( throw new IOException(MessageFormat.format(
@ -173,12 +165,6 @@ public static final byte[] readFully(final File path, final int max)
buf = nb; buf = nb;
} }
return buf; return buf;
} finally {
try {
in.close();
} catch (IOException ignored) {
// ignore any close errors, this was a read only stream
}
} }
} }

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2018, David Pursehouse <david.pursehouse@gmail.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.util.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* An implementation of FileInputStream that ignores any exceptions on close().
*
* @since 5.0
*/
public class SilentFileInputStream extends FileInputStream {
/**
* @param file
* the file
* @throws FileNotFoundException
* the file was not found
*/
public SilentFileInputStream(File file) throws FileNotFoundException {
super(file);
}
@Override
public void close() {
try {
super.close();
} catch (IOException e) {
// Ignore
}
}
}