- final byte[] b = InputStreamToByteArray.convert(r.getInputStream());
- Scheduler.add(new Scheduler.Task() { public void perform() {
- try {
- Picture p = null;
- InputStream pbis = new ByteArrayInputStream(b);
- if ((b[0] & 0xff) == 'G') p = gif.fromInputStream(pbis, "some picture");
- else if ((b[0] & 0xff) == 137) p = new PNG().fromInputStream(pbis, "some picture");
- else if ((b[0] & 0xff) == 0xff) p = Platform.decodeJPEG(pbis, "some picture");
- else throw new JSExn("couldn't figure out image type from first byte");
- p.res = r;
- holder.picture = p;
- Scheduler.add(callback);
- } catch (Exception e) {
- Log.log(Picture.class, e);
- } } });
- } catch (IOException e) {
- Log.log(Picture.class, e);
- return;
+ in = b == null ? Stream.getInputStream(stream) : b.getImage();
+ } catch (IOException e) { Log.error(Picture.class, stream);
+ } catch (JSExn e) { Log.error(Picture.class, stream);
+ }
+ if (in == null) { Log.warn(Picture.class, "couldn't load image for stream " + stream); return; }
+ try {
+ 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);