Skip to content

Instantly share code, notes, and snippets.

@cscotta
Created September 17, 2011 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscotta/1224270 to your computer and use it in GitHub Desktop.
Save cscotta/1224270 to your computer and use it in GitHub Desktop.
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
jint srcFD,
jlong position, jlong count,
jint dstFD)
{
#ifdef __linux__
jlong max = (jlong)java_lang_Integer_MAX_VALUE;
jlong n;
if (my_sendfile64_func == NULL) {
off_t offset;
if (position > max)
return IOS_UNSUPPORTED_CASE;
if (count > max)
count = max;
offset = (off_t)position;
n = sendfile(dstFD, srcFD, &offset, (size_t)count);
} else {
off64_t offset = (off64_t)position;
n = (*my_sendfile64_func)(dstFD, srcFD, &offset, (size_t)count);
}
if (n < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if ((errno == EINVAL) && ((ssize_t)count >= 0))
return IOS_UNSUPPORTED_CASE;
if (errno == EINTR) {
return IOS_INTERRUPTED;
}
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
return IOS_THROWN;
}
return n;
#endif
#ifdef __solaris__
if (my_sendfile_func == NULL) {
return IOS_UNSUPPORTED;
} else {
sendfilevec_t sfv;
size_t numBytes = 0;
jlong result;
sfv.sfv_fd = srcFD;
sfv.sfv_flag = 0;
sfv.sfv_off = (off64_t)position;
sfv.sfv_len = count;
result = (*my_sendfile_func)(dstFD, &sfv, 1, &numBytes);
/* Solaris sendfilev() will return -1 even if some bytes have been
* transferred, so we check numBytes first.
*/
if (numBytes > 0)
return numBytes;
if (result < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if ((errno == EINVAL) && ((ssize_t)count >= 0))
return IOS_UNSUPPORTED_CASE;
if (errno == EINTR)
return IOS_INTERRUPTED;
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
return IOS_THROWN;
}
return result;
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment