8000 Java I/O - Paths, files, buffers, scanning and formatting · yogiprogram/Java-Coding-Problems@6bf74a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bf74a8

Browse files
Java I/O - Paths, files, buffers, scanning and formatting
1 parent 3520fb1 commit 6bf74a8

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Chapter06/P140_SearchInBigFiles/src/modern/challenge/TextFiles.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static long countOccurrencesV4(Path path, String text, Charset ch) throws
109109
return count;
110110
}
111111

112-
public static int countOccurrencesV5(Path path, String text) throws IOException {
112+
public static long countOccurrencesV5(Path path, String text) throws IOException {
113113

114114
if (path == null || text == null) {
115115
throw new IllegalArgumentException("Path/text cannot be null");
@@ -121,20 +121,20 @@ public static int countOccurrencesV5(Path path, String text) throws IOException
121121

122122
final byte[] texttofind = text.getBytes(StandardCharsets.UTF_8);
123123

124-
int count = 0;
124+
long count = 0;
125125
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) {
126126

127-
int position = 0;
127+
long position = 0;
128128
long 9439 length = fileChannel.size();
129129
while (position < length) {
130130

131131
long remaining = length - position;
132-
int bytestomap = (int) Math.min(MAP_SIZE, remaining);
132+
long bytestomap = (long) Math.min(MAP_SIZE, remaining);
133133
MappedByteBuffer mbBuffer = fileChannel.map(MapMode.READ_ONLY, position, bytestomap);
134134

135-
int limit = mbBuffer.limit();
136-
int lastSpace = -1;
137-
int firstChar = -1;
135+
long limit = mbBuffer.limit();
136+
long lastSpace = -1;
137+
long firstChar = -1;
138138
while (mbBuffer.hasRemaining()) {
139139

140140
boolean isFirstChar = false;

0 commit comments

Comments
 (0)
0