Skip to content

Commit

Permalink
PDFBOX-4892: sync read/readFully code to get the same behaviour for a…
Browse files Browse the repository at this point in the history
…ll classes implementing the same interface

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/branches/2.0@1886911 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Feb 25, 2021
1 parent 845b090 commit 5c5a837
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
24 changes: 13 additions & 11 deletions pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.pdfbox.io;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -243,7 +244,7 @@ public int read(byte[] b, int offset, int length) throws IOException
checkClosed();
if (pointer >= size)
{
return 0;
return -1;
}
int bytesRead = readRemainingBytes(b, offset, length);
while (bytesRead < length && available() > 0)
Expand All @@ -259,10 +260,6 @@ public int read(byte[] b, int offset, int length) throws IOException

private int readRemainingBytes(byte[] b, int offset, int length)
{
if (pointer >= size)
{
return 0;
}
int maxLength = (int) Math.min(length, size-pointer);
int remainingBytes = chunkSize - currentBufferPointer;
// no more bytes left
Expand Down Expand Up @@ -500,13 +497,18 @@ public void rewind(int bytes) throws IOException
@Override
public byte[] readFully(int length) throws IOException
{
byte[] b = new byte[length];
int bytesRead = read(b);
while (bytesRead < length)
byte[] bytes = new byte[length];
int bytesRead = 0;
do
{
bytesRead += read(b, bytesRead, length - bytesRead);
}
return b;
int count = read(bytes, bytesRead, length - bytesRead);
if (count < 0)
{
throw new EOFException();
}
bytesRead += count;
} while (bytesRead < length);
return bytes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.pdfbox.io;

import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -336,13 +337,18 @@ public void rewind(int bytes) throws IOException
@Override
public byte[] readFully(int length) throws IOException
{
byte[] b = new byte[length];
int bytesRead = read(b);
while(bytesRead < length)
byte[] bytes = new byte[length];
int bytesRead = 0;
do
{
bytesRead += read(b, bytesRead, length-bytesRead);
}
return b;
int count = read(bytes, bytesRead, length - bytesRead);
if (count < 0)
{
throw new EOFException();
}
bytesRead += count;
} while (bytesRead < length);
return bytes;
}

@Override
Expand Down
16 changes: 7 additions & 9 deletions pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,20 @@ public void rewind(int bytes) throws IOException
* {@inheritDoc}
*/
@Override
public byte[] readFully(int len) throws IOException
public byte[] readFully(int length) throws IOException
{
byte[] b = new byte[len];

int n = 0;
byte[] bytes = new byte[length];
int bytesRead = 0;
do
{
int count = read(b, n, len - n);
int count = read(bytes, bytesRead, length - bytesRead);
if (count < 0)
{
throw new EOFException();
}
n += count;
} while (n < len);

return b;
bytesRead += count;
} while (bytesRead < length);
return bytes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.pdfbox.pdfparser;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
Expand Down Expand Up @@ -120,21 +121,16 @@ public void unread(byte[] bytes, int start, int len) throws IOException
public byte[] readFully(int length) throws IOException
{
byte[] bytes = new byte[length];
int off = 0;
int len = length;
while (len > 0)
int bytesRead = 0;
do
{
int n = this.read(bytes, off, len);
if (n > 0)
int count = read(bytes, bytesRead, length - bytesRead);
if (count < 0)
{
off += n;
len -= n;
throw new EOFException();
}
else
{
break;
}
}
bytesRead += count;
} while (bytesRead < length);
return bytes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.pdfbox.pdfparser;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -38,6 +39,16 @@ public void testPositionReadFully() throws IOException
inputStreamSource.readFully(5);
Assert.assertEquals(5, inputStreamSource.getPosition());

try
{
inputStreamSource.readFully(10);
Assert.fail("readFully beyond EOF should have triggered an EOFException");
}
catch(EOFException exception)
{

}

inputStreamSource.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.pdfbox.pdfparser;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;

import org.apache.pdfbox.io.RandomAccessBuffer;
Expand All @@ -41,6 +42,16 @@ public void testPositionReadFully() throws IOException
randomAccessSource.readFully(5);
Assert.assertEquals(5, randomAccessSource.getPosition());

try
{
randomAccessSource.readFully(10);
Assert.fail("readFully beyond EOF should have triggered an EOFException");
}
catch (EOFException exception)
{

}

randomAccessSource.close();
}

Expand Down

0 comments on commit 5c5a837

Please sign in to comment.