diff options
Diffstat (limited to 'guava/src/com/google/common/io/FileBackedOutputStream.java')
-rw-r--r-- | guava/src/com/google/common/io/FileBackedOutputStream.java | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/guava/src/com/google/common/io/FileBackedOutputStream.java b/guava/src/com/google/common/io/FileBackedOutputStream.java index 9912e2fcd..dd297e90a 100644 --- a/guava/src/com/google/common/io/FileBackedOutputStream.java +++ b/guava/src/com/google/common/io/FileBackedOutputStream.java @@ -14,12 +14,15 @@ package com.google.common.io; +import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.annotations.VisibleForTesting; import com.google.errorprone.annotations.concurrent.GuardedBy; +import com.google.j2objc.annotations.J2ObjCIncompatible; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -34,6 +37,14 @@ import javax.annotation.CheckForNull; * An {@link OutputStream} that starts buffering to a byte array, but switches to file buffering * once the data reaches a configurable size. * + * <p>When this stream creates a temporary file, it restricts the file's permissions to the current + * user or, in the case of Android, the current app. If that is not possible (as is the case under + * the very old Android Ice Cream Sandwich release), then this stream throws an exception instead of + * creating a file that would be more accessible. (This behavior is new in Guava 32.0.0. Previous + * versions would create a file that is more accessible, as discussed in <a + * href="https://github.com/google/guava/issues/2575">Guava issue 2575</a>. TODO: b/283778848 - Fill + * in CVE number once it's available.) + * * <p>Temporary files created by this stream may live in the local filesystem until either: * * <ul> @@ -51,13 +62,14 @@ import javax.annotation.CheckForNull; * @since 1.0 */ @Beta +@J2ktIncompatible @GwtIncompatible +@J2ObjCIncompatible @ElementTypesAreNonnullByDefault public final class FileBackedOutputStream extends OutputStream { private final int fileThreshold; private final boolean resetOnFinalize; private final ByteSource source; - @CheckForNull private final File parentDirectory; @GuardedBy("this") private OutputStream out; @@ -93,6 +105,7 @@ public final class FileBackedOutputStream extends OutputStream { * {@link ByteSource} returned by {@link #asByteSource} is finalized. * * @param fileThreshold the number of bytes before the stream should switch to buffering to a file + * @throws IllegalArgumentException if {@code fileThreshold} is negative */ public FileBackedOutputStream(int fileThreshold) { this(fileThreshold, false); @@ -105,16 +118,13 @@ public final class FileBackedOutputStream extends OutputStream { * @param fileThreshold the number of bytes before the stream should switch to buffering to a file * @param resetOnFinalize if true, the {@link #reset} method will be called when the {@link * ByteSource} returned by {@link #asByteSource} is finalized. + * @throws IllegalArgumentException if {@code fileThreshold} is negative */ public FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize) { - this(fileThreshold, resetOnFinalize, null); - } - - private FileBackedOutputStream( - int fileThreshold, boolean resetOnFinalize, @CheckForNull File parentDirectory) { + checkArgument( + fileThreshold >= 0, "fileThreshold must be non-negative, but was %s", fileThreshold); this.fileThreshold = fileThreshold; this.resetOnFinalize = resetOnFinalize; - this.parentDirectory = parentDirectory; memory = new MemoryOutput(); out = memory; @@ -225,7 +235,7 @@ public final class FileBackedOutputStream extends OutputStream { @GuardedBy("this") private void update(int len) throws IOException { if (memory != null && (memory.getCount() + len > fileThreshold)) { - File temp = File.createTempFile("FileBackedOutputStream", null, parentDirectory); + File temp = TempFileCreator.INSTANCE.createTempFile("FileBackedOutputStream"); if (resetOnFinalize) { // Finalizers are not guaranteed to be called on system shutdown; // this is insurance. |