- final byte[] b = InputStreamToByteArray.convert(r.getInputStream());
- Scheduler.add(new Scheduler.Task() { public void perform() {
- try {
- Picture ret = null;
- InputStream pbis = new ByteArrayInputStream(b);
- if ((b[0] & 0xff) == 'G') ret = gif.fromInputStream(pbis, r.getDescriptiveName());
- else if ((b[0] & 0xff) == 137) ret = new PNG().fromInputStream(pbis, r.getDescriptiveName());
- else if ((b[0] & 0xff) == 0xff) ret = Platform.decodeJPEG(pbis, r.getDescriptiveName());
- else throw new JS.Exn("couldn't figure out image type from first byte");
- ret.res = r;
- cache.put(r, ret);
- callback.call(ret);
- } catch (Exception e) {
- Log.log(Picture.class, e);
- } } });
- } catch (IOException e) {
- Log.log(Picture.class, e);
- return;
+ PushbackInputStream pbis = new PushbackInputStream(in);
+ int firstByte = pbis.read();
+ if (firstByte == -1) throw new JSExn("empty stream reading image");
+ pbis.unread(firstByte);
+ if ((firstByte & 0xff) == 'G') GIF.load(pbis, p);
+ else if ((firstByte & 0xff) == 137) PNG.load(pbis, p);
+ else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, p);
+ else throw new JSExn("couldn't figure out image type from first byte");
+ p.loaded();
+ Scheduler.add(callback);
+ } catch (Exception e) {
+ Log.info(this, "exception while loading image");
+ Log.info(this, e);