From 3e1066d0a4460c63b7d77293959c6e548bef9a16 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 13 Mar 2018 16:11:40 +0900 Subject: [PATCH] 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 --- .../org/eclipse/jgit/dircache/DirCache.java | 18 ++--- .../storage/file/PackBitmapIndex.java | 22 +++--- .../jgit/internal/storage/file/PackIndex.java | 18 ++--- .../src/org/eclipse/jgit/util/IO.java | 20 +---- .../jgit/util/io/SilentFileInputStream.java | 75 +++++++++++++++++++ 5 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/util/io/SilentFileInputStream.java diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java index cc431dbdf..fcee252f8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java @@ -49,7 +49,6 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.EOFException; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -87,6 +86,7 @@ import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.MutableInteger; import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.TemporaryBuffer; +import org.eclipse.jgit.util.io.SilentFileInputStream; /** * Support for the Git dircache (aka index file). @@ -429,18 +429,10 @@ public class DirCache { if (!liveFile.exists()) clear(); else if (snapshot == null || snapshot.isModified(liveFile)) { - try { - final FileInputStream inStream = new FileInputStream(liveFile); - try { - clear(); - readFrom(inStream); - } finally { - try { - inStream.close(); - } catch (IOException err2) { - // Ignore any close failures. - } - } + try (SilentFileInputStream inStream = new SilentFileInputStream( + liveFile)) { + clear(); + readFrom(inStream); } catch (FileNotFoundException fnfe) { if (liveFile.exists()) { // Panic: the index file exists but we can't read it diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndex.java index 21a1c7fcb..6772e2c90 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndex.java @@ -44,7 +44,6 @@ package org.eclipse.jgit.internal.storage.file; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; @@ -53,6 +52,7 @@ import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.util.io.SilentFileInputStream; import com.googlecode.javaewah.EWAHCompressedBitmap; @@ -93,19 +93,15 @@ public abstract class PackBitmapIndex { public static PackBitmapIndex open( File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex) throws IOException { - final FileInputStream fd = new FileInputStream(idxFile); - try { - return read(fd, packIndex, reverseIndex); - } catch (IOException ioe) { - throw new IOException(MessageFormat - .format(JGitText.get().unreadablePackIndex, - idxFile.getAbsolutePath()), - ioe); - } finally { + try (SilentFileInputStream fd = new SilentFileInputStream( + idxFile)) { try { - fd.close(); - } catch (IOException err2) { - // ignore + return read(fd, packIndex, reverseIndex); + } catch (IOException ioe) { + throw new IOException( + MessageFormat.format(JGitText.get().unreadablePackIndex, + idxFile.getAbsolutePath()), + ioe); } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndex.java index acfd9c471..e324c1f52 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndex.java @@ -45,7 +45,6 @@ package org.eclipse.jgit.internal.storage.file; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -64,6 +63,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectIdSet; import org.eclipse.jgit.util.IO; 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 @@ -95,20 +95,14 @@ public abstract class PackIndex * unrecognized data version, or unexpected data corruption. */ public static PackIndex open(final File idxFile) throws IOException { - final FileInputStream fd = new FileInputStream(idxFile); - try { - return read(fd); + try (SilentFileInputStream fd = new SilentFileInputStream( + idxFile)) { + return read(fd); } catch (IOException ioe) { - throw new IOException(MessageFormat - .format(JGitText.get().unreadablePackIndex, + throw new IOException( + MessageFormat.format(JGitText.get().unreadablePackIndex, idxFile.getAbsolutePath()), ioe); - } finally { - try { - fd.close(); - } catch (IOException err2) { - // ignore - } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java index f39d21786..fa19e89f2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java @@ -47,7 +47,6 @@ package org.eclipse.jgit.util; import java.io.EOFException; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -59,6 +58,7 @@ import java.util.ArrayList; import java.util.List; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.util.io.SilentFileInputStream; /** * Input/Output utilities @@ -98,8 +98,7 @@ public class IO { */ public static final byte[] readSome(final File path, final int limit) throws FileNotFoundException, IOException { - FileInputStream in = new FileInputStream(path); - try { + try (SilentFileInputStream in = new SilentFileInputStream(path)) { byte[] buf = new byte[limit]; int cnt = 0; for (;;) { @@ -113,12 +112,6 @@ public class IO { byte[] res = new byte[cnt]; System.arraycopy(buf, 0, res, 0, cnt); return res; - } finally { - try { - in.close(); - } catch (IOException ignored) { - // do nothing - } } } @@ -138,8 +131,7 @@ public class IO { */ public static final byte[] readFully(final File path, final int max) throws FileNotFoundException, IOException { - final FileInputStream in = new FileInputStream(path); - try { + try (SilentFileInputStream in = new SilentFileInputStream(path)) { long sz = Math.max(path.length(), 1); if (sz > max) throw new IOException(MessageFormat.format( @@ -173,12 +165,6 @@ public class IO { buf = nb; } return buf; - } finally { - try { - in.close(); - } catch (IOException ignored) { - // ignore any close errors, this was a read only stream - } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/SilentFileInputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/SilentFileInputStream.java new file mode 100644 index 000000000..e58803bb9 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/SilentFileInputStream.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2018, David Pursehouse + * 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 + } + } +}