[Git][java-team/libsejda-io-java][upstream] New upstream version 2.1.4
Markus Koschany (@apo)
gitlab at salsa.debian.org
Thu Aug 11 11:34:08 BST 2022
Markus Koschany pushed to branch upstream at Debian Java Maintainers / libsejda-io-java
Commits:
cb79ccda by Markus Koschany at 2022-08-11T12:29:01+02:00
New upstream version 2.1.4
- - - - -
16 changed files:
- + .github/workflows/build.yml
- − .travis.yml
- README.md
- pom.xml
- src/main/java/org/sejda/io/BaseSeekableSource.java
- src/main/java/org/sejda/io/BufferedCountingChannelWriter.java
- src/main/java/org/sejda/io/BufferedSeekableSource.java
- + src/main/java/org/sejda/io/OffsettableSeekableSource.java
- + src/main/java/org/sejda/io/OffsettableSeekableSourceImpl.java
- src/main/java/org/sejda/io/SeekableSource.java
- src/main/java/org/sejda/io/SeekableSources.java
- src/test/java/org/sejda/io/BaseTestSeekableSource.java
- src/test/java/org/sejda/io/BufferedSeekableSourceTest.java
- src/test/java/org/sejda/io/FileChannelSeekableSourceTest.java
- + src/test/java/org/sejda/io/OffsettableSeekableSourceImplTest.java
- src/test/java/org/sejda/io/SeekableSourcesTest.java
Changes:
=====================================
.github/workflows/build.yml
=====================================
@@ -0,0 +1,22 @@
+name: build
+
+on: [push]
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ java: ['8', '11', '17']
+ name: JDK ${{ matrix.java }}
+
+ steps:
+ - uses: actions/checkout at v2
+ - name: Set up JDK
+ uses: actions/setup-java at v2
+ with:
+ java-version: ${{ matrix.java }}
+ distribution: 'temurin'
+ cache: 'maven'
+ - name: Build with Maven
+ run: mvn --batch-mode --update-snapshots verify
\ No newline at end of file
=====================================
.travis.yml deleted
=====================================
@@ -1,7 +0,0 @@
-language: java
-sudo: false
-jdk:
- - openjdk8
-notifications:
- email:
- - info at sejda.org
=====================================
README.md
=====================================
@@ -1,6 +1,6 @@
Sejda-io (http://www.sejda.org)
=====
-[![Build Status](https://travis-ci.org/torakiki/sejda-io.png)](https://travis-ci.org/torakiki/sejda-io)
+![Build Status](https://github.com/torakiki/sejda-io/actions/workflows/build.yml/badge.svg)
[![License](http://img.shields.io/badge/license-APLv2-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
A layer of Input/Output classes built on top of java io and nio.
=====================================
pom.xml
=====================================
@@ -5,7 +5,7 @@
<artifactId>sejda-io</artifactId>
<packaging>jar</packaging>
<name>sejda-io</name>
- <version>2.1.3</version>
+ <version>2.1.4</version>
<description>An Input/Output layer built on top of Java standard io and nio packages</description>
<url>http://www.sejda.org</url>
@@ -37,7 +37,7 @@
<connection>scm:git:git at github.com:torakiki/sejda-io.git</connection>
<developerConnection>scm:git:git at github.com:torakiki/sejda-io.git</developerConnection>
<url>scm:git:git at github.com:torakiki/sejda-io.git</url>
- <tag>v2.1.3</tag>
+ <tag>v2.1.4</tag>
</scm>
<developers>
@@ -104,7 +104,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
- <version>3.0.1</version>
+ <version>3.3.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
@@ -122,7 +122,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
- <version>2.5.3</version>
+ <version>3.0.0-M4</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<preparationGoals>clean install</preparationGoals>
@@ -131,7 +131,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
- <version>1.6</version>
+ <version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
@@ -172,7 +172,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
- <version>3.1.2</version>
+ <version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
@@ -184,7 +184,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
- <version>3.1.0</version>
+ <version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
@@ -197,7 +197,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>3.0.0-M3</version>
+ <version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
@@ -205,41 +205,41 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
- <version>1.7.28</version>
+ <version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.sejda</groupId>
<artifactId>sejda-commons</artifactId>
- <version>1.1.0</version>
+ <version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
- <version>1.2.3</version>
+ <version>1.2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
- <version>1.2.3</version>
+ <version>1.2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
- <version>3.0.0</version>
+ <version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
- <version>5.5.1</version>
+ <version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
- <version>2.1</version>
+ <version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
=====================================
src/main/java/org/sejda/io/BaseSeekableSource.java
=====================================
@@ -45,11 +45,6 @@ public abstract class BaseSeekableSource implements SeekableSource {
this.open = false;
}
- /**
- * @throws IllegalStateException
- * if the source is closed
- * @throws IOException
- */
@Override
public void requireOpen() throws IOException {
requireState(isOpen(), "The SeekableSource has been closed");
=====================================
src/main/java/org/sejda/io/BufferedCountingChannelWriter.java
=====================================
@@ -63,7 +63,7 @@ public class BufferedCountingChannelWriter implements Closeable {
}
/**
- * Writes the given string in {@link Charsets#ISO_8859_1}
+ * Writes the given string in {@link StandardCharsets#ISO_8859_1}
*
* @param value
* @throws IOException
=====================================
src/main/java/org/sejda/io/BufferedSeekableSource.java
=====================================
@@ -17,6 +17,9 @@ package org.sejda.io;
import static java.util.Optional.ofNullable;
import static org.sejda.commons.util.RequireUtils.requireArg;
+import static org.sejda.commons.util.RequireUtils.requireNotBlank;
+import static org.sejda.commons.util.RequireUtils.requireNotNullArg;
+import static org.sejda.commons.util.RequireUtils.requireState;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -27,26 +30,42 @@ import org.sejda.commons.util.IOUtils;
* {@link SeekableSource} wrapping an existing one and providing buffered read. When a read method is called, a {@link SeekableSources#INPUT_BUFFER_SIZE_PROPERTY} long chunk of
* bytes is read from the underlying source and stored in memory. Subsequent reads are served from the in memory buffer until they fall outside its range, at that point a new
* buffer is read from the wrapped source.
- *
+ *
* @author Andrea Vacondio
*/
-public class BufferedSeekableSource extends BaseSeekableSource {
+public class BufferedSeekableSource implements SeekableSource {
- private ByteBuffer buffer = ByteBuffer
- .allocate(Integer.getInteger(SeekableSources.INPUT_BUFFER_SIZE_PROPERTY, 8192));
+ private ByteBuffer buffer = ByteBuffer.allocate(
+ Integer.getInteger(SeekableSources.INPUT_BUFFER_SIZE_PROPERTY, 8192));
private SeekableSource wrapped;
private long position;
private long size;
public BufferedSeekableSource(SeekableSource wrapped) {
- super(ofNullable(wrapped).map(SeekableSource::id).orElseThrow(() -> {
- return new IllegalArgumentException("Input decorated SeekableSource cannot be null");
- }));
+ requireNotNullArg(wrapped, "Input decorated SeekableSource cannot be null");
this.wrapped = wrapped;
this.size = wrapped.size();
this.buffer.limit(0);
}
+ @Override
+ public boolean isOpen() {
+ return this.wrapped.isOpen();
+ }
+
+ @Override
+ public void requireOpen() throws IOException {
+ this.wrapped.requireOpen();
+ }
+
+ /**
+ * @return the unique id for this source
+ */
+ @Override
+ public String id() {
+ return this.wrapped.id();
+ }
+
@Override
public long position() {
return this.position;
@@ -75,7 +94,6 @@ public class BufferedSeekableSource extends BaseSeekableSource {
@Override
public void close() throws IOException {
- super.close();
IOUtils.close(wrapped);
buffer.clear();
buffer.limit(0);
=====================================
src/main/java/org/sejda/io/OffsettableSeekableSource.java
=====================================
@@ -0,0 +1,34 @@
+package org.sejda.io;
+/*
+ * Copyright 2022 Sober Lemur S.a.s. di Vacondio Andrea and Sejda BV
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+
+/**
+ * A {@link SeekableSource} that can be offsetted by a given number of bytes
+ *
+ * @author Andrea Vacondio
+ */
+public interface OffsettableSeekableSource extends SeekableSource {
+
+ /**
+ * Sets the offset for this source
+ *
+ * @param offset
+ * @throws IllegalArgumentException if the offset is negative or if it would lead to a negative size
+ */
+ void offset(long offset) throws IOException;
+}
=====================================
src/main/java/org/sejda/io/OffsettableSeekableSourceImpl.java
=====================================
@@ -0,0 +1,99 @@
+package org.sejda.io;
+/*
+ * Copyright 2022 Sober Lemur S.a.s. di Vacondio Andrea and Sejda BV
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.sejda.commons.util.IOUtils;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import static org.sejda.commons.util.RequireUtils.requireArg;
+import static org.sejda.commons.util.RequireUtils.requireNotNullArg;
+
+/**
+ * A decorator for a {@link SeekableSource} that adds the concept of offset.
+ *
+ * @author Andrea Vacondio
+ */
+class OffsettableSeekableSourceImpl implements OffsettableSeekableSource {
+
+ private SeekableSource wrapped;
+ private long offset = 0;
+
+ public OffsettableSeekableSourceImpl(SeekableSource wrapped) {
+ requireNotNullArg(wrapped, "Input decorated SeekableSource cannot be null");
+ this.wrapped = wrapped;
+ }
+
+ @Override
+ public String id() {
+ return this.wrapped.id();
+ }
+
+ @Override
+ public boolean isOpen() {
+ return this.wrapped.isOpen();
+ }
+
+ @Override
+ public void requireOpen() throws IOException {
+ this.wrapped.requireOpen();
+ }
+
+ @Override
+ public long size() {
+ return wrapped.size() - offset;
+ }
+
+ @Override
+ public void offset(long offset) throws IOException{
+ requireArg(offset >= 0, "Cannot set a negative offset");
+ requireArg((wrapped.size() - offset) >= 0, "Invalid offset bigger then the wrapped source size");
+ this.offset = offset;
+ this.wrapped.position(this.wrapped.position() + offset);
+ }
+
+ @Override
+ public long position() throws IOException {
+ return this.wrapped.position() - offset;
+ }
+
+ @Override
+ public SeekableSource position(long position) throws IOException {
+ return this.wrapped.position(position + offset);
+ }
+
+ @Override
+ public int read() throws IOException {
+ return this.wrapped.read();
+ }
+
+ @Override
+ public SeekableSource view(long startingPosition, long length) throws IOException {
+ return this.wrapped.view(startingPosition + offset, length);
+ }
+
+ @Override
+ public int read(ByteBuffer dst) throws IOException {
+ return this.wrapped.read(dst);
+ }
+
+ @Override
+ public void close() throws IOException {
+ IOUtils.close(wrapped);
+ }
+
+}
=====================================
src/main/java/org/sejda/io/SeekableSource.java
=====================================
@@ -21,7 +21,7 @@ import java.nio.channels.ReadableByteChannel;
/**
* Readable source that provides random access capabilities.
- *
+ *
* @author Andrea Vacondio
*/
public interface SeekableSource extends ReadableByteChannel {
@@ -39,7 +39,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Sets the source position. Setting the position to a value that is greater than the source's size is legal but does not change the size of the source. A later attempt to read
* bytes at such a position will immediately return an end-of-file indication.
- *
+ *
* @param position
* a non-negative long for the new position
* @return this source
@@ -54,7 +54,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Reads a byte of data from this source. The byte is returned as an integer in the range 0 to 255 ( {@code 0x00-0xff} ).
- *
+ *
* @return the next byte of data, or {@code -1} if there is no more data.
* @throws IOException
*/
@@ -73,7 +73,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Skips backward the given number of bytes moving back the source position
- *
+ *
* @param offset
* the number of bytes to skip back.
* @return this source
@@ -91,7 +91,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Skips backward moving back the source position of one byte
- *
+ *
* @return this source
* @throws IOException
* @see SeekableSource#back(long)
@@ -101,9 +101,8 @@ public interface SeekableSource extends ReadableByteChannel {
}
/**
- *
* Skips the given number of bytes moving forward the source position
- *
+ *
* @param offset
* the number of bytes to skip .
* @return this source
@@ -115,7 +114,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Reads the next byte and sets the position back by one.
- *
+ *
* @return the next byte or {@code -1} if there is no more data.
* @throws IOException
* @see #read()
@@ -130,7 +129,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Reads the previous byte and sets the position back where it was.
- *
+ *
* @return the previous byte or {@code -1} if we are at the beginning of the source.
* @throws IOException
* @see #read()
@@ -145,7 +144,7 @@ public interface SeekableSource extends ReadableByteChannel {
/**
* Creates an {@link InputStream} from this {@link SeekableSource}.
- *
+ *
* @return the input stream wrapping the given {@link SeekableSource}
*/
default InputStream asInputStream() {
@@ -164,6 +163,10 @@ public interface SeekableSource extends ReadableByteChannel {
}
}
+ /**
+ * @throws IllegalStateException if the source is closed
+ * @throws IOException
+ */
void requireOpen() throws IOException;
/**
@@ -173,7 +176,6 @@ public interface SeekableSource extends ReadableByteChannel {
*/
default InputStream asNewInputStream() {
reset();
-
return new SeekableSourceInputStream(this);
}
}
=====================================
src/main/java/org/sejda/io/SeekableSources.java
=====================================
@@ -21,7 +21,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
-import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.sejda.commons.util.IOUtils;
@@ -29,7 +28,7 @@ import org.sejda.commons.util.IOUtils;
/**
* This class consists of solely static methods to create the most appropriate {@link SeekableSource} based on the given input or to bridge {@link SeekableSource}s to the more
* traditional {@link InputStream} or other standard I/O classes.
- *
+ *
* @author Andrea Vacondio
*
*/
@@ -61,7 +60,7 @@ public final class SeekableSources {
/**
* Factory method to create a {@link SeekableSource} from a {@link File}. An attempt is made to return the best {@link SeekableSource} implementation based on the size of the
* file and bitness of the JVM.
- *
+ *
* @param file
* @return a {@link SeekableSource} from the given file.
* @throws IOException
@@ -78,7 +77,7 @@ public final class SeekableSources {
/**
* Factory method to create a {@link SeekableSource} from a {@link InputStream}. The whole stream is read an stored in a byte array with a max size of 2GB.
- *
+ *
* @param stream
* @return a {@link SeekableSource} from the given stream.
* @throws IOException
@@ -90,7 +89,7 @@ public final class SeekableSources {
/**
* Factory method to create a {@link SeekableSource} from a byte array.
- *
+ *
* @param bytes
* @return a {@link SeekableSource} wrapping the given byte array.
*/
@@ -112,7 +111,7 @@ public final class SeekableSources {
/**
* Factory method to create a {@link SeekableSource} from a {@link InputStream}. The whole stream is copied to a temporary file.
- *
+ *
* @param stream
* @param filenameHint name to use for the temp file that will be created
* @return a {@link SeekableSource} from the given stream.
@@ -125,7 +124,7 @@ public final class SeekableSources {
if(temp.exists()) {
throw new RuntimeException("Temp file collision: "+ temp.getAbsolutePath());
}
-
+
Files.copy(stream, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
return new BufferedSeekableSource(new FileChannelSeekableSource(temp) {
@Override
@@ -135,4 +134,14 @@ public final class SeekableSources {
}
});
}
+
+ /**
+ * Factory method to create an {@link OffsettableSeekableSource} from a {@link SeekableSource}
+ * @param source
+ * @return
+ */
+ public static OffsettableSeekableSource asOffsettable(SeekableSource source) {
+ requireNonNull(source);
+ return new OffsettableSeekableSourceImpl(source);
+ }
}
=====================================
src/test/java/org/sejda/io/BaseTestSeekableSource.java
=====================================
@@ -140,4 +140,18 @@ public abstract class BaseTestSeekableSource {
assertEquals(-1, victim().peekBack());
}
+ @Test
+ public void requireOpen() throws IOException {
+ assertTrue(victim().isOpen());
+ victim().requireOpen();
+ }
+
+ @Test
+ public void failingRequireOpen() throws IOException {
+ assertTrue(victim().isOpen());
+ victim().close();
+ assertThrows(IllegalStateException.class, () -> {
+ victim().requireOpen();
+ });
+ }
}
=====================================
src/test/java/org/sejda/io/BufferedSeekableSourceTest.java
=====================================
@@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
/**
* @author Andrea Vacondio
- *
*/
public class BufferedSeekableSourceTest extends BaseTestSeekableSource {
private ByteArraySeekableSource wrapped;
@@ -111,6 +110,12 @@ public class BufferedSeekableSourceTest extends BaseTestSeekableSource {
assertEquals(-1, victim.read());
}
+ @Test
+ public void closedWrapped() throws IOException {
+ this.wrapped.close();
+ assertFalse(victim.isOpen());
+ }
+
@Override
SeekableSource victim() {
return victim;
=====================================
src/test/java/org/sejda/io/FileChannelSeekableSourceTest.java
=====================================
@@ -15,6 +15,11 @@
*/
package org.sejda.io;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -26,19 +31,15 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
-
/**
* @author Andrea Vacondio
*/
-public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
-{
+public class FileChannelSeekableSourceTest extends BaseTestSeekableSource {
private FileChannelSeekableSource victim;
private Path tempFile;
@BeforeEach
- public void setUp() throws Exception
- {
+ public void setUp() throws Exception {
tempFile = Files.createTempFile("SejdaIO", null);
Files.copy(getClass().getResourceAsStream("/pdf/simple_test.pdf"), tempFile,
StandardCopyOption.REPLACE_EXISTING);
@@ -46,22 +47,19 @@ public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
}
@AfterEach
- public void after() throws IOException
- {
+ public void after() throws IOException {
Files.deleteIfExists(tempFile);
}
@Test
- public void failingConstructor()
- {
+ public void failingConstructor() {
assertThrows(IllegalArgumentException.class, () -> {
new FileChannelSeekableSource(null);
}, "Input file cannot be null");
}
@Test
- public void read() throws IOException
- {
+ public void read() throws IOException {
assertEquals(0, victim.position());
assertNotNull(victim.read());
assertNotNull(victim.read());
@@ -71,8 +69,7 @@ public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
}
@Test
- public void readBuff() throws IOException
- {
+ public void readBuff() throws IOException {
ByteBuffer dst = ByteBuffer.allocate(20);
victim.read(dst);
dst.flip();
@@ -86,8 +83,7 @@ public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
}
@Test
- public void reset() throws IOException
- {
+ public void reset() throws IOException {
ByteBuffer dst = ByteBuffer.allocate(20);
victim.read(dst);
assertEquals(20, victim.position());
@@ -96,8 +92,7 @@ public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
}
@Test
- public void newInputStream() throws IOException
- {
+ public void newInputStream() throws IOException {
int length = 20;
byte[] buffer1 = new byte[length];
byte[] buffer2 = new byte[length];
@@ -110,8 +105,7 @@ public class FileChannelSeekableSourceTest extends BaseTestSeekableSource
}
@Override
- SeekableSource victim()
- {
+ SeekableSource victim() {
return victim;
}
=====================================
src/test/java/org/sejda/io/OffsettableSeekableSourceImplTest.java
=====================================
@@ -0,0 +1,123 @@
+package org.sejda.io;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/*
+ * Copyright 2022 Sober Lemur S.a.s. di Vacondio Andrea and Sejda BV
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class OffsettableSeekableSourceImplTest extends BaseTestSeekableSource {
+
+ private ByteArraySeekableSource wrapped;
+ private OffsettableSeekableSource victim;
+
+ @BeforeEach
+ public void setUp() throws IOException {
+ wrapped = new ByteArraySeekableSource(new byte[] { 'd', 'e', 'f', 'a', 'b', 'c' });
+ victim = SeekableSources.asOffsettable(wrapped);
+ victim.offset(3);
+ }
+
+ @Test
+ public void size() {
+ assertEquals(3, victim.size());
+ }
+
+ @Test
+ public void failingConstructor() {
+ assertThrows(IllegalArgumentException.class, () -> {
+ new OffsettableSeekableSourceImpl(null);
+ }, "Input decorated SeekableSource cannot be null");
+ }
+
+ @Test
+ public void constructor() {
+ assertEquals(wrapped.id(), victim.id());
+ }
+
+ @Override
+ @Test
+ public void close() throws IOException {
+ victim.read();
+ assertTrue(victim.isOpen());
+ assertTrue(wrapped.isOpen());
+ victim.close();
+ assertFalse(victim.isOpen());
+ assertFalse(wrapped.isOpen());
+ }
+
+ @Test
+ public void read() throws IOException {
+ assertEquals(97, victim.read());
+ assertEquals(1, victim.position());
+ assertEquals(98, victim.read());
+ assertEquals(2, victim.position());
+ assertEquals(99, victim.read());
+ assertEquals(3, victim.position());
+ assertEquals(-1, victim.read());
+ assertEquals(3, victim.position());
+ }
+
+ @Test
+ public void readBuff() throws IOException {
+ victim.position(1);
+ ByteBuffer dst = ByteBuffer.allocate(10);
+ victim.read(dst);
+ dst.flip();
+ assertEquals(2, dst.remaining());
+ assertEquals(98, dst.get());
+ assertEquals(99, dst.get());
+ ByteBuffer empty = ByteBuffer.allocate(10);
+ victim.read(empty);
+ empty.flip();
+ assertFalse(empty.hasRemaining());
+ }
+
+ @Test
+ public void position() throws IOException {
+ assertEquals(0, victim.position());
+ assertEquals(97, victim.read());
+ victim.position(0);
+ assertEquals(0, victim.position());
+ victim.position(0);
+ assertEquals(0, victim.position());
+ victim.position(2);
+ assertEquals(2, victim.position());
+ victim.read();
+ assertEquals(3, victim.position());
+ assertEquals(-1, victim.read());
+ victim.position(2);
+ assertEquals(2, victim.position());
+ victim.position(20);
+ assertEquals(3, victim.position());
+ assertEquals(-1, victim.read());
+ }
+
+ @Test
+ public void closedWrapped() throws IOException {
+ this.wrapped.close();
+ assertFalse(victim.isOpen());
+ }
+
+ @Override
+ SeekableSource victim() {
+ return victim;
+ }
+}
\ No newline at end of file
=====================================
src/test/java/org/sejda/io/SeekableSourcesTest.java
=====================================
@@ -15,11 +15,10 @@
*/
package org.sejda.io;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.core.StringEndsWith.endsWith;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -28,14 +27,19 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.io.TempDir;
-
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.StringEndsWith.endsWith;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.sejda.io.SeekableSources.DISABLE_MEMORY_MAPPED_PROPERTY;
+import static org.sejda.io.SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY;
+import static org.sejda.io.SeekableSources.asOffsettable;
+import static org.sejda.io.SeekableSources.inMemorySeekableSourceFrom;
+import static org.sejda.io.SeekableSources.onTempFileSeekableSourceFrom;
+import static org.sejda.io.SeekableSources.seekableSourceFrom;
/**
* @author Andrea Vacondio
- *
*/
public class SeekableSourcesTest {
@@ -55,7 +59,7 @@ public class SeekableSourcesTest {
@Test
public void nullSeekableSourceFrom() {
assertThrows(NullPointerException.class, () -> {
- SeekableSources.seekableSourceFrom(null);
+ seekableSourceFrom(null);
});
}
@@ -63,7 +67,7 @@ public class SeekableSourcesTest {
@Test
public void nullInMemorySeekableSourceFromBytes() {
assertThrows(NullPointerException.class, () -> {
- SeekableSources.inMemorySeekableSourceFrom((byte[]) null);
+ inMemorySeekableSourceFrom((byte[]) null);
});
}
@@ -71,96 +75,109 @@ public class SeekableSourcesTest {
@Test
public void nullInMemorySeekableSourceFromStream() {
assertThrows(NullPointerException.class, () -> {
- SeekableSources.inMemorySeekableSourceFrom((InputStream) null);
+ inMemorySeekableSourceFrom((InputStream) null);
});
}
@Test
public void nullOnTempFileSeekableSourceFrom() {
assertThrows(NullPointerException.class, () -> {
- SeekableSources.onTempFileSeekableSourceFrom(null);
+ onTempFileSeekableSourceFrom(null);
+ });
+ }
+
+ @Test
+ public void nullOffsettableSource() {
+ assertThrows(NullPointerException.class, () -> {
+ asOffsettable(null);
});
}
@Test
- public void seekableSourceFrom(@TempDir Path temp) throws IOException {
+ public void seekableSourceFromTest(@TempDir Path temp) throws IOException {
Path test = temp.resolve("test.txt");
Files.createFile(test);
- assertNotNull(SeekableSources.seekableSourceFrom(test.toFile()));
+ assertNotNull(seekableSourceFrom(test.toFile()));
}
@Test
public void aboveThresholdSeekableSourceFrom() throws IOException {
try {
- System.setProperty(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
+ System.setProperty(MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
Path tempFile = Files.createTempFile("SejdaIO", null);
Files.copy(getClass().getResourceAsStream("/pdf/simple_test.pdf"), tempFile,
StandardCopyOption.REPLACE_EXISTING);
- SeekableSource source = SeekableSources.seekableSourceFrom(tempFile.toFile());
+ SeekableSource source = seekableSourceFrom(tempFile.toFile());
assertNotNull(source);
assertTrue(source instanceof BufferedSeekableSource);
assertTrue(((BufferedSeekableSource) source).wrapped() instanceof MemoryMappedSeekableSource);
} finally {
- System.getProperties().remove(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY);
+ System.getProperties().remove(MAPPED_SIZE_THRESHOLD_PROPERTY);
}
}
@Test
public void disableMemoryMapped() throws IOException {
try {
- System.setProperty(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
- System.setProperty(SeekableSources.DISABLE_MEMORY_MAPPED_PROPERTY, "true");
+ System.setProperty(MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
+ System.setProperty(DISABLE_MEMORY_MAPPED_PROPERTY, "true");
Path tempFile = Files.createTempFile("SejdaIO", null);
Files.copy(getClass().getResourceAsStream("/pdf/simple_test.pdf"), tempFile,
StandardCopyOption.REPLACE_EXISTING);
- SeekableSource source = SeekableSources.seekableSourceFrom(tempFile.toFile());
+ SeekableSource source = seekableSourceFrom(tempFile.toFile());
assertNotNull(source);
assertTrue(source instanceof BufferedSeekableSource);
assertTrue(((BufferedSeekableSource) source).wrapped() instanceof FileChannelSeekableSource);
} finally {
- System.getProperties().remove(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY);
- System.getProperties().remove(SeekableSources.DISABLE_MEMORY_MAPPED_PROPERTY);
+ System.getProperties().remove(MAPPED_SIZE_THRESHOLD_PROPERTY);
+ System.getProperties().remove(DISABLE_MEMORY_MAPPED_PROPERTY);
}
}
@Test
public void nonMappedMemoryFor32bits() throws IOException {
try {
- System.setProperty(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
+ System.setProperty(MAPPED_SIZE_THRESHOLD_PROPERTY, "10");
System.setProperty("sun.arch.data.model", "32");
Path tempFile = Files.createTempFile("SejdaIO", null);
Files.copy(getClass().getResourceAsStream("/pdf/simple_test.pdf"), tempFile,
StandardCopyOption.REPLACE_EXISTING);
- SeekableSource source = SeekableSources.seekableSourceFrom(tempFile.toFile());
+ SeekableSource source = seekableSourceFrom(tempFile.toFile());
assertNotNull(source);
assertTrue(source instanceof BufferedSeekableSource);
assertTrue(((BufferedSeekableSource) source).wrapped() instanceof FileChannelSeekableSource);
} finally {
- System.getProperties().remove(SeekableSources.MAPPED_SIZE_THRESHOLD_PROPERTY);
+ System.getProperties().remove(MAPPED_SIZE_THRESHOLD_PROPERTY);
System.setProperty("sun.arch.data.model", BITNESS);
}
}
@Test
public void inMemorySeekableSourceFromBytes() {
- assertNotNull(SeekableSources.inMemorySeekableSourceFrom(new byte[] { -1 }));
+ assertNotNull(inMemorySeekableSourceFrom(new byte[] { -1 }));
}
@Test
public void inMemorySeekableSourceFromStream() throws IOException {
assertNotNull(
- SeekableSources.inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/simple_test.pdf")));
+ inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/simple_test.pdf")));
+ }
+
+ @Test
+ public void asOffsettableTest() throws IOException {
+ assertNotNull(asOffsettable(onTempFileSeekableSourceFrom(new ByteArrayInputStream(new byte[] { -1 }))));
}
@Test
- public void onTempFileSeekableSourceFrom() throws IOException {
- assertNotNull(SeekableSources.onTempFileSeekableSourceFrom(new ByteArrayInputStream(new byte[] { -1 })));
+ public void onTempFileSeekableSourceFromTest() throws IOException {
+ assertNotNull(onTempFileSeekableSourceFrom(new ByteArrayInputStream(new byte[] { -1 })));
}
@Test
public void onTempFileSeekableSourceFromWithFilenameHint() throws IOException {
String filenameHint = "input.pdf";
- SeekableSource result = SeekableSources.onTempFileSeekableSourceFrom(new ByteArrayInputStream(new byte[] { -1 }), filenameHint);
+ SeekableSource result = onTempFileSeekableSourceFrom(
+ new ByteArrayInputStream(new byte[] { -1 }), filenameHint);
assertThat(result.id(), endsWith(filenameHint));
}
View it on GitLab: https://salsa.debian.org/java-team/libsejda-io-java/-/commit/cb79ccda57cd5fc70c2348b6f9a792bf24614570
--
View it on GitLab: https://salsa.debian.org/java-team/libsejda-io-java/-/commit/cb79ccda57cd5fc70c2348b6f9a792bf24614570
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20220811/43606b28/attachment.htm>
More information about the pkg-java-commits
mailing list