1a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk/* 2a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Copyright (C) 2013 The Android Open Source Project 3a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * 4a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Licensed under the Apache License, Version 2.0 (the "License"); 5a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * you may not use this file except in compliance with the License. 6a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * You may obtain a copy of the License at 7a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * 8a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * http://www.apache.org/licenses/LICENSE-2.0 9a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * 10a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * Unless required by applicable law or agreed to in writing, software 11a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * distributed under the License is distributed on an "AS IS" BASIS, 12a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * See the License for the specific language governing permissions and 14a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk * limitations under the License. 15a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk */ 16a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 17a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include "inputstream_wrapper.h" 18a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk#include "error_codes.h" 19a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 20a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben BrunkjmethodID InputStreamWrapper::sReadID = NULL; 21a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben BrunkjmethodID InputStreamWrapper::sSkipID = NULL; 22a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 23a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkint32_t InputStreamWrapper::read(int32_t length, int32_t offset) { 24a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (offset < 0 || length < 0 || (offset + length) > getBufferSize()) { 25a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_ERROR_BAD_ARGS; 26a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 27a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk int32_t bytesRead = 0; 28a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mEnv->ReleaseByteArrayElements(mByteArray, mBytes, JNI_COMMIT); 29a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mBytes = NULL; 30a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (mEnv->ExceptionCheck()) { 31a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_EXCEPTION; 32a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 33a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk bytesRead = static_cast<int32_t>(mEnv->CallIntMethod(mStream, sReadID, 34a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mByteArray, offset, length)); 35a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (mEnv->ExceptionCheck()) { 36a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_EXCEPTION; 37a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 38a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mBytes = mEnv->GetByteArrayElements(mByteArray, NULL); 39a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (mBytes == NULL || mEnv->ExceptionCheck()) { 40a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_EXCEPTION; 41a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 42a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (bytesRead == END_OF_STREAM) { 43a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_DONE; 44a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 45a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return bytesRead; 46a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk} 47a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 48a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkint64_t InputStreamWrapper::skip(int64_t count) { 49a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk int64_t bytesSkipped = 0; 50a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk bytesSkipped = static_cast<int64_t>(mEnv->CallLongMethod(mStream, sSkipID, 51a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk static_cast<jlong>(count))); 52a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk if (mEnv->ExceptionCheck() || bytesSkipped < 0) { 53a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return J_EXCEPTION; 54a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk } 55a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return bytesSkipped; 56a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk} 57a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 58a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk// Acts like a read call that returns the End Of Image marker for a JPEG file. 59a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkint32_t InputStreamWrapper::forceReadEOI() { 60a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mBytes[0] = (jbyte) 0xFF; 61a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk mBytes[1] = (jbyte) 0xD9; 62a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk return 2; 63a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk} 64a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk 65a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunkvoid InputStreamWrapper::setReadSkipMethodIDs(jmethodID readID, 66a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk jmethodID skipID) { 67a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk sReadID = readID; 68a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk sSkipID = skipID; 69a8221bbdb8ece9b02dbf7b72565f9fbc5b314f7cRuben Brunk} 70