Merge branch '2.0.x'
This commit is contained in:
commit
a193c89c6d
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.loader.data;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -56,6 +57,8 @@ public interface RandomAccessData {
|
|||
* @param length the number of bytes to be read
|
||||
* @return the data
|
||||
* @throws IOException if the data cannot be read
|
||||
* @throws IndexOutOfBoundsException if offset is beyond the end of the file or subsection
|
||||
* @throws EOFException if offset plus length is greater than the length of the file or subsection
|
||||
*/
|
||||
byte[] read(long offset, long length) throws IOException;
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.loader.data;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
@ -90,6 +91,12 @@ public class RandomAccessDataFile implements RandomAccessData {
|
|||
|
||||
@Override
|
||||
public byte[] read(long offset, long length) throws IOException {
|
||||
if (offset > this.length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (offset + length > this.length) {
|
||||
throw new EOFException();
|
||||
}
|
||||
byte[] bytes = new byte[(int) length];
|
||||
read(bytes, offset, 0, bytes.length);
|
||||
return bytes;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.loader.data;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
@ -96,6 +97,38 @@ public class RandomAccessDataFileTests {
|
|||
new RandomAccessDataFile(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithOffsetAndLengthShouldRead() throws Exception {
|
||||
byte[] read = this.file.read(2, 3);
|
||||
assertThat(read).isEqualTo(new byte[] { 2, 3, 4 });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWhenOffsetIsBeyondEOFShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IndexOutOfBoundsException.class);
|
||||
this.file.read(257, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWhenOffsetIsBeyondEndOfSubsectionShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IndexOutOfBoundsException.class);
|
||||
RandomAccessData subsection = this.file.getSubsection(0, 10);
|
||||
subsection.read(11, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWhenOffsetPlusLengthGreaterThanEOFShouldThrowException() throws Exception {
|
||||
this.thrown.expect(EOFException.class);
|
||||
this.file.read(256, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWhenOffsetPlusLengthGreaterThanEndOfSubsectionShouldThrowException() throws Exception {
|
||||
this.thrown.expect(EOFException.class);
|
||||
RandomAccessData subsection = this.file.getSubsection(0, 10);
|
||||
subsection.read(10, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inputStreamRead() throws Exception {
|
||||
for (int i = 0; i <= 255; i++) {
|
||||
|
|
Loading…
Reference in New Issue