jdk/src/macosx/native/sun/awt/CDataTransferer.m
changeset 23303 a88030d506db
parent 12047 320a714614e9
child 23328 4c53a6ebc779
equal deleted inserted replaced
23302:e48db1c03189 23303:a88030d506db
     1 /*
     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 #import "CDataTransferer.h"
    26 #import "CDataTransferer.h"
    27 #include "sun_lwawt_macosx_CDataTransferer.h"
    27 #include "sun_lwawt_macosx_CDataTransferer.h"
    28 
    28 
    29 #import <AppKit/AppKit.h>
    29 #import <AppKit/AppKit.h>
    30 #import <JavaNativeFoundation/JavaNativeFoundation.h>
    30 #import <JavaNativeFoundation/JavaNativeFoundation.h>
       
    31 #import "jni_util.h"
    31 
    32 
    32 #include "ThreadUtilities.h"
    33 #include "ThreadUtilities.h"
    33 
    34 
    34 
    35 
    35 // ***** NOTE ***** This dictionary corresponds to the static array predefinedClipboardNames
    36 // ***** NOTE ***** This dictionary corresponds to the static array predefinedClipboardNames
   170 
   171 
   171         (*env)->ReleasePrimitiveArrayCritical(env, inPixelData, rawImageData, JNI_ABORT);
   172         (*env)->ReleasePrimitiveArrayCritical(env, inPixelData, rawImageData, JNI_ABORT);
   172         NSData *tiffImage = [imageRep TIFFRepresentation];
   173         NSData *tiffImage = [imageRep TIFFRepresentation];
   173         jsize tiffSize = (jsize)[tiffImage length]; // #warning 64-bit: -length returns NSUInteger, but NewByteArray takes jsize
   174         jsize tiffSize = (jsize)[tiffImage length]; // #warning 64-bit: -length returns NSUInteger, but NewByteArray takes jsize
   174         returnValue = (*env)->NewByteArray(env, tiffSize);
   175         returnValue = (*env)->NewByteArray(env, tiffSize);
       
   176         CHECK_NULL_RETURN(returnValue, nil);
   175         jbyte *tiffData = (jbyte *)(*env)->GetPrimitiveArrayCritical(env, returnValue, 0);
   177         jbyte *tiffData = (jbyte *)(*env)->GetPrimitiveArrayCritical(env, returnValue, 0);
       
   178         CHECK_NULL_RETURN(tiffData, nil);
   176         [tiffImage getBytes:tiffData];
   179         [tiffImage getBytes:tiffData];
   177         (*env)->ReleasePrimitiveArrayCritical(env, returnValue, tiffData, 0); // Do not use JNI_COMMIT, as that will not free the buffer copy when +ProtectJavaHeap is on.
   180         (*env)->ReleasePrimitiveArrayCritical(env, returnValue, tiffData, 0); // Do not use JNI_COMMIT, as that will not free the buffer copy when +ProtectJavaHeap is on.
   178         [imageRep release];
   181         [imageRep release];
   179     }
   182     }
   180 JNF_COCOA_EXIT(env);
   183 JNF_COCOA_EXIT(env);
   182 
   185 
   183 }
   186 }
   184 
   187 
   185 static jobject getImageForByteStream(JNIEnv *env, jbyteArray sourceData)
   188 static jobject getImageForByteStream(JNIEnv *env, jbyteArray sourceData)
   186 {
   189 {
   187     if (sourceData == NULL) return NULL;
   190     CHECK_NULL_RETURN(sourceData, NULL);
   188 
   191 
   189     jsize sourceSize = (*env)->GetArrayLength(env, sourceData);
   192     jsize sourceSize = (*env)->GetArrayLength(env, sourceData);
   190     if (sourceSize == 0) return NULL;
   193     if (sourceSize == 0) return NULL;
   191 
   194 
   192     jbyte *sourceBytes = (*env)->GetPrimitiveArrayCritical(env, sourceData, NULL);
   195     jbyte *sourceBytes = (*env)->GetPrimitiveArrayCritical(env, sourceData, NULL);
       
   196     CHECK_NULL_RETURN(sourceBytes, NULL);
   193     NSData *rawData = [NSData dataWithBytes:sourceBytes length:sourceSize];
   197     NSData *rawData = [NSData dataWithBytes:sourceBytes length:sourceSize];
   194 
   198 
   195     NSImage *newImage = [[NSImage alloc] initWithData:rawData];
   199     NSImage *newImage = [[NSImage alloc] initWithData:rawData];
   196     if (newImage) CFRetain(newImage); // GC
   200     if (newImage) CFRetain(newImage); // GC
   197     [newImage release];
   201     [newImage release];
   198 
   202 
   199     (*env)->ReleasePrimitiveArrayCritical(env, sourceData, sourceBytes, JNI_ABORT);
   203     (*env)->ReleasePrimitiveArrayCritical(env, sourceData, sourceBytes, JNI_ABORT);
   200 
   204     CHECK_NULL_RETURN(newImage, NULL);
   201     if (newImage == nil) return NULL;
       
   202 
   205 
   203     // The ownership of the NSImage is passed to the new CImage jobject. No need to release it.
   206     // The ownership of the NSImage is passed to the new CImage jobject. No need to release it.
   204     static JNF_CLASS_CACHE(jc_CImage, "sun/lwawt/macosx/CImage");
   207     static JNF_CLASS_CACHE(jc_CImage, "sun/lwawt/macosx/CImage");
   205     static JNF_STATIC_MEMBER_CACHE(jm_CImage_getCreator, jc_CImage, "getCreator", "()Lsun/lwawt/macosx/CImage$Creator;");
   208     static JNF_STATIC_MEMBER_CACHE(jm_CImage_getCreator, jc_CImage, "getCreator", "()Lsun/lwawt/macosx/CImage$Creator;");
   206     jobject creator = JNFCallStaticObjectMethod(env, jm_CImage_getCreator);
   209     jobject creator = JNFCallStaticObjectMethod(env, jm_CImage_getCreator);
   229 {
   232 {
   230     NSUInteger filenameCount = [filenameArray count];
   233     NSUInteger filenameCount = [filenameArray count];
   231     if (filenameCount == 0) return nil;
   234     if (filenameCount == 0) return nil;
   232 
   235 
   233     // Get the java.lang.String class object:
   236     // Get the java.lang.String class object:
   234     jclass stringClazz = (*env)->FindClass(env, "java/lang/String"); // can't be null
   237     jclass stringClazz = (*env)->FindClass(env, "java/lang/String");
       
   238     CHECK_NULL_RETURN(stringClazz, nil);
   235     jobject jfilenameArray = (*env)->NewObjectArray(env, filenameCount, stringClazz, NULL); // AWT_THREADING Safe (known object)
   239     jobject jfilenameArray = (*env)->NewObjectArray(env, filenameCount, stringClazz, NULL); // AWT_THREADING Safe (known object)
   236     if ((*env)->ExceptionOccurred(env)) {
   240     if ((*env)->ExceptionOccurred(env)) {
   237         (*env)->ExceptionDescribe(env);
   241         (*env)->ExceptionDescribe(env);
   238         (*env)->ExceptionClear(env);
   242         (*env)->ExceptionClear(env);
   239         return nil;
   243         return nil;