This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit c28846150343c2e5601288359fa1fa91b5c8f978 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Sun Jan 15 15:43:10 2023 +0100 Consolidate the constants for `ByteBuffer` capacity and increase the default capacity from 8 kb to 16 kb. --- .../storage/inflater/CompressionChannel.java | 6 ++++-- .../internal/storage/io/FileCacheByteChannel.java | 3 ++- .../internal/storage/io/RewindableLineReader.java | 7 ++++-- .../org/apache/sis/storage/ProbeInputStream.java | 2 +- .../java/org/apache/sis/storage/ProbeReader.java | 2 +- .../org/apache/sis/storage/StorageConnector.java | 25 +++++++++++++--------- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/storage/inflater/CompressionChannel.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/storage/inflater/CompressionChannel.java index 860a7aff47..bef43cb3d0 100644 --- a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/storage/inflater/CompressionChannel.java +++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/storage/inflater/CompressionChannel.java @@ -24,6 +24,7 @@ import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.geotiff.Resources; import org.apache.sis.internal.storage.io.ChannelDataInput; import org.apache.sis.storage.event.StoreListeners; +import org.apache.sis.storage.StorageConnector; /** @@ -39,9 +40,10 @@ import org.apache.sis.storage.event.StoreListeners; abstract class CompressionChannel extends PixelChannel { /** * Desired size of the buffer where to temporarily copy decompressed data. - * The actual buffer size may be larger, but should not be smaller. + * The actual buffer size may become larger (but not smaller) + * because we try to use a multiple of scanline stride. */ - private static final int BUFFER_SIZE = 4096; + private static final int BUFFER_SIZE = StorageConnector.DEFAULT_BUFFER_SIZE / 2; /** * The source of data to decompress. diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/FileCacheByteChannel.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/FileCacheByteChannel.java index 986b804628..3234dcfd08 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/FileCacheByteChannel.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/FileCacheByteChannel.java @@ -25,6 +25,7 @@ import java.nio.file.StandardOpenOption; import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; import java.nio.channels.NonWritableChannelException; +import org.apache.sis.storage.StorageConnector; import org.apache.sis.internal.storage.Resources; import org.apache.sis.internal.system.DelayedExecutor; import org.apache.sis.internal.system.DelayedRunnable; @@ -61,7 +62,7 @@ public abstract class FileCacheByteChannel implements SeekableByteChannel { /** * Size of the transfer buffer, in number of bytes. */ - private static final int BUFFER_SIZE = 8 * 1024; + private static final int BUFFER_SIZE = StorageConnector.DEFAULT_BUFFER_SIZE; /** * Threshold for implementing a change of position by closing current connection and opening a new one. diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/RewindableLineReader.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/RewindableLineReader.java index fb1fc90f60..d9d1d633ea 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/RewindableLineReader.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/RewindableLineReader.java @@ -23,6 +23,7 @@ import java.io.LineNumberReader; import java.nio.charset.Charset; import org.apache.sis.util.resources.Errors; import org.apache.sis.io.InvalidSeekException; +import org.apache.sis.storage.StorageConnector; /** @@ -42,9 +43,11 @@ import org.apache.sis.io.InvalidSeekException; @SuppressWarnings("SynchronizeOnNonFinalField") public final class RewindableLineReader extends LineNumberReader { /** - * Size of the buffer, in number of characters. + * Size of the buffer, in number of characters. The number of bytes is twice this amount. + * This is also the maximal "read ahead limit" that can be passed to {@link #mark(int)} + * without causing buffer reallocation. */ - public static final int BUFFER_SIZE = 8192; + public static final int BUFFER_SIZE = StorageConnector.DEFAULT_BUFFER_SIZE / 2; /** * The input stream, or {@code null} if this reader cannot rewind anymore. diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeInputStream.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeInputStream.java index dee9603fa9..888d9a52d3 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeInputStream.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeInputStream.java @@ -45,7 +45,7 @@ final class ProbeInputStream extends FilterInputStream { if (!input.markSupported()) { throw new DataStoreException(Resources.format(Resources.Keys.MarkNotSupported_1, owner.getStorageName())); } - input.mark(StorageConnector.DEFAULT_BUFFER_SIZE); + input.mark(StorageConnector.READ_AHEAD_LIMIT); } /** diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeReader.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeReader.java index 5f25cba743..5ecc30d27f 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeReader.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeReader.java @@ -48,7 +48,7 @@ final class ProbeReader extends FilterReader { if (!input.markSupported()) { throw new DataStoreException(Resources.format(Resources.Keys.MarkNotSupported_1, owner.getStorageName())); } - input.mark(StorageConnector.DEFAULT_BUFFER_SIZE); + input.mark(StorageConnector.READ_AHEAD_LIMIT); } /** diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java index 9fcb237b2a..88a47baa62 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java @@ -110,17 +110,22 @@ public class StorageConnector implements Serializable { private static final long serialVersionUID = 2524083964906593093L; /** - * The default size of the {@link ByteBuffer} to be created. + * The default size (in bytes) of {@link ByteBuffer}s created by storage connectors. + * Those buffers are typically created when the specified storage object is a + * {@link File}, {@link Path}, {@link URL} or {@link URI}. * Users can override this value by providing a value for {@link OptionKey#BYTE_BUFFER}. * - * <p>This buffer capacity is also used as read-ahead limit for mark operations. - * The rational is to allow as many bytes as contained in buffers of default size. - * For increasing the chances to meet that goal, this size should be the same than - * {@link java.io.BufferedInputStream} default buffer size.</p> - * - * @see RewindableLineReader#BUFFER_SIZE + * @since 1.4 + */ + public static final int DEFAULT_BUFFER_SIZE = 16 * 1024; + + /** + * The read-ahead limit for mark operations. + * We try to allow as many bytes as contained in buffers of default size. + * For increasing the chances to meet that goal, this size should be the + * same than {@link BufferedInputStream} default buffer size. */ - static final int DEFAULT_BUFFER_SIZE = 8192; + static final int READ_AHEAD_LIMIT = 8 * 1024; /** * The minimal size of the {@link ByteBuffer} to be created. This size is used only @@ -962,7 +967,7 @@ public class StorageConnector implements Serializable { */ reset(); if (storage instanceof InputStream) { - ((InputStream) storage).mark(DEFAULT_BUFFER_SIZE); + ((InputStream) storage).mark(READ_AHEAD_LIMIT); } if (storage instanceof ByteBuffer) { final ChannelDataInput asDataInput = new ChannelImageInputStream(getStorageName(), (ByteBuffer) storage); @@ -1256,7 +1261,7 @@ public class StorageConnector implements Serializable { addView(Reader.class, null); // Remember that there is no view. return null; } - input.mark(DEFAULT_BUFFER_SIZE); + input.mark(READ_AHEAD_LIMIT); final Reader in = new RewindableLineReader(input, getOption(OptionKey.ENCODING)); addView(Reader.class, in, InputStream.class, (byte) (CLEAR_ON_RESET | CASCADE_ON_RESET)); return in;