changeset 7281:429b2e166839

RT-36240: Drag and Drop for complex datatypes fails with JavaFX embedded in Swing Reviewed-by: snorthov
author Anthony Petrov <anthony.petrov@oracle.com>
date Wed, 11 Jun 2014 13:00:56 +0400
parents cbc97e1d7330
children 5527e3f6b542
files modules/swing/src/main/java/javafx/embed/swing/DataFlavorUtils.java
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/swing/src/main/java/javafx/embed/swing/DataFlavorUtils.java	Wed Jun 11 11:16:21 2014 +0300
+++ b/modules/swing/src/main/java/javafx/embed/swing/DataFlavorUtils.java	Wed Jun 11 13:00:56 2014 +0400
@@ -45,6 +45,8 @@
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 
+import java.nio.ByteBuffer;
+
 
 final class DataFlavorUtils {
 
@@ -52,6 +54,31 @@
         return flavor.getPrimaryType() + "/" + flavor.getSubType();
     }
 
+    /**
+     * InputStream implementation backed by a ByteBuffer.
+     * It can handle byte buffers that are backed by arrays
+     * as well as operating system memory.
+     */
+    private static class ByteBufferInputStream extends InputStream {
+        private final ByteBuffer bb;
+
+        private ByteBufferInputStream(ByteBuffer bb) { this.bb = bb; }
+
+        @Override public int available() { return bb.remaining(); }
+
+        @Override public int read() throws IOException {
+            if (!bb.hasRemaining()) return -1;
+            return bb.get() & 0xFF; // Make sure the value is in [0..255]
+        }
+
+        @Override public int read(byte[] bytes, int off, int len) throws IOException {
+            if (!bb.hasRemaining()) return -1;
+            len = Math.min(len, bb.remaining());
+            bb.get(bytes, off, len);
+            return len;
+        }
+    }
+
     static Object adjustFxData(final DataFlavor flavor, final Object fxData)
             throws UnsupportedEncodingException
     {
@@ -67,6 +94,11 @@
                 // ...
             }
         }
+        if (fxData instanceof ByteBuffer) {
+            if (flavor.isRepresentationClassInputStream()) {
+                return new ByteBufferInputStream((ByteBuffer)fxData);
+            }
+        }
         return fxData;
     }