-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package pega.io; | ||
|
||
import java.io.*; | ||
|
||
public class FileInputProvider implements InputProvider { | ||
|
||
private final File file; | ||
private final int startPosition; | ||
private final int numberOfElementsToRead; | ||
private final int intSize = 4; | ||
|
||
public FileInputProvider(File file, Integer startPosition, Integer numberOfElementsToRead) { | ||
|
||
this.file = file; | ||
this.startPosition = startPosition; | ||
this.numberOfElementsToRead = numberOfElementsToRead; | ||
} | ||
|
||
@Override | ||
public int[] getInput() throws IOException { | ||
int[] buffer = new int[numberOfElementsToRead]; | ||
|
||
try (RandomAccessFile input = new RandomAccessFile(file, "r")) { | ||
input.seek(startPosition * intSize); | ||
|
||
for (int index = 0; index < numberOfElementsToRead; index++) { | ||
buffer[index] = input.readInt(); | ||
} | ||
} | ||
|
||
return buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package pega.io; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
public interface InputProvider { | ||
int[] getInput(); | ||
int[] getInput() throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package pega.io; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FileInputProviderTest { | ||
|
||
@Rule | ||
public TemporaryFolder folder = new TemporaryFolder(); | ||
private File file; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
file = folder.newFile("test"); | ||
|
||
try (RandomAccessFile accessFile = new RandomAccessFile( | ||
file, | ||
"rw" | ||
)) { | ||
for (int i = 0; i < 10; i++) { | ||
accessFile.writeInt(i + 1); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetAllData() throws IOException { | ||
FileInputProvider provider = new FileInputProvider( | ||
file, | ||
0, | ||
10 | ||
); | ||
|
||
int[] expectedResult = {1, 2 ,3 ,4 ,5, 6, 7, 8, 9, 10}; | ||
assertArrayEquals(expectedResult, provider.getInput()); | ||
} | ||
|
||
@Test | ||
public void testGetDataToMiddle() throws IOException { | ||
FileInputProvider provider = new FileInputProvider( | ||
file, | ||
0, | ||
5 | ||
); | ||
|
||
int[] expectedResult = {1, 2 ,3 ,4 ,5}; | ||
assertArrayEquals(expectedResult, provider.getInput()); | ||
} | ||
|
||
@Test | ||
public void testGetDataFromMiddle() throws IOException { | ||
FileInputProvider provider = new FileInputProvider( | ||
file, | ||
5, | ||
5 | ||
); | ||
|
||
int[] expectedResult = {6, 7, 8, 9, 10}; | ||
assertArrayEquals(expectedResult, provider.getInput()); | ||
} | ||
} |