www.pudn.com > GMapViewer-src.zip > MapTile.java
package org.sreid.j2me.gmapviewer;
import javax.microedition.lcdui.Image;
import java.io.*;
class MapTile {
static final int WIDTH = 128;
static final int HEIGHT = 128;
static final byte WITH_BYTES = 101, WITH_RMSID = 102, WITH_MINIMUM = 0;
private static final byte SERIALIZED_VERSION = 1;
final XYZ xyz;
Image image;
byte[] bytes;
int rmsID = -1; // ID in RMS-based cache, or -1 if invalid.
int rmsBytesUsed; // only valid if rmsID != -1
boolean downloading = false;
boolean decoding = false;
MapTile(XYZ xyz) {
this.xyz = xyz;
}
/** Constructs from previously saved data. Only sets fields: xyz, and either bytes or rmsID */
MapTile(DataInput in, byte withBytesOrRmsID) throws IOException {
byte v = in.readByte();
if (v != SERIALIZED_VERSION) throw new IOException("Bad version header in data");
int x = in.readInt();
int y = in.readInt();
int z = in.readByte();
xyz = new XYZ(x, y, z);
switch (withBytesOrRmsID) {
case WITH_BYTES:
int nBytes = in.readInt();
byte[] b = new byte[nBytes];
in.readFully(b);
bytes = b;
rmsBytesUsed = b.length;
break;
case WITH_RMSID:
rmsID = in.readInt();
rmsBytesUsed = in.readInt();
break;
case WITH_MINIMUM:
break;
default:
throw new IllegalArgumentException("Not a valid option: " + withBytesOrRmsID);
}
}
void writeTo(DataOutput out, byte withBytesOrRmsID) throws IOException {
out.writeByte(SERIALIZED_VERSION);
out.writeInt(xyz.x);
out.writeInt(xyz.y);
out.writeByte((byte)xyz.z);
switch (withBytesOrRmsID) {
case WITH_BYTES:
out.writeInt(bytes.length);
out.write(bytes);
break;
case WITH_RMSID:
out.writeInt(rmsID);
out.writeInt(rmsBytesUsed);
break;
default:
throw new IllegalArgumentException("Not a valid option: " + withBytesOrRmsID);
}
}
public String toString() {
return "MapTile["
+ xyz
+ "," + (bytes == null ? -1 : bytes.length)
+ "," + rmsID
+ "," + rmsBytesUsed
+ "," + (image == null ? "null" : "" + image.getWidth() + "x" + image.getHeight())
+ "," + downloading
+ "," + decoding
+ "," + System.identityHashCode(this)
+ "]";
}
}