Skip to content

Commit dddc74b

Browse files
committed
Fix page size retrieval in Main.java
1 parent bb14d5c commit dddc74b

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

compiled_starters/java/src/main/java/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public static void main(String[] args){
1919
try {
2020
byte[] header = Files.readAllBytes(Path.of(databaseFilePath));
2121

22-
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order
23-
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort();
22+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order.
23+
// '& 0xFFFF' is used to convert the signed short to an unsigned int.
24+
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF;
2425

2526
// You can use print statements as follows for debugging, they'll be visible when running tests.
2627
System.out.println("Logs from your program will appear here!");

solutions/java/01-init/code/src/main/java/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public static void main(String[] args){
1919
try {
2020
byte[] header = Files.readAllBytes(Path.of(databaseFilePath));
2121

22-
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order
23-
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort();
22+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order.
23+
// '& 0xFFFF' is used to convert the signed short to an unsigned int.
24+
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF;
2425

2526
System.out.println("database page size: " + pageSize);
2627
} catch (IOException e) {

solutions/java/01-init/diff/src/main/java/Main.java.diff

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@@ -1,37 +1,33 @@
1+
@@ -1,38 +1,34 @@
22
import java.io.IOException;
33
import java.nio.ByteBuffer;
44
import java.nio.ByteOrder;
@@ -20,8 +20,9 @@
2020
try {
2121
byte[] header = Files.readAllBytes(Path.of(databaseFilePath));
2222

23-
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order
24-
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort();
23+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order.
24+
// '& 0xFFFF' is used to convert the signed short to an unsigned int.
25+
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF;
2526

2627
- // You can use print statements as follows for debugging, they'll be visible when running tests.
2728
- System.out.println("Logs from your program will appear here!");

starter_templates/java/src/main/java/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public static void main(String[] args){
1919
try {
2020
byte[] header = Files.readAllBytes(Path.of(databaseFilePath));
2121

22-
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order
23-
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort();
22+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order.
23+
// '& 0xFFFF' is used to convert the signed short to an unsigned int.
24+
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF;
2425

2526
// You can use print statements as follows for debugging, they'll be visible when running tests.
2627
System.out.println("Logs from your program will appear here!");

0 commit comments

Comments
 (0)