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:
parent
aa563091d5
commit
3e1066d0a4
|
@ -49,7 +49,6 @@
|
|||
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.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 void read() throws IOException, CorruptObjectException {
|
|||
if (!liveFile.exists())
|
||||
clear();
|
||||
else if (snapshot == null || snapshot.isModified(liveFile)) {
|
||||
try {
|
||||
final FileInputStream inStream = new FileInputStream(liveFile);
|
||||
try {
|
||||
try (SilentFileInputStream inStream = new SilentFileInputStream(
|
||||
liveFile)) {
|
||||
clear();
|
||||
readFrom(inStream);
|
||||
} finally {
|
||||
try {
|
||||
inStream.close();
|
||||
} catch (IOException err2) {
|
||||
// Ignore any close failures.
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
if (liveFile.exists()) {
|
||||
// Panic: the index file exists but we can't read it
|
||||
|
|
|
@ -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.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 (SilentFileInputStream fd = new SilentFileInputStream(
|
||||
idxFile)) {
|
||||
try {
|
||||
return read(fd, packIndex, reverseIndex);
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,6 @@
|
|||
|
||||
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.List;
|
||||
|
||||
import org.eclipse.jgit.internal.JGitText;
|
||||
import org.eclipse.jgit.util.io.SilentFileInputStream;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
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 static final byte[] readSome(final File path, final int limit)
|
|||
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 static final byte[] readSome(final File path, final int limit)
|
|||
*/
|
||||
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 static final byte[] readFully(final File path, final int max)
|
|||
buf = nb;
|
||||
}
|
||||
return buf;
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException ignored) {
|
||||
// ignore any close errors, this was a read only stream
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue