Java Reference
In-Depth Information
Listing 12-8. Using a ContentConnection to Download an Image
public void run() {
ContentConnection cc = null;
DataInputStream in = null;
try {
String url = "http://www.noplace.com/image.png";
cc = (ContentConnection)Connector.open(url);
in = new DataInputStream(cc.openInputStream());
int length = (int)cc.getLength();
byte[] data = null;
if (length != -1) {
data = new byte[length];
in.readFully(data);
}
else {
int chunkSize = 512;
int index = 0;
int readLength = 0;
data = new byte[chunkSize];
do {
if (data.length < index + chunkSize) {
byte[] newData = new byte[index + chunkSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
readLength = in.read(data, index, chunkSize);
index += readLength;
} while (readLength == chunkSize);
length = index;
}
Image image = Image.createImage(data, 0, length);
}
catch (Exception e) {...}
finally {
try {
if (in != null) in.close();
}
catch (IOException ioe) {}
}
}
 
Search WWH ::




Custom Search