jdk/src/java.base/windows/native/libnio/ch/FileChannelImpl.c
changeset 27937 0c9f63e42e91
parent 26732 9b27273e6131
child 45893 2678b360eecd
equal deleted inserted replaced
27936:ca9ee8e3d527 27937:0c9f63e42e91
    29 #include "jlong.h"
    29 #include "jlong.h"
    30 #include <io.h>
    30 #include <io.h>
    31 #include "nio.h"
    31 #include "nio.h"
    32 #include "nio_util.h"
    32 #include "nio_util.h"
    33 #include "sun_nio_ch_FileChannelImpl.h"
    33 #include "sun_nio_ch_FileChannelImpl.h"
       
    34 #include "java_lang_Integer.h"
       
    35 
       
    36 #include <Mswsock.h>
       
    37 #pragma comment(lib, "Mswsock.lib")
    34 
    38 
    35 static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
    39 static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
    36 
    40 
    37 /**************************************************************
    41 /**************************************************************
    38  * static method to store field ID's in initializers
    42  * static method to store field ID's in initializers
   173     }
   177     }
   174 }
   178 }
   175 
   179 
   176 JNIEXPORT jlong JNICALL
   180 JNIEXPORT jlong JNICALL
   177 Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
   181 Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
   178                                             jint srcFD,
   182                                             jobject srcFD,
   179                                             jlong position, jlong count,
   183                                             jlong position, jlong count,
   180                                             jint dstFD)
   184                                             jobject dstFD)
   181 {
   185 {
   182     return IOS_UNSUPPORTED;
   186     const int PACKET_SIZE = 524288;
   183 }
   187 
       
   188     HANDLE src = (HANDLE)(handleval(env, srcFD));
       
   189     SOCKET dst = (SOCKET)(fdval(env, dstFD));
       
   190     DWORD chunkSize = (count > java_lang_Integer_MAX_VALUE) ?
       
   191         java_lang_Integer_MAX_VALUE : (DWORD)count;
       
   192     BOOL result = 0;
       
   193 
       
   194     jlong pos = Java_sun_nio_ch_FileChannelImpl_position0(env, this, srcFD, position);
       
   195     if (pos == IOS_THROWN) {
       
   196         return IOS_THROWN;
       
   197     }
       
   198 
       
   199     result = TransmitFile(
       
   200         dst,
       
   201         src,
       
   202         chunkSize,
       
   203         PACKET_SIZE,
       
   204         NULL,
       
   205         NULL,
       
   206         TF_USE_KERNEL_APC
       
   207     );
       
   208     if (!result) {
       
   209         int error = WSAGetLastError();
       
   210         if (WSAEINVAL == error && count >= 0) {
       
   211             return IOS_UNSUPPORTED_CASE;
       
   212         }
       
   213         if (WSAENOTSOCK == error) {
       
   214             return IOS_UNSUPPORTED_CASE;
       
   215         }
       
   216         JNU_ThrowIOExceptionWithLastError(env, "transfer failed");
       
   217         return IOS_THROWN;
       
   218     }
       
   219     return chunkSize;
       
   220 }