Java Code Examples for javax.imageio.ImageWriter#getOriginatingProvider()

The following examples show how to use javax.imageio.ImageWriter#getOriginatingProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GifTransparencyTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void doTest() {
    File pwd = new File(".");
    try {
        File f = File.createTempFile("transparency_test_", ".gif", pwd);
        System.out.println("file: " + f.getCanonicalPath());

        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();

        ImageWriterSpi spi = w.getOriginatingProvider();

        boolean succeed_write = ImageIO.write(src, "gif", f);

        if (!succeed_write) {
            throw new RuntimeException("Test failed: failed to write src.");
        }

        dst = ImageIO.read(f);

        checkResult(src, dst);

    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
Example 2
Source File: GifTransparencyTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void doTest() {
    File pwd = new File(".");
    try {
        File f = File.createTempFile("transparency_test_", ".gif", pwd);
        System.out.println("file: " + f.getCanonicalPath());

        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();

        ImageWriterSpi spi = w.getOriginatingProvider();

        boolean succeed_write = ImageIO.write(src, "gif", f);

        if (!succeed_write) {
            throw new RuntimeException("Test failed: failed to write src.");
        }

        dst = ImageIO.read(f);

        checkResult(src, dst);

    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
Example 3
Source File: GifTransparencyTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void doTest() {
    File pwd = new File(".");
    try {
        File f = File.createTempFile("transparency_test_", ".gif", pwd);
        System.out.println("file: " + f.getCanonicalPath());

        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();

        ImageWriterSpi spi = w.getOriginatingProvider();

        boolean succeed_write = ImageIO.write(src, "gif", f);

        if (!succeed_write) {
            throw new RuntimeException("Test failed: failed to write src.");
        }

        dst = ImageIO.read(f);

        checkResult(src, dst);

    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
Example 4
Source File: ImageIOGreyScale.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an <code>Iterator</code> containing all currently registered <code>ImageTranscoder</code>s that
 * claim to be able to transcode between the metadata of the given <code>ImageReader</code> and
 * <code>ImageWriter</code>.
 * 
 * @param reader
 *            an <code>ImageReader</code>.
 * @param writer
 *            an <code>ImageWriter</code>.
 * 
 * @return an <code>Iterator</code> containing <code>ImageTranscoder</code>s.
 * 
 * @exception IllegalArgumentException
 *                if <code>reader</code> or <code>writer</code> is <code>null</code>.
 */
public static Iterator<ImageTranscoder> getImageTranscoders(ImageReader reader, ImageWriter writer) {
	if (reader == null) {
		throw new IllegalArgumentException("reader == null!");
	}
	if (writer == null) {
		throw new IllegalArgumentException("writer == null!");
	}
	ImageReaderSpi readerSpi = reader.getOriginatingProvider();
	ImageWriterSpi writerSpi = writer.getOriginatingProvider();
	ServiceRegistry.Filter filter = new TranscoderFilter(readerSpi, writerSpi);

	Iterator iter;
	// Ensure category is present
	try {
		iter = theRegistry.getServiceProviders(ImageTranscoderSpi.class, filter, true);
	} catch (IllegalArgumentException e) {
		return new HashSet().iterator();
	}
	return new ImageTranscoderIterator(iter);
}
 
Example 5
Source File: ImageUtil.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 6
Source File: ImageUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided {@code ImageWriter} can encode
 * the provided {@code ImageTypeSpecifier} or not.  If not, an
 * {@code IIOException} will be thrown.
 * @param writer The provided {@code ImageWriter}.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 7
Source File: ImageUtil.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** Checks that the provided {@code ImageWriter} can encode
 * the provided {@code ImageTypeSpecifier} or not.  If not, an
 * {@code IIOException} will be thrown.
 * @param writer The provided {@code ImageWriter}.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 8
Source File: ImageUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 9
Source File: ImageUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 10
Source File: ImageUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 11
Source File: ImageUtil.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 12
Source File: ImageUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 13
Source File: ImageUtil.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 14
Source File: XDataTransferer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 15
Source File: XDataTransferer.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 16
Source File: XDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 17
Source File: XDataTransferer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 18
Source File: XDataTransferer.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 19
Source File: XDataTransferer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 20
Source File: XDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}