private static void loadBuffered(BufferedImage bufImage, SharedMemory.WithRaster mem) {
    final int width = mem.getWidth();
    final int height = mem.getHeight();
    final int rectsCount = mem.getDirtyRectsCount();

    ByteBuffer buffer = mem.wrapRaster();
    int[] dst = ((DataBufferInt)bufImage.getRaster().getDataBuffer()).getData();
    IntBuffer src = buffer.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();

    Rectangle[] dirtyRects = new Rectangle[]{new Rectangle(0, 0, width, height)};
    if (rectsCount > 0) {
      ByteBuffer rectsMem = mem.wrapRects();
      IntBuffer rects = rectsMem.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
      for (int c = 0; c < rectsCount; ++c) {
        int pos = c*4;
        Rectangle r = new Rectangle();
        r.x = rects.get(pos++);
        r.y = rects.get(pos++);
        r.width = rects.get(pos++);
        r.height = rects.get(pos);
        dirtyRects[c] = r;
      }
    }

    // flip image here
    // TODO: consider to optimize
    for (Rectangle rect : dirtyRects) {
      if (rect.width < width) {
        for (int line = rect.y; line < rect.y + rect.height; line++) {
          int offset = line*width + rect.x;
          src.position(offset).get(dst, offset, rect.width);
        }
      }
      else { // optimized for a buffer wide dirty rect
        src.position(rect.y*width).get(dst, rect.y*width, width*rect.height);
      }
    }

    // draw debug
    //            Graphics2D g = bufImage.createGraphics();
    //            g.setColor(Color.RED);
    //            for (Rectangle r : dirtyRects)
    //                g.drawRect(r.x, r.y, r.width, r.height);
    //            g.dispose();
  }
private static void loadBuffered(BufferedImage bufImage, SharedMemory.WithRaster mem)