Java 8 fixed the silent flush during close issue by FilterOutputStream (base class of BufferedOutputStream) using try-with-resources to close the stream, getting a behavior matching what JGit's SafeBufferedOutputStream was doing: try { flush(); } finally { out.close(); } With Java 8 as the minimum required version to run JGit it is no longer necessary to override close() or have this class. Deprecate the class, and use the JRE's version of close. Change-Id: Ic0584c140010278dbe4062df2e71be5df9a797b3tags/v4.6.0.201612231935-r
@@ -46,6 +46,7 @@ package org.eclipse.jgit.junit; | |||
import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.fail; | |||
import java.io.BufferedOutputStream; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
@@ -103,7 +104,6 @@ import org.eclipse.jgit.treewalk.TreeWalk; | |||
import org.eclipse.jgit.treewalk.filter.PathFilterGroup; | |||
import org.eclipse.jgit.util.ChangeIdUtil; | |||
import org.eclipse.jgit.util.FileUtils; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* Wrapper to make creating test data easier. | |||
@@ -885,14 +885,14 @@ public class TestRepository<R extends Repository> { | |||
pack = nameFor(odb, name, ".pack"); | |||
try (OutputStream out = | |||
new SafeBufferedOutputStream(new FileOutputStream(pack))) { | |||
new BufferedOutputStream(new FileOutputStream(pack))) { | |||
pw.writePack(m, m, out); | |||
} | |||
pack.setReadOnly(); | |||
idx = nameFor(odb, name, ".idx"); | |||
try (OutputStream out = | |||
new SafeBufferedOutputStream(new FileOutputStream(idx))) { | |||
new BufferedOutputStream(new FileOutputStream(idx))) { | |||
pw.writeIndex(out); | |||
} | |||
idx.setReadOnly(); |
@@ -45,6 +45,7 @@ package org.eclipse.jgit.diff; | |||
import static org.junit.Assert.assertEquals; | |||
import java.io.BufferedOutputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.File; | |||
@@ -65,7 +66,6 @@ import org.eclipse.jgit.treewalk.filter.PathFilter; | |||
import org.eclipse.jgit.util.FileUtils; | |||
import org.eclipse.jgit.util.RawParseUtils; | |||
import org.eclipse.jgit.util.io.DisabledOutputStream; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
@@ -381,7 +381,7 @@ public class DiffFormatterTest extends RepositoryTestCase { | |||
write(new File(folder, "folder.txt"), "folder"); | |||
try (Git git = new Git(db); | |||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||
DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) { | |||
DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) { | |||
git.add().addFilepattern(".").call(); | |||
git.commit().setMessage("Initial commit").call(); | |||
write(new File(folder, "folder.txt"), "folder change"); | |||
@@ -414,7 +414,7 @@ public class DiffFormatterTest extends RepositoryTestCase { | |||
write(new File(folder, "folder.txt"), "folder"); | |||
try (Git git = new Git(db); | |||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||
DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) { | |||
DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) { | |||
git.add().addFilepattern(".").call(); | |||
RevCommit commit = git.commit().setMessage("Initial commit").call(); | |||
write(new File(folder, "folder.txt"), "folder change"); | |||
@@ -446,7 +446,7 @@ public class DiffFormatterTest extends RepositoryTestCase { | |||
write(new File(folder, "folder.txt"), "folder"); | |||
try (Git git = new Git(db); | |||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||
DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));) { | |||
DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os));) { | |||
git.add().addFilepattern(".").call(); | |||
RevCommit commit = git.commit().setMessage("Initial commit").call(); | |||
write(new File(folder, "folder.txt"), "folder change"); | |||
@@ -473,7 +473,7 @@ public class DiffFormatterTest extends RepositoryTestCase { | |||
@Test | |||
public void testDiffNullToNull() throws Exception { | |||
try (ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||
DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os))) { | |||
DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) { | |||
dfmt.setRepository(db); | |||
dfmt.format((AnyObjectId) null, null); | |||
dfmt.flush(); |
@@ -50,6 +50,7 @@ import static org.junit.Assert.assertNotNull; | |||
import static org.junit.Assert.assertTrue; | |||
import static org.junit.Assert.fail; | |||
import java.io.BufferedOutputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
@@ -69,7 +70,6 @@ import org.eclipse.jgit.lib.Repository; | |||
import org.eclipse.jgit.revwalk.RevBlob; | |||
import org.eclipse.jgit.transport.PackedObjectInfo; | |||
import org.eclipse.jgit.util.FileUtils; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
@@ -180,13 +180,10 @@ public class AbbreviationTest extends LocalDiskRepositoryTestCase { | |||
File idxFile = new File(packDir, packName + ".idx"); | |||
File packFile = new File(packDir, packName + ".pack"); | |||
FileUtils.mkdir(packDir, true); | |||
OutputStream dst = new SafeBufferedOutputStream(new FileOutputStream( | |||
idxFile)); | |||
try { | |||
try (OutputStream dst = new BufferedOutputStream( | |||
new FileOutputStream(idxFile))) { | |||
PackIndexWriter writer = new PackIndexWriterV2(dst); | |||
writer.write(objects, new byte[OBJECT_ID_LENGTH]); | |||
} finally { | |||
dst.close(); | |||
} | |||
new FileOutputStream(packFile).close(); | |||
@@ -51,6 +51,7 @@ import static org.junit.Assert.assertNotNull; | |||
import static org.junit.Assert.assertNotSame; | |||
import static org.junit.Assert.fail; | |||
import java.io.BufferedOutputStream; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
@@ -71,7 +72,6 @@ import org.eclipse.jgit.revwalk.RevObject; | |||
import org.eclipse.jgit.revwalk.RevWalk; | |||
import org.eclipse.jgit.storage.file.WindowCacheConfig; | |||
import org.eclipse.jgit.util.FileUtils; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
@@ -235,20 +235,15 @@ public class ConcurrentRepackTest extends RepositoryTestCase { | |||
throws IOException { | |||
final long begin = files[0].getParentFile().lastModified(); | |||
NullProgressMonitor m = NullProgressMonitor.INSTANCE; | |||
OutputStream out; | |||
out = new SafeBufferedOutputStream(new FileOutputStream(files[0])); | |||
try { | |||
try (OutputStream out = new BufferedOutputStream( | |||
new FileOutputStream(files[0]))) { | |||
pw.writePack(m, m, out); | |||
} finally { | |||
out.close(); | |||
} | |||
out = new SafeBufferedOutputStream(new FileOutputStream(files[1])); | |||
try { | |||
try (OutputStream out = new BufferedOutputStream( | |||
new FileOutputStream(files[1]))) { | |||
pw.writeIndex(out); | |||
} finally { | |||
out.close(); | |||
} | |||
touch(begin, files[0].getParentFile()); |
@@ -46,6 +46,7 @@ | |||
package org.eclipse.jgit.dircache; | |||
import java.io.BufferedInputStream; | |||
import java.io.BufferedOutputStream; | |||
import java.io.EOFException; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
@@ -86,7 +87,6 @@ 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.SafeBufferedOutputStream; | |||
/** | |||
* Support for the Git dircache (aka index file). | |||
@@ -636,7 +636,7 @@ public class DirCache { | |||
requireLocked(tmp); | |||
try { | |||
writeTo(liveFile.getParentFile(), | |||
new SafeBufferedOutputStream(tmp.getOutputStream())); | |||
new BufferedOutputStream(tmp.getOutputStream())); | |||
} catch (IOException err) { | |||
tmp.unlock(); | |||
throw err; |
@@ -53,7 +53,6 @@ import java.text.MessageFormat; | |||
import org.eclipse.jgit.internal.JGitText; | |||
import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder.StoredEntry; | |||
import org.eclipse.jgit.lib.Constants; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import com.googlecode.javaewah.EWAHCompressedBitmap; | |||
@@ -74,7 +73,7 @@ public class PackBitmapIndexWriterV1 { | |||
*/ | |||
public PackBitmapIndexWriterV1(final OutputStream dst) { | |||
out = new DigestOutputStream(dst instanceof BufferedOutputStream ? dst | |||
: new SafeBufferedOutputStream(dst), | |||
: new BufferedOutputStream(dst), | |||
Constants.newMessageDigest()); | |||
dataOutput = new SimpleDataOutput(out); | |||
} |
@@ -56,7 +56,6 @@ import org.eclipse.jgit.lib.Constants; | |||
import org.eclipse.jgit.lib.ObjectId; | |||
import org.eclipse.jgit.transport.PackedObjectInfo; | |||
import org.eclipse.jgit.util.NB; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* Creates a table of contents to support random access by {@link PackFile}. | |||
@@ -183,7 +182,7 @@ public abstract class PackIndexWriter { | |||
*/ | |||
protected PackIndexWriter(final OutputStream dst) { | |||
out = new DigestOutputStream(dst instanceof BufferedOutputStream ? dst | |||
: new SafeBufferedOutputStream(dst), | |||
: new BufferedOutputStream(dst), | |||
Constants.newMessageDigest()); | |||
tmp = new byte[4 + Constants.OBJECT_ID_LENGTH]; | |||
} |
@@ -43,6 +43,7 @@ | |||
package org.eclipse.jgit.lib; | |||
import java.io.BufferedOutputStream; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
@@ -53,7 +54,6 @@ import java.util.List; | |||
import org.eclipse.jgit.lib.RebaseTodoLine.Action; | |||
import org.eclipse.jgit.util.IO; | |||
import org.eclipse.jgit.util.RawParseUtils; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* Offers methods to read and write files formatted like the git-rebase-todo | |||
@@ -216,9 +216,8 @@ public class RebaseTodoFile { | |||
*/ | |||
public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps, | |||
boolean append) throws IOException { | |||
OutputStream fw = new SafeBufferedOutputStream(new FileOutputStream( | |||
new File(repo.getDirectory(), path), append)); | |||
try { | |||
try (OutputStream fw = new BufferedOutputStream(new FileOutputStream( | |||
new File(repo.getDirectory(), path), append))) { | |||
StringBuilder sb = new StringBuilder(); | |||
for (RebaseTodoLine step : steps) { | |||
sb.setLength(0); | |||
@@ -234,8 +233,6 @@ public class RebaseTodoFile { | |||
sb.append('\n'); | |||
fw.write(Constants.encode(sb.toString())); | |||
} | |||
} finally { | |||
fw.close(); | |||
} | |||
} | |||
} |
@@ -52,6 +52,7 @@ import java.io.File; | |||
import java.io.FileNotFoundException; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
import java.net.URISyntaxException; | |||
import java.text.MessageFormat; | |||
import java.util.Collection; | |||
@@ -94,7 +95,6 @@ import org.eclipse.jgit.util.FileUtils; | |||
import org.eclipse.jgit.util.IO; | |||
import org.eclipse.jgit.util.RawParseUtils; | |||
import org.eclipse.jgit.util.SystemReader; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
@@ -1799,15 +1799,12 @@ public abstract class Repository implements AutoCloseable { | |||
throws FileNotFoundException, IOException { | |||
File headsFile = new File(getDirectory(), filename); | |||
if (heads != null) { | |||
BufferedOutputStream bos = new SafeBufferedOutputStream( | |||
new FileOutputStream(headsFile)); | |||
try { | |||
try (OutputStream bos = new BufferedOutputStream( | |||
new FileOutputStream(headsFile))) { | |||
for (ObjectId id : heads) { | |||
id.copyTo(bos); | |||
bos.write('\n'); | |||
} | |||
} finally { | |||
bos.close(); | |||
} | |||
} else { | |||
FileUtils.delete(headsFile, FileUtils.SKIP_MISSING); |
@@ -44,6 +44,7 @@ | |||
package org.eclipse.jgit.transport; | |||
import java.io.BufferedInputStream; | |||
import java.io.BufferedOutputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.OutputStream; | |||
@@ -52,7 +53,6 @@ import java.net.Socket; | |||
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; | |||
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** Active network client of {@link Daemon}. */ | |||
public class DaemonClient { | |||
@@ -95,7 +95,7 @@ public class DaemonClient { | |||
void execute(final Socket sock) throws IOException, | |||
ServiceNotEnabledException, ServiceNotAuthorizedException { | |||
rawIn = new BufferedInputStream(sock.getInputStream()); | |||
rawOut = new SafeBufferedOutputStream(sock.getOutputStream()); | |||
rawOut = new BufferedOutputStream(sock.getOutputStream()); | |||
if (0 < daemon.getTimeout()) | |||
sock.setSoTimeout(daemon.getTimeout() * 1000); |
@@ -46,6 +46,7 @@ | |||
package org.eclipse.jgit.transport; | |||
import java.io.BufferedInputStream; | |||
import java.io.BufferedOutputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.OutputStream; | |||
@@ -62,7 +63,6 @@ import org.eclipse.jgit.errors.NotSupportedException; | |||
import org.eclipse.jgit.errors.TransportException; | |||
import org.eclipse.jgit.internal.JGitText; | |||
import org.eclipse.jgit.lib.Repository; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* Transport through a git-daemon waiting for anonymous TCP connections. | |||
@@ -182,7 +182,7 @@ class TransportGitAnon extends TcpTransport implements PackTransport { | |||
OutputStream sOut = sock.getOutputStream(); | |||
sIn = new BufferedInputStream(sIn); | |||
sOut = new SafeBufferedOutputStream(sOut); | |||
sOut = new BufferedOutputStream(sOut); | |||
init(sIn, sOut); | |||
service("git-upload-pack", pckOut); //$NON-NLS-1$ | |||
@@ -221,7 +221,7 @@ class TransportGitAnon extends TcpTransport implements PackTransport { | |||
OutputStream sOut = sock.getOutputStream(); | |||
sIn = new BufferedInputStream(sIn); | |||
sOut = new SafeBufferedOutputStream(sOut); | |||
sOut = new BufferedOutputStream(sOut); | |||
init(sIn, sOut); | |||
service("git-receive-pack", pckOut); //$NON-NLS-1$ |
@@ -48,6 +48,7 @@ | |||
package org.eclipse.jgit.transport; | |||
import java.io.BufferedInputStream; | |||
import java.io.BufferedOutputStream; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
@@ -67,7 +68,6 @@ import org.eclipse.jgit.transport.resolver.ReceivePackFactory; | |||
import org.eclipse.jgit.transport.resolver.UploadPackFactory; | |||
import org.eclipse.jgit.util.FS; | |||
import org.eclipse.jgit.util.io.MessageWriter; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
import org.eclipse.jgit.util.io.StreamCopyThread; | |||
/** | |||
@@ -258,7 +258,7 @@ class TransportLocal extends Transport implements PackTransport { | |||
OutputStream upOut = uploadPack.getOutputStream(); | |||
upIn = new BufferedInputStream(upIn); | |||
upOut = new SafeBufferedOutputStream(upOut); | |||
upOut = new BufferedOutputStream(upOut); | |||
init(upIn, upOut); | |||
readAdvertisedRefs(); | |||
@@ -311,7 +311,7 @@ class TransportLocal extends Transport implements PackTransport { | |||
OutputStream rpOut = receivePack.getOutputStream(); | |||
rpIn = new BufferedInputStream(rpIn); | |||
rpOut = new SafeBufferedOutputStream(rpOut); | |||
rpOut = new BufferedOutputStream(rpOut); | |||
init(rpIn, rpOut); | |||
readAdvertisedRefs(); |
@@ -45,6 +45,7 @@ package org.eclipse.jgit.transport; | |||
import static org.eclipse.jgit.transport.WalkRemoteObjectDatabase.ROOT_DIR; | |||
import java.io.BufferedOutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
import java.util.ArrayList; | |||
@@ -69,7 +70,6 @@ import org.eclipse.jgit.lib.Ref.Storage; | |||
import org.eclipse.jgit.lib.RefWriter; | |||
import org.eclipse.jgit.lib.Repository; | |||
import org.eclipse.jgit.transport.RemoteRefUpdate.Status; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* Generic push support for dumb transport protocols. | |||
@@ -262,21 +262,15 @@ class WalkPushConnection extends BaseConnection implements PushConnection { | |||
// Write the pack file, then the index, as readers look the | |||
// other direction (index, then pack file). | |||
// | |||
final String wt = "Put " + base.substring(0, 12); //$NON-NLS-1$ | |||
OutputStream os = dest.writeFile(pathPack, monitor, wt + "..pack"); //$NON-NLS-1$ | |||
try { | |||
os = new SafeBufferedOutputStream(os); | |||
String wt = "Put " + base.substring(0, 12); //$NON-NLS-1$ | |||
try (OutputStream os = new BufferedOutputStream( | |||
dest.writeFile(pathPack, monitor, wt + "..pack"))) { //$NON-NLS-1$ | |||
writer.writePack(monitor, monitor, os); | |||
} finally { | |||
os.close(); | |||
} | |||
os = dest.writeFile(pathIdx, monitor, wt + "..idx"); //$NON-NLS-1$ | |||
try { | |||
os = new SafeBufferedOutputStream(os); | |||
try (OutputStream os = new BufferedOutputStream( | |||
dest.writeFile(pathIdx, monitor, wt + "..idx"))) { //$NON-NLS-1$ | |||
writer.writeIndex(os); | |||
} finally { | |||
os.close(); | |||
} | |||
// Record the pack at the start of the pack info list. This |
@@ -56,7 +56,6 @@ import java.util.ArrayList; | |||
import org.eclipse.jgit.internal.JGitText; | |||
import org.eclipse.jgit.lib.NullProgressMonitor; | |||
import org.eclipse.jgit.lib.ProgressMonitor; | |||
import org.eclipse.jgit.util.io.SafeBufferedOutputStream; | |||
/** | |||
* A fully buffered output stream. | |||
@@ -360,7 +359,7 @@ public abstract class TemporaryBuffer extends OutputStream { | |||
overflow.write(b.buffer, 0, b.count); | |||
blocks = null; | |||
overflow = new SafeBufferedOutputStream(overflow, Block.SZ); | |||
overflow = new BufferedOutputStream(overflow, Block.SZ); | |||
overflow.write(last.buffer, 0, last.count); | |||
} | |||
@@ -43,20 +43,13 @@ | |||
package org.eclipse.jgit.util.io; | |||
import java.io.BufferedOutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
/** | |||
* A BufferedOutputStream that throws an error if the final flush fails on | |||
* close. | |||
* <p> | |||
* Java's BufferedOutputStream swallows errors that occur when the output stream | |||
* tries to write the final bytes to the output during close. This may result in | |||
* corrupted files without notice. | |||
* </p> | |||
* @deprecated use BufferedOutputStream in Java 8 and later. | |||
*/ | |||
@Deprecated | |||
public class SafeBufferedOutputStream extends BufferedOutputStream { | |||
/** | |||
* @see BufferedOutputStream#BufferedOutputStream(OutputStream) | |||
* @param out | |||
@@ -76,13 +69,4 @@ public class SafeBufferedOutputStream extends BufferedOutputStream { | |||
public SafeBufferedOutputStream(OutputStream out, int size) { | |||
super(out, size); | |||
} | |||
@Override | |||
public void close() throws IOException { | |||
try { | |||
flush(); | |||
} finally { | |||
super.close(); | |||
} | |||
} | |||
} |