Friday, April 26, 2013

[android help] Android: Can I use single InputStream for multi method?


I have some problem about using readline(). I have single inputStream it's from main class



private String url = "BoardLayoutSet/01_basic.templete";
private InputStream boardName = getResources().getAssets().open(url);
Board b = new Board(boardName);


And I send this "boardName" is InputStream to "Board" class. In this Board.class has constructor like this code below



public Board(InputStream boardName) throws IOException{
int[] d = LayoutDimensions(boardName); //<----First,use InputStream
....
......
build(null, boardName); //<<--Second,Use InputStream
}


First method is called by this constructor(Above). First method it use "InputStream" like this(Below)



public int[] LayoutDimensions(InputStream boardName) throws IOException {
BufferedReader reader1 = new BufferedReader(new InputStreamReader(boardName));
L1 = reader1.readLine(); // #Basic
L1 = reader1.readLine(); // %Level 1
L1 = reader1.readLine(); // space line
L1 = reader1.readLine(); // First row of board
for (int i = 0; i < L1.length(); i++) {
if (L1.charAt(i) != ' ') {
x++;
}
}
System.out.println(L1);
System.out.println("Width(x):" + x);
// ////////////////
boardName.reset();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(boardName));
L2 = reader2.readLine(); // #Basic
L2 = reader2.readLine(); // %Level 1
L2 = reader2.readLine(); // space line
L2 = reader2.readLine(); // First row of board

while (L2.length()!=0) {
System.out.println(L2.charAt(0));
y++;
L2 = reader2.readLine();
}
System.out.println("Height(y):" + y);
.....
......

reader1.close();
reader2.close();
reader3.close();

return dimensions;

}


When I use one method only .it can work. Next, I need to call Second method like this



protected void build(Random r1, InputStream boardName) throws IOException {



if (r1==null) {
long seed = new Random().nextLong();
r = new Random(seed);
} else {
r = r1;
}


tTile = new Tile[depth][height][width];
int x;
int y;
int z=-1;

BufferedReader buffer = new BufferedReader(new InputStreamReader(boardName));

String L=null;
buffer.reset();
L = buffer.readLine(); //Basic
.......
......
......


it's error since this line>> L=buffer.readLine(); I tried to swap order : use second method before fist method . the second can use but first method is error. I think it's problem about Inputstream so I tried to make two Inputstream but it's not better.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...