0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <errno.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include <sys/socket.h>
0N/A
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "net_util.h"
0N/A
0N/A#include "java_net_SocketOutputStream.h"
0N/A
0N/A#define min(a, b) ((a) < (b) ? (a) : (b))
0N/A
0N/A/*
0N/A * SocketOutputStream
0N/A */
0N/A
0N/Astatic jfieldID IO_fd_fdID;
0N/A
0N/A/*
0N/A * Class: java_net_SocketOutputStream
0N/A * Method: init
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) {
0N/A IO_fd_fdID = NET_GetFileDescriptorID(env);
0N/A}
0N/A
0N/A/*
0N/A * Class: java_net_SocketOutputStream
0N/A * Method: socketWrite0
0N/A * Signature: (Ljava/io/FileDescriptor;[BII)V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
0N/A jobject fdObj,
0N/A jbyteArray data,
0N/A jint off, jint len) {
0N/A char *bufP;
0N/A char BUF[MAX_BUFFER_LEN];
0N/A int buflen;
0N/A int fd;
0N/A
0N/A if (IS_NULL(fdObj)) {
0N/A JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
0N/A return;
0N/A } else {
0N/A fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
0N/A /* Bug 4086704 - If the Socket associated with this file descriptor
0N/A * was closed (sysCloseFD), the the file descriptor is set to -1.
0N/A */
0N/A if (fd == -1) {
0N/A JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
0N/A return;
0N/A }
0N/A
0N/A }
0N/A
0N/A if (len <= MAX_BUFFER_LEN) {
0N/A bufP = BUF;
0N/A buflen = MAX_BUFFER_LEN;
0N/A } else {
0N/A buflen = min(MAX_HEAP_BUFFER_LEN, len);
0N/A bufP = (char *)malloc((size_t)buflen);
0N/A
0N/A /* if heap exhausted resort to stack buffer */
0N/A if (bufP == NULL) {
0N/A bufP = BUF;
0N/A buflen = MAX_BUFFER_LEN;
0N/A }
0N/A }
0N/A
0N/A while(len > 0) {
0N/A int loff = 0;
0N/A int chunkLen = min(buflen, len);
0N/A int llen = chunkLen;
0N/A (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
0N/A
0N/A while(llen > 0) {
0N/A int n = NET_Send(fd, bufP + loff, llen, 0);
0N/A if (n > 0) {
0N/A llen -= n;
0N/A loff += n;
0N/A continue;
0N/A }
0N/A if (n == JVM_IO_INTR) {
0N/A JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
0N/A } else {
0N/A if (errno == ECONNRESET) {
0N/A JNU_ThrowByName(env, "sun/net/ConnectionResetException",
0N/A "Connection reset");
0N/A } else {
0N/A NET_ThrowByNameWithLastError(env, "java/net/SocketException",
0N/A "Write failed");
0N/A }
0N/A }
0N/A if (bufP != BUF) {
0N/A free(bufP);
0N/A }
0N/A return;
0N/A }
0N/A len -= chunkLen;
0N/A off += chunkLen;
0N/A }
0N/A
0N/A if (bufP != BUF) {
0N/A free(bufP);
0N/A }
0N/A}