如何读取DataInputStream直到结束而不需要捕获EOFException?

时间:2017-12-04 21:47:09

标签: java exception-handling datainputstream

假设我们有一些只包含整数的二进制数据byte[] data。如果我想使用DataInputStream读取这些数据,我能想出的唯一方法如下:

DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
try {
    while (true){
        int i = in.readInt();
    }
} catch (EOFException e) {
    // we're done!
} catch (IOException e){
    throw new RuntimeException(e);
}

关于这一点的问题是,到达流的末尾是预期的,只有当没有抛出异常时才会出现异常,IMO首先会破坏异常的目的。

使用Java NIO的IntBuffer时,没有这样的问题。

IntBuffer in = ByteBuffer.wrap(data).asIntBuffer();
while (in.hasRemaining()){
    int i = in.get();
}

来自C#并正在学习Java,我拒绝相信这是实现这一目标的预期方式。

此外,我只是遇到了Java NIO,这似乎是“非常新的”。在这里使用IntBuffer将是我拖延此事的方式。无论如何,我想知道如何在Java中正确完成。

2 个答案:

答案 0 :(得分:1)

你不能。 readInt()可以返回任何整数值,因此需要带外机制来表示流的结束,因此抛出异常。这就是API的设计方式。你无能为力。

答案 1 :(得分:1)

由于您来自.NET,因此Java DataInputStream大致相当于.NET的BinaryReader

就像它的.NET等价物readXYZ类及其主界面DataInput一样,没有规定确定任何给定类型的原语是否可用于在流的当前位置进行检索

通过查看设计人员自己对API的使用情况,您可以获得API的设计者希望您如何使用它的宝贵见解。

例如,查看用于对象反序列化的ObjectInputStream.java源代码。读取各种类型数组的代码在循环中调用DataInput的特定于类型的private Object readArray(boolean unshared) throws IOException { if (bin.readByte() != TC_ARRAY) { throw new InternalError(); } ObjectStreamClass desc = readClassDesc(false); int len = bin.readInt(); ... if (ccl == Integer.TYPE) { bin.readInts((int[]) array, 0, len); ... } ... } 方法。为了找出基元的结束位置,代码检索项目数(第1642行):

bin

上面,BlockDataInputStreamDataInput,这是len接口的另一个实现。请注意readInts如何将数组序列化副本存储的数组中的项目数传递给readIntlen#include <stdio.h> #include <string.h> #include <stdlib.h> int main (void){ //Function used to set up your game board int disp[4][4]; int i, j; int game_over = 0; for(i = 0; i < 4; i++) { for(j = 0;j < 4; j++) { printf("Enter a value from 1 - 15 for %d %d: ", i, j); scanf("%d", &disp[i][j]); } } //Function used to display the game board for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) printf(" _ "); else if (disp[i][j] < 10) printf(" %d ",disp[i][j]); else printf("%d ",disp[i][j]); } } // Function to show where cursor is for(i = 0; i < 4; i++){ printf("\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) printf(""); } } //Skeleton to function to controller do{ char move[10]; printf("Enter a move: "); scanf("%s", move); if (strcmp("up", move) == 0){ for(i = 0; i < 4; i++){ printf(""); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i >= 1){ int temp = disp[i][j]; disp[i][j] = disp[i - 1][j]; disp[i - 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("Up", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 1){ int temp = disp[i][j]; disp[i][j] = disp[i - 1][j]; disp[i - 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("UP", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 1){ int temp = disp[i][j]; disp[i][j] = disp[i - 1][j]; disp[i - 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("down", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i < 4){ int temp = disp[i][j]; disp[i][j] = disp[i + 1][j]; disp[i + 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("Down", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i < 4){ int temp = disp[i][j]; disp[i][j] = disp[i + 1][j]; disp[i + 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("DOWN", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i < 4){ int temp = disp[i][j]; disp[i][j] = disp[i + 1][j]; disp[i + 1][j] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("left", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (j >= 1){ int temp = disp[i][j]; disp[i][j] = disp[i][j - 1]; disp[i][j - 1] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("Left", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 1){ int temp = disp[i][j]; disp[i][j] = disp[i][j + 1]; disp[i][j + 1] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("LEFT", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 1){ int temp = disp[i][j]; disp[i][j] = disp[i][j + 1]; disp[i][j + 1] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("right", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (j <= 4){ int temp = disp[i][j]; disp[i][j] = disp[i][j + 1]; disp[i][j + 1] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("Right", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 4){ int temp = disp[i][j]; disp[i][j] = disp[i][j - 1]; disp[i][j - 1] = temp; } else { printf("Invalid move!"); } } } } else if (strcmp("RIGHT", move) == 0){ for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) if (i > 4){ int temp = disp[i][j]; disp[i][j] = disp[i][j - 1]; disp[i][j - 1] = temp; } else { printf("Invalid move!"); } } } } else printf("Invalid Move!"); //Update the board function for(i = 0; i < 4; i++){ printf("\n\n"); for(j = 0; j < 4; j++){ if (disp[i][j] == -1) printf(" _ "); else if (disp[i][j] < 10) printf(" %d ",disp[i][j]); else printf("%d ",disp[i][j]); } } } while (game_over == 0); return 0; } 次循环中调用{{1}}(第2918行)