throws IOException { wrapped.mark(array.length); // BufferedReader supports mark int count = 0; if ((char)c == array[0]) { for (count = 1; count < array.length; count++) { c = wrapped.read(); if (c == -1 || ((char)c) != array[count]) { break; } } } if (count == array.length) { return true; } else { wrapped.reset(); return false; } } } /** * Decodes a byte array into a set of String filenames. */ protected abstract String[] dragQueryFile(byte[] bytes); /** * Decodes URIs from either a byte array or a stream. */ protected URI[] dragQueryURIs(InputStream stream, byte[] bytes, long format, Transferable localeTransferable) throws IOException { throw new IOException( new UnsupportedOperationException("not implemented on this platform")); } /** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ protected abstract Image platformImageBytesOrStreamToImage(InputStream str, byte[] bytes, long format) throws IOException; /** * Translates either a byte array or an input stream which contain * an image data in the given standard format into an Image. * * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif */ protected Image standardImageBytesOrStreamToImage(InputStream inputStream, byte[] bytes, String mimeType) throws IOException { if (inputStream == null) { inputStream = new ByteArrayInputStream(bytes); } Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType); if (!readerIterator.hasNext()) { throw new IOException("No registered service provider can decode " + " an image from " + mimeType); } IOException ioe = null; while (readerIterator.hasNext()) { ImageReader imageReader = (ImageReader)readerIterator.next(); try { ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream); try { ImageReadParam param = imageReader.getDefaultReadParam(); imageReader.setInput(imageInputStream, true, true); BufferedImage bufferedImage = imageReader.read(imageReader.getMinIndex(), param); if (bufferedImage != null) { return bufferedImage; } } finally { imageInputStream.close(); imageReader.dispose(); } } catch (IOException e) { ioe = e; continue; } } if (ioe == null) { ioe = new IOException("Registered service providers failed to decode" + " an image from " + mimeType); } throw ioe; } /** * Translates a Java Image into a byte array which contains platform- * specific image data in the given format. */ protected abstract byte[] imageToPlatformBytes(Image image, long format) throws IOException; /** * Translates a Java Image into a byte array which contains * an image data in the given standard format. * * @param mimeType image MIME type, such as: image/png, image/jpeg */ protected byte[] imageToStandardBytes(Image image, String mimeType) throws IOException { IOException originalIOE = null; Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); if (!writerIterator.hasNext()) { throw new IOException("No registered service provider can encode " + " an image to " + mimeType); } if (image instanceof RenderedImage) { // Try to encode the original image. try { return imageToStandardBytesImpl((RenderedImage)image, mimeType); } catch (IOException ioe) { originalIOE = ioe; } } // Retry with a BufferedImage. int width = 0; int height = 0; if (image instanceof ToolkitImage) { ImageRepresentation ir = ((ToolkitImage)image).getImageRep(); ir.reconstruct(ImageObserver.ALLBITS); width = ir.getWidth(); height = ir.getHeight(); } else { width = image.getWidth(null); height = image.getHeight(null); } ColorModel model = ColorModel.getRGBdefault(); WritableRaster raster = model.createCompatibleWritableRaster(width, height); BufferedImage bufferedImage = new BufferedImage(model, raster, model.isAlphaPremultiplied(), null); Graphics g = bufferedImage.getGraphics(); try { g.drawImage(image, 0, 0, width, height, null); } finally { g.dispose(); } try { return imageToStandardBytesImpl(bufferedImage, mimeType); } catch (IOException ioe) { if (originalIOE != null) { throw originalIOE; } else { throw ioe; } } } protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage, String mimeType) throws IOException { Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); ImageTypeSpecifier typeSpecifier = new ImageTypeSpecifier(renderedImage); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOException ioe = null; while (writerIterator.hasNext()) { ImageWriter imageWriter = (ImageWriter)writerIterator.next(); ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider(); if (!writerSpi.canEncodeImage(typeSpecifier)) { continue; } try { ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos); try { imageWriter.setOutput(imageOutputStream); imageWriter.write(renderedImage); imageOutputStream.flush(); } finally { imageOutputStream.close(); } } catch (IOException e) { imageWriter.dispose(); baos.reset(); ioe = e; continue; } imageWriter.dispose(); baos.close(); return baos.toByteArray(); } baos.close(); if (ioe == null) { ioe = new IOException("Registered service providers failed to encode " + renderedImage + " to " + mimeType); } throw ioe; } /** * Concatenates the data represented by two objects. Objects can be either * byte arrays or instances of <code>InputStream</code>. If both arguments * are byte arrays byte array will be returned. Otherwise an * <code>InputStream</code> will be returned. * <p> * Currently is only called from native code to prepend palette data to * platform-specific image data during image transfer on Win32. * * @param obj1 the first object to be concatenated. * @param obj2 the second object to be concatenated. * @return a byte array or an <code>InputStream</code> which represents * a logical concatenation of the two arguments. * @throws NullPointerException is either of the arguments is * <code>null</code> * @throws ClassCastException is either of the arguments is * neither byte array nor an instance of <code>InputStream</code>. */ private Object concatData(Object obj1, Object obj2) { InputStream str1 = null; InputStream str2 = null; if (obj1 instanceof byte[]) { byte[] arr1 = (byte[])obj1; if (obj2 instanceof byte[]) { byte[] arr2 = (byte[])obj2; byte[] ret = new byte[arr1.length + arr2.length]; System.arraycopy(arr1, 0, ret, 0, arr1.length); System.arraycopy(arr2, 0, ret, arr1.length, arr2.length); return ret; } else { str1 = new ByteArrayInputStream(arr1); str2 = (InputStream)obj2; } } else { str1 = (InputStream)obj1; if (obj2 instanceof byte[]) { str2 = new ByteArrayInputStream((byte[])obj2); } else { str2 = (InputStream)obj2; } } return new SequenceInputStream(str1, str2); } public byte[] convertData(final Object source, final Transferable contents, final long format, final Map formatMap, final boolean isToolkitThread) throws IOException { byte[] ret = null; /* * If the current thread is the Toolkit thread we should post a * Runnable to the event dispatch thread associated with source Object, * since translateTransferable() calls Transferable.getTransferData() * that may contain client code. */ if (isToolkitThread) try { final Stack stack = new Stack(); final Runnable dataConverter = new Runnable() { // Guard against multiple executions. private boolean done = false; public void run() { if (done) { return; } byte[] data = null; try { DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format)); if (flavor != null) { data = translateTransferable(contents, flavor, format); } } catch (Exception e) { e.printStackTrace(); data = null; } try { getToolkitThreadBlockedHandler().lock(); stack.push(data); getToolkitThreadBlockedHandler().exit(); } finally { getToolkitThreadBlockedHandler().unlock(); done = true; } } }; final AppContext appContext = SunToolkit.targetToAppContext(source); getToolkitThreadBlockedHandler().lock(); if (appContext != null) { appContext.put(DATA_CONVERTER_KEY, dataConverter); } SunToolkit.executeOnEventHandlerThread(source, dataConverter); while (stack.empty()) { getToolkitThreadBlockedHandler().enter(); } if (appContext != null) { appContext.remove(DATA_CONVERTER_KEY); } ret = (byte[])stack.pop(); } finally { getToolkitThreadBlockedHandler().unlock(); } else { DataFlavor flavor = (DataFlavor) formatMap.get(Long.valueOf(format)); if (flavor != null) { ret = translateTransferable(contents, flavor, format); } } return ret; } public void processDataConversionRequests() { if (EventQueue.isDispatchThread()) { AppContext appContext = AppContext.getAppContext(); getToolkitThreadBlockedHandler().lock(); try { Runnable dataConverter = (Runnable)appContext.get(DATA_CONVERTER_KEY); if (dataConverter != null) { dataConverter.run(); appContext.remove(DATA_CONVERTER_KEY); } } finally { getToolkitThreadBlockedHandler().unlock(); } } } public abstract ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler(); /** * Helper function to reduce a Map with Long keys to a long array. * <p> * The map keys are sorted according to the native formats preference * order. */ public static long[] keysToLongArray(SortedMap map) { Set keySet = map.keySet(); long[] retval = new long[keySet.size()]; int i = 0; for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) { retval[i] = ((Long)iter.next()).longValue(); } return retval; } /** * Helper function to reduce a Map with DataFlavor keys to a DataFlavor * array. The array will be sorted according to * <code>DataFlavorComparator</code>. */ public static DataFlavor[] keysToDataFlavorArray(Map map) { return setToSortedDataFlavorArray(map.keySet(), map); } /** * Helper function to convert a Set of DataFlavors to a sorted array. * The array will be sorted according to <code>DataFlavorComparator</code>. */ public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) { DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; flavorsSet.toArray(flavors); final Comparator comparator = new DataFlavorComparator(IndexedComparator.SELECT_WORST); Arrays.sort(flavors, comparator); return flavors; } /** * Helper function to convert a Set of DataFlavors to a sorted array. * The array will be sorted according to a * <code>DataFlavorComparator</code> created with the specified * flavor-to-native map as an argument. */ public static DataFlavor[] setToSortedDataFlavorArray (Set flavorsSet, Map flavorToNativeMap) { DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; flavorsSet.toArray(flavors); Comparator comparator = new DataFlavorComparator(flavorToNativeMap, IndexedComparator.SELECT_WORST); Arrays.sort(flavors, comparator); return flavors; } /** * Helper function to convert an InputStream to a byte[] array. */ protected static byte[] inputStreamToByteArray(InputStream str) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buf = new byte[8192]; while ((len = str.read(buf)) != -1) { baos.write(buf, 0, len); } return baos.toByteArray(); } /** * Returns platform-specific mappings for the specified native. * If there are no platform-specific mappings for this native, the method * returns an empty <code>List</code>. */ public List getPlatformMappingsForNative(String nat) { return new ArrayList(); } /** * Returns platform-specific mappings for the specified flavor. * If there are no platform-specific mappings for this flavor, the method * returns an empty <code>List</code>. */ public List getPlatformMappingsForFlavor(DataFlavor df) { return new ArrayList(); } /** * A Comparator which includes a helper function for comparing two Objects * which are likely to be keys in the specified Map. */ public abstract static class IndexedComparator implements Comparator { /** * The best Object (e.g., DataFlavor) will be the last in sequence. */ public static final boolean SELECT_BEST = true; /** * The best Object (e.g., DataFlavor) will be the first in sequence. */ public static final boolean SELECT_WORST = false; protected final boolean order; public IndexedComparator() { this(SELECT_BEST); } public IndexedComparator(boolean order) { this.order = order; } /** * Helper method to compare two objects by their Integer indices in the * given map. If the map doesn't contain an entry for either of the * objects, the fallback index will be used for the object instead. * * @param indexMap the map which maps objects into Integer indexes. * @param obj1 the first object to be compared. * @param obj2 the second object to be compared. * @param fallbackIndex the Integer to be used as a fallback index. * @return a negative integer, zero, or a positive integer as the * first object is mapped to a less, equal to, or greaterLines 2001–2500 of 3090. Previous lines Load more
throws IOException { wrapped.mark(array.length); // BufferedReader supports mark int count = 0; if ((char)c == array[0]) { for (count = 1; count < array.length; count++) { c = wrapped.read(); if (c == -1 || ((char)c) != array[count]) { break; } } } if (count == array.length) { return true; } else { wrapped.reset(); return false; } } } /** * Decodes a byte array into a set of String filenames. */ protected abstract String[] dragQueryFile(byte[] bytes); /** * Decodes URIs from either a byte array or a stream. */ protected URI[] dragQueryURIs(InputStream stream, byte[] bytes, long format, Transferable localeTransferable) throws IOException { throw new IOException( new UnsupportedOperationException("not implemented on this platform")); } /** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ protected abstract Image platformImageBytesOrStreamToImage(InputStream str, byte[] bytes, long format) throws IOException; /** * Translates either a byte array or an input stream which contain * an image data in the given standard format into an Image. * * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif */ protected Image standardImageBytesOrStreamToImage(InputStream inputStream, byte[] bytes, String mimeType) throws IOException { if (inputStream == null) { inputStream = new ByteArrayInputStream(bytes); } Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType); if (!readerIterator.hasNext()) { throw new IOException("No registered service provider can decode " + " an image from " + mimeType); } IOException ioe = null; while (readerIterator.hasNext()) { ImageReader imageReader = (ImageReader)readerIterator.next(); try { ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream); try { ImageReadParam param = imageReader.getDefaultReadParam(); imageReader.setInput(imageInputStream, true, true); BufferedImage bufferedImage = imageReader.read(imageReader.getMinIndex(), param); if (bufferedImage != null) { return bufferedImage; } } finally { imageInputStream.close(); imageReader.dispose(); } } catch (IOException e) { ioe = e; continue; } } if (ioe == null) { ioe = new IOException("Registered service providers failed to decode" + " an image from " + mimeType); } throw ioe; } /** * Translates a Java Image into a byte array which contains platform- * specific image data in the given format. */ protected abstract byte[] imageToPlatformBytes(Image image, long format) throws IOException; /** * Translates a Java Image into a byte array which contains * an image data in the given standard format. * * @param mimeType image MIME type, such as: image/png, image/jpeg */ protected byte[] imageToStandardBytes(Image image, String mimeType) throws IOException { IOException originalIOE = null; Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); if (!writerIterator.hasNext()) { throw new IOException("No registered service provider can encode " + " an image to " + mimeType); } if (image instanceof RenderedImage) { // Try to encode the original image. try { return imageToStandardBytesImpl((RenderedImage)image, mimeType); } catch (IOException ioe) { originalIOE = ioe; } } // Retry with a BufferedImage. int width = 0; int height = 0; if (image instanceof ToolkitImage) { ImageRepresentation ir = ((ToolkitImage)image).getImageRep(); ir.reconstruct(ImageObserver.ALLBITS); width = ir.getWidth(); height = ir.getHeight(); } else { width = image.getWidth(null); height = image.getHeight(null); } ColorModel model = ColorModel.getRGBdefault(); WritableRaster raster = model.createCompatibleWritableRaster(width, height); BufferedImage bufferedImage = new BufferedImage(model, raster, model.isAlphaPremultiplied(), null); Graphics g = bufferedImage.getGraphics(); try { g.drawImage(image, 0, 0, width, height, null); } finally { g.dispose(); } try { return imageToStandardBytesImpl(bufferedImage, mimeType); } catch (IOException ioe) { if (originalIOE != null) { throw originalIOE; } else { throw ioe; } } } protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage, String mimeType) throws IOException { Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); ImageTypeSpecifier typeSpecifier = new ImageTypeSpecifier(renderedImage); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOException ioe = null; while (writerIterator.hasNext()) { ImageWriter imageWriter = (ImageWriter)writerIterator.next(); ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider(); if (!writerSpi.canEncodeImage(typeSpecifier)) { continue; } try { ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos); try { imageWriter.setOutput(imageOutputStream); imageWriter.write(renderedImage); imageOutputStream.flush(); } finally { imageOutputStream.close(); } } catch (IOException e) { imageWriter.dispose(); baos.reset(); ioe = e; continue; } imageWriter.dispose(); baos.close(); return baos.toByteArray(); } baos.close(); if (ioe == null) { ioe = new IOException("Registered service providers failed to encode " + renderedImage + " to " + mimeType); } throw ioe; } /** * Concatenates the data represented by two objects. Objects can be either * byte arrays or instances of <code>InputStream</code>. If both arguments * are byte arrays byte array will be returned. Otherwise an * <code>InputStream</code> will be returned. * <p> * Currently is only called from native code to prepend palette data to * platform-specific image data during image transfer on Win32. * * @param obj1 the first object to be concatenated. * @param obj2 the second object to be concatenated. * @return a byte array or an <code>InputStream</code> which represents * a logical concatenation of the two arguments. * @throws NullPointerException is either of the arguments is * <code>null</code> * @throws ClassCastException is either of the arguments is * neither byte array nor an instance of <code>InputStream</code>. */ private Object concatData(Object obj1, Object obj2) { InputStream str1 = null; InputStream str2 = null; if (obj1 instanceof byte[]) { byte[] arr1 = (byte[])obj1; if (obj2 instanceof byte[]) { byte[] arr2 = (byte[])obj2; byte[] ret = new byte[arr1.length + arr2.length]; System.arraycopy(arr1, 0, ret, 0, arr1.length); System.arraycopy(arr2, 0, ret, arr1.length, arr2.length); return ret; } else { str1 = new ByteArrayInputStream(arr1); str2 = (InputStream)obj2; } } else { str1 = (InputStream)obj1; if (obj2 instanceof byte[]) { str2 = new ByteArrayInputStream((byte[])obj2); } else { str2 = (InputStream)obj2; } } return new SequenceInputStream(str1, str2); } public byte[] convertData(final Object source, final Transferable contents, final long format, final Map formatMap, final boolean isToolkitThread) throws IOException { byte[] ret = null; /* * If the current thread is the Toolkit thread we should post a * Runnable to the event dispatch thread associated with source Object, * since translateTransferable() calls Transferable.getTransferData() * that may contain client code. */ if (isToolkitThread) try { final Stack stack = new Stack(); final Runnable dataConverter = new Runnable() { // Guard against multiple executions. private boolean done = false; public void run() { if (done) { return; } byte[] data = null; try { DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format)); if (flavor != null) { data = translateTransferable(contents, flavor, format); } } catch (Exception e) { e.printStackTrace(); data = null; } try { getToolkitThreadBlockedHandler().lock(); stack.push(data); getToolkitThreadBlockedHandler().exit(); } finally { getToolkitThreadBlockedHandler().unlock(); done = true; } } }; final AppContext appContext = SunToolkit.targetToAppContext(source); getToolkitThreadBlockedHandler().lock(); if (appContext != null) { appContext.put(DATA_CONVERTER_KEY, dataConverter); } SunToolkit.executeOnEventHandlerThread(source, dataConverter); while (stack.empty()) { getToolkitThreadBlockedHandler().enter(); } if (appContext != null) { appContext.remove(DATA_CONVERTER_KEY); } ret = (byte[])stack.pop(); } finally { getToolkitThreadBlockedHandler().unlock(); } else { DataFlavor flavor = (DataFlavor) formatMap.get(Long.valueOf(format)); if (flavor != null) { ret = translateTransferable(contents, flavor, format); } } return ret; } public void processDataConversionRequests() { if (EventQueue.isDispatchThread()) { AppContext appContext = AppContext.getAppContext(); getToolkitThreadBlockedHandler().lock(); try { Runnable dataConverter = (Runnable)appContext.get(DATA_CONVERTER_KEY); if (dataConverter != null) { dataConverter.run(); appContext.remove(DATA_CONVERTER_KEY); } } finally { getToolkitThreadBlockedHandler().unlock(); } } } public abstract ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler(); /** * Helper function to reduce a Map with Long keys to a long array. * <p> * The map keys are sorted according to the native formats preference * order. */ public static long[] keysToLongArray(SortedMap map) { Set keySet = map.keySet(); long[] retval = new long[keySet.size()]; int i = 0; for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) { retval[i] = ((Long)iter.next()).longValue(); } return retval; } /** * Helper function to reduce a Map with DataFlavor keys to a DataFlavor * array. The array will be sorted according to * <code>DataFlavorComparator</code>. */ public static DataFlavor[] keysToDataFlavorArray(Map map) { return setToSortedDataFlavorArray(map.keySet(), map); } /** * Helper function to convert a Set of DataFlavors to a sorted array. * The array will be sorted according to <code>DataFlavorComparator</code>. */ public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) { DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; flavorsSet.toArray(flavors); final Comparator comparator = new DataFlavorComparator(IndexedComparator.SELECT_WORST); Arrays.sort(flavors, comparator); return flavors; } /** * Helper function to convert a Set of DataFlavors to a sorted array. * The array will be sorted according to a * <code>DataFlavorComparator</code> created with the specified * flavor-to-native map as an argument. */ public static DataFlavor[] setToSortedDataFlavorArray (Set flavorsSet, Map flavorToNativeMap) { DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; flavorsSet.toArray(flavors); Comparator comparator = new DataFlavorComparator(flavorToNativeMap, IndexedComparator.SELECT_WORST); Arrays.sort(flavors, comparator); return flavors; } /** * Helper function to convert an InputStream to a byte[] array. */ protected static byte[] inputStreamToByteArray(InputStream str) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buf = new byte[8192]; while ((len = str.read(buf)) != -1) { baos.write(buf, 0, len); } return baos.toByteArray(); } /** * Returns platform-specific mappings for the specified native. * If there are no platform-specific mappings for this native, the method * returns an empty <code>List</code>. */ public List getPlatformMappingsForNative(String nat) { return new ArrayList(); } /** * Returns platform-specific mappings for the specified flavor. * If there are no platform-specific mappings for this flavor, the method * returns an empty <code>List</code>. */ public List getPlatformMappingsForFlavor(DataFlavor df) { return new ArrayList(); } /** * A Comparator which includes a helper function for comparing two Objects * which are likely to be keys in the specified Map. */ public abstract static class IndexedComparator implements Comparator { /** * The best Object (e.g., DataFlavor) will be the last in sequence. */ public static final boolean SELECT_BEST = true; /** * The best Object (e.g., DataFlavor) will be the first in sequence. */ public static final boolean SELECT_WORST = false; protected final boolean order; public IndexedComparator() { this(SELECT_BEST); } public IndexedComparator(boolean order) { this.order = order; } /** * Helper method to compare two objects by their Integer indices in the * given map. If the map doesn't contain an entry for either of the * objects, the fallback index will be used for the object instead. * * @param indexMap the map which maps objects into Integer indexes. * @param obj1 the first object to be compared. * @param obj2 the second object to be compared. * @param fallbackIndex the Integer to be used as a fallback index. * @return a negative integer, zero, or a positive integer as the * first object is mapped to a less, equal to, or greater