Author: tilman
Date: Sat Sep 2 10:08:46 2023
New Revision: 1912051
URL: http://svn.apache.org/viewvc?rev=1912051&view=rev
Log:
PDFBOX-5669: refactor and deprecate methods to use JDK 11 built-in methods, as
suggested by Axel Howind
Modified:
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java
Modified: pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java?rev=1912051&r1=1912050&r2=1912051&view=diff
==============================================================================
--- pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java (original)
+++ pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java Sat Sep 2
10:08:46 2023
@@ -27,7 +27,6 @@ import static java.lang.invoke.MethodHan
import static java.lang.invoke.MethodType.methodType;
import static java.util.Objects.nonNull;
-import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
@@ -79,12 +78,12 @@ public final class IOUtils
* @param in the input stream to read from.
* @return the byte array
* @throws IOException if an I/O error occurs
+ * @deprecated use {@link InputStream#readAllBytes()} instead
*/
+ @Deprecated
public static byte[] toByteArray(InputStream in) throws IOException
{
- ByteArrayOutputStream baout = new ByteArrayOutputStream();
- copy(in, baout);
- return baout.toByteArray();
+ return in.readAllBytes();
}
/**
@@ -93,18 +92,12 @@ public final class IOUtils
* @param output the output stream
* @return the number of bytes that have been copied
* @throws IOException if an I/O error occurs
+ * @deprecated use {@link InputStream#transferTo(OutputStream)} instead
*/
+ @Deprecated
public static long copy(InputStream input, OutputStream output) throws
IOException
{
- byte[] buffer = new byte[4096];
- long count = 0;
- int n = 0;
- while (-1 != (n = input.read(buffer)))
- {
- output.write(buffer, 0, n);
- count += n;
- }
- return count;
+ return input.transferTo(output);
}
/**
@@ -115,21 +108,12 @@ public final class IOUtils
* @param buffer the buffer to fill
* @return the number of bytes written to the buffer
* @throws IOException if an I/O error occurs
+ * @deprecated use {@link InputStream#readNBytes(byte[], int, int)} or
{@link InputStream#readNBytes(int)} instead
*/
+ @Deprecated
public static long populateBuffer(InputStream in, byte[] buffer) throws
IOException
{
- int remaining = buffer.length;
- while (remaining > 0)
- {
- int bufferWritePos = buffer.length - remaining;
- int bytesRead = in.read(buffer, bufferWritePos, remaining);
- if (bytesRead < 0)
- {
- break; //EOD
- }
- remaining -= bytesRead;
- }
- return (long) buffer.length - remaining;
+ return in.readNBytes(buffer, 0, buffer.length);
}
/**