1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "outputstream_wrapper.h"
18#include "error_codes.h"
19
20jmethodID OutputStreamWrapper::sWriteID = NULL;
21
22int32_t OutputStreamWrapper::write(int32_t length, int32_t offset) {
23    if (offset < 0 || length < 0 || (offset + length) > getBufferSize()) {
24        return J_ERROR_BAD_ARGS;
25    }
26    mEnv->ReleaseByteArrayElements(mByteArray, mBytes, JNI_COMMIT);
27    mBytes = NULL;
28    if (mEnv->ExceptionCheck()) {
29        return J_EXCEPTION;
30    }
31    if (sWriteID == NULL) {
32        LOGE("Uninitialized method ID for OutputStream write function.");
33        return J_ERROR_FATAL;
34    }
35    // Call OutputStream write with byte array.
36    mEnv->CallVoidMethod(mStream, sWriteID, mByteArray, offset, length);
37    if (mEnv->ExceptionCheck()) {
38        return J_EXCEPTION;
39    }
40    mBytes = mEnv->GetByteArrayElements(mByteArray, NULL);
41    if (mBytes == NULL || mEnv->ExceptionCheck()) {
42        return J_EXCEPTION;
43    }
44    return J_SUCCESS;
45}
46
47void OutputStreamWrapper::setWriteMethodID(jmethodID id) {
48    sWriteID = id;
49}
50