summaryrefslogtreecommitdiffhomepage
path: root/src/java/nxt_jni_OutputStream.c
blob: 170b33ba937c2cf51ecef2d9c1a114f5e98dff62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

/*
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_auto_config.h>

#include <jni.h>
#include <nxt_unit.h>

#include "nxt_jni.h"
#include "nxt_jni_OutputStream.h"
#include "nxt_jni_URLClassLoader.h"


static void JNICALL nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls,
    jlong req_info_ptr, jint b);
static nxt_unit_buf_t *nxt_java_OutputStream_req_buf(JNIEnv *env,
    nxt_unit_request_info_t *req);
static void JNICALL nxt_java_OutputStream_write(JNIEnv *env, jclass cls,
    jlong req_info_ptr, jarray b, jint off, jint len);
static void JNICALL nxt_java_OutputStream_flush(JNIEnv *env, jclass cls,
    jlong req_info_ptr);
static void JNICALL nxt_java_OutputStream_close(JNIEnv *env, jclass cls,
    jlong req_info_ptr);


static jclass  nxt_java_OutputStream_class;


int
nxt_java_initOutputStream(JNIEnv *env, jobject cl)
{
    int     res;
    jclass  cls;

    cls = nxt_java_loadClass(env, cl, "nginx.unit.OutputStream");
    if (cls == NULL) {
        return NXT_UNIT_ERROR;
    }

    nxt_java_OutputStream_class = (*env)->NewGlobalRef(env, cls);
    (*env)->DeleteLocalRef(env, cls);

    cls = nxt_java_OutputStream_class;

    JNINativeMethod os_methods[] = {
        { (char *) "write",
          (char *) "(JI)V",
          nxt_java_OutputStream_writeByte },

        { (char *) "write",
          (char *) "(J[BII)V",
          nxt_java_OutputStream_write },

        { (char *) "flush",
          (char *) "(J)V",
          nxt_java_OutputStream_flush },

        { (char *) "close",
          (char *) "(J)V",
          nxt_java_OutputStream_close },
    };

    res = (*env)->RegisterNatives(env, nxt_java_OutputStream_class,
                                  os_methods,
                                  sizeof(os_methods) / sizeof(os_methods[0]));

    nxt_unit_debug(NULL, "registered OutputStream methods: %d", res);

    if (res != 0) {
        (*env)->DeleteGlobalRef(env, cls);
        return NXT_UNIT_ERROR;
    }

    return NXT_UNIT_OK;
}


static void JNICALL
nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls, jlong req_info_ptr,
    jint b)
{
    nxt_unit_buf_t           *buf;
    nxt_unit_request_info_t  *req;
    nxt_java_request_data_t  *data;

    req = nxt_jlong2ptr(req_info_ptr);
    data = req->data;

    buf = nxt_java_OutputStream_req_buf(env, req);
    if (buf == NULL) {
        return;
    }

    *buf->free++ = b;

    if ((uint32_t) (buf->free - buf->start) >= data->buf_size) {
        nxt_java_OutputStream_flush_buf(env, req);
    }
}


int
nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req)
{
    int                      rc;
    nxt_java_request_data_t  *data;

    data = req->data;

    if (!nxt_unit_response_is_init(req)) {
        rc = nxt_unit_response_init(req, 200, 0, 0);
        if (rc != NXT_UNIT_OK) {
            nxt_java_throw_IOException(env, "Failed to allocate response");

            return rc;
        }
    }

    if (!nxt_unit_response_is_sent(req)) {
        rc = nxt_unit_response_send(req);
        if (rc != NXT_UNIT_OK) {
            nxt_java_throw_IOException(env, "Failed to send response headers");

            return rc;
        }
    }

    if (data->buf != NULL) {
        rc = nxt_unit_buf_send(data->buf);
        if (rc != NXT_UNIT_OK) {
            nxt_java_throw_IOException(env, "Failed to send buffer");

        } else {
            data->buf = NULL;
        }

    } else {
        rc = NXT_UNIT_OK;
    }

    return rc;
}


static nxt_unit_buf_t *
nxt_java_OutputStream_req_buf(JNIEnv *env, nxt_unit_request_info_t *req)
{
    uint32_t                 size;
    nxt_unit_buf_t           *buf;
    nxt_java_request_data_t  *data;

    data = req->data;
    buf = data->buf;

    if (buf == NULL || buf->free >= buf->end) {
        size = data->buf_size == 0 ? nxt_unit_buf_min() : data->buf_size;

        buf = nxt_unit_response_buf_alloc(req, size);
        if (buf == NULL) {
            nxt_java_throw_IOException(env, "Failed to allocate buffer");

            return NULL;
        }

        data->buf = buf;
    }

    return buf;
}


static void JNICALL
nxt_java_OutputStream_write(JNIEnv *env, jclass cls, jlong req_info_ptr,
    jarray b, jint off, jint len)
{
    int                      rc;
    jint                     copy;
    uint8_t                  *ptr;
    nxt_unit_buf_t           *buf;
    nxt_unit_request_info_t  *req;
    nxt_java_request_data_t  *data;

    req = nxt_jlong2ptr(req_info_ptr);
    data = req->data;

    ptr = (*env)->GetPrimitiveArrayCritical(env, b, NULL);

    while (len > 0) {
        buf = nxt_java_OutputStream_req_buf(env, req);
        if (buf == NULL) {
            return;
        }

        copy = buf->end - buf->free;
        copy = copy < len ? copy : len;

        memcpy(buf->free, ptr + off, copy);
        buf->free += copy;

        len -= copy;
        off += copy;

        if ((uint32_t) (buf->free - buf->start) >= data->buf_size) {
            rc = nxt_java_OutputStream_flush_buf(env, req);
            if (rc != NXT_UNIT_OK) {
                break;
            }
        }
    }

    (*env)->ReleasePrimitiveArrayCritical(env, b, ptr, 0);
}


static void JNICALL
nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
    nxt_unit_request_info_t  *req;
    nxt_java_request_data_t  *data;

    req = nxt_jlong2ptr(req_info_ptr);
    data = req->data;

    if (data->buf != NULL && data->buf->free > data->buf->start) {
        nxt_java_OutputStream_flush_buf(env, req);
    }
}


static void JNICALL
nxt_java_OutputStream_close(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
    nxt_java_OutputStream_flush_buf(env, nxt_jlong2ptr(req_info_ptr));
}