Home | History | Annotate | Download | only in src
      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 "inputstream_wrapper.h"
     18 #include "error_codes.h"
     19 
     20 jmethodID InputStreamWrapper::sReadID = NULL;
     21 jmethodID InputStreamWrapper::sSkipID = NULL;
     22 
     23 int32_t InputStreamWrapper::read(int32_t length, int32_t offset) {
     24     if (offset < 0 || length < 0 || (offset + length) > getBufferSize()) {
     25         return J_ERROR_BAD_ARGS;
     26     }
     27     int32_t bytesRead = 0;
     28     mEnv->ReleaseByteArrayElements(mByteArray, mBytes, JNI_COMMIT);
     29     mBytes = NULL;
     30     if (mEnv->ExceptionCheck()) {
     31         return J_EXCEPTION;
     32     }
     33     bytesRead = static_cast<int32_t>(mEnv->CallIntMethod(mStream, sReadID,
     34             mByteArray, offset, length));
     35     if (mEnv->ExceptionCheck()) {
     36         return J_EXCEPTION;
     37     }
     38     mBytes = mEnv->GetByteArrayElements(mByteArray, NULL);
     39     if (mBytes == NULL || mEnv->ExceptionCheck()) {
     40         return J_EXCEPTION;
     41     }
     42     if (bytesRead == END_OF_STREAM) {
     43         return J_DONE;
     44     }
     45     return bytesRead;
     46 }
     47 
     48 int64_t InputStreamWrapper::skip(int64_t count) {
     49     int64_t bytesSkipped = 0;
     50     bytesSkipped = static_cast<int64_t>(mEnv->CallLongMethod(mStream, sSkipID,
     51             static_cast<jlong>(count)));
     52     if (mEnv->ExceptionCheck() || bytesSkipped < 0) {
     53         return J_EXCEPTION;
     54     }
     55     return bytesSkipped;
     56 }
     57 
     58 // Acts like a read call that returns the End Of Image marker for a JPEG file.
     59 int32_t InputStreamWrapper::forceReadEOI() {
     60     mBytes[0] = (jbyte) 0xFF;
     61     mBytes[1] = (jbyte) 0xD9;
     62     return 2;
     63 }
     64 
     65 void InputStreamWrapper::setReadSkipMethodIDs(jmethodID readID,
     66         jmethodID skipID) {
     67     sReadID = readID;
     68     sSkipID = skipID;
     69 }
     70