java - GZIPInputStream shows bad data -
java - GZIPInputStream shows bad data -
the problem in short whenever add together gzipinputstream , gzipoutputstream, info becomes corrupt. without them, works fine.
i have next code:
public boolean loadall() { filehandle file = gdx.files.external("tinyvoxel/world_comp.lvl"); if (!file.exists()) homecoming false; inputstream in = file.read(); seek { in = new gzipinputstream(in); int count = in.read(); errorhandler.log("count loading grids: " + count); int = 0; while(in.available() > 0) { int x = in.read(); int y = in.read(); int z = in.read(); grid grid = creategrid(x, y, z); grid.loadgrid(in); it++; if (it % 10 == 0) { system.out.println("loading: " + ((float)it * 100f / (float)count)); } } in.close(); } grab (ioexception ex) { errorhandler.log("failure saving!"); } homecoming true; } if remove gzipinputstream , not compress original info gzipoutputstream (see below, remove gzipoutputstream), works fine.
public void saveall() { iterator<objectmap.entry<list<integer>, grid>> = terrainmap.entries().iterator(); int count = 0; outputstream out = gdx.files.external("tinyvoxel/world_comp.lvl").write(false); seek { out = new gzipoutputstream(out); out.write(terrainmap.size); errorhandler.log("count writing grids: " + terrainmap.size); while (it.hasnext()) { objectmap.entry<list<integer>, grid> entry = it.next(); list<integer> pos = entry.key; final grid grid = entry.value; grid.savegrid(out); if (count % 10 == 0) system.out.println("saving: " + ((float)count * 100f / (float) terrainmap.size)); count++; } out.close(); } grab (ioexception ex) { errorhandler.log("failure saving!"); } errorhandler.log("complete saving!"); } it notable, byte saving , loading in grid.loadgrid , grid.savegrid. may caused these operations, why work fine regular out , inputstreams?
editadditionally code functions of grid.loadgrid , grid.savegrid - parts matter @ least.
public boolean loadgrid(inputstream in) throws ioexception { int count = in.read(); palettestotal = 0; (int gx = 0; gx < config.grid_size_x; gx++) (int gy = 0; gy < config.grid_size_y; gy++) (int gz = 0; gz < config.grid_size_z; gz++) { grids[gx][gy][gz] = (byte)0xff; } (int = 0; < count; i++) { int gx = in.read(); int gy = in.read(); int gz = in.read(); grids[gx][gy][gz] = (byte) (palettestotal & 0xff); palettestotal++; byte[] buff = new byte[config.tiny_grid_size * 2]; tinygridcontainer tinygrid = bytepool.obtain(); (int ty = 0; ty < config.tiny_grid_size; ty++) (int tz = 0; tz < config.tiny_grid_size; tz++) { in.read(buff, 0, buff.length); (int tx = 0; tx < config.tiny_grid_size; tx++) { tinygrid.grid[tx][ty][tz] = (short) ((buff[tx * 2] >> 2) & 0xff + buff[tx * 2 + 1] & 0xff); } } tinygrids.put(position.create(gx, gy, gz), tinygrid); } .... } public void savegrid(outputstream out) throws ioexception { out.write(x); out.write(y); out.write(z); out.write(palettestotal); (int gx = 0; gx < config.grid_size_x; gx++) (int gy = 0; gy < config.grid_size_y; gy++) (int gz = 0; gz < config.grid_size_z; gz++) { if (grids[gx][gy][gz] != (byte) 0xff) { // add together tiny grid pixmap list<integer> pos = position.create(gx, gy, gz); tinygridcontainer tinygrid = tinygrids.get(pos); out.write(gx); out.write(gy); out.write(gz); position.free(pos); byte[] buff = new byte[config.tiny_grid_size * 2]; (int ty = 0; ty < config.tiny_grid_size; ty++) (int tz = 0; tz < config.tiny_grid_size; tz++) { (int tx = 0; tx < config.tiny_grid_size; tx++) { buff[tx*2] = (byte)((tinygrid.grid[tx][ty][tz] >> 2) & 0xff); buff[tx*2+1] = (byte)(tinygrid.grid[tx][ty][tz] & 0xff); } out.write(buff, 0, buff.length); } } } }
the comment mister smith got me in right direction.
byte[] buff = new byte[config.tiny_grid_size * 2]; tinygridcontainer tinygrid = bytepool.obtain(); (int ty = 0; ty < config.tiny_grid_size; ty++) (int tz = 0; tz < config.tiny_grid_size; tz++) { int cnt = 0; (int tx = 0; tx < config.tiny_grid_size; tx++) { int id = tx * 2; if (cnt <= id) { cnt += in.read(buff, cnt, buff.length - cnt); } tinygrid.grid[tx][ty][tz] = (short) ((buff[id] >> 2) & 0xff); id++; if (cnt <= id) { cnt += in.read(buff, cnt, buff.length - cnt); } tinygrid.grid[tx][ty][tz] += (short) (buff[id] & 0xff); } } java libgdx
Comments
Post a Comment