1 /* 2 * Copyright (C) 2017 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 #ifndef CONSCRYPT_BIOSTREAM_H_ 18 #define CONSCRYPT_BIOSTREAM_H_ 19 20 #include <jni.h> 21 22 #include "JniConstants.h" 23 #include "Trace.h" 24 25 namespace conscrypt { 26 27 /** 28 * BIO for InputStream 29 */ 30 class BioStream { 31 public: BioStream(jobject stream)32 BioStream(jobject stream) : mEof(false) { 33 JNIEnv* env = JniConstants::getJNIEnv(); 34 mStream = env->NewGlobalRef(stream); 35 } 36 ~BioStream()37 ~BioStream() { 38 JNIEnv* env = JniConstants::getJNIEnv(); 39 40 env->DeleteGlobalRef(mStream); 41 } 42 isEof()43 bool isEof() const { 44 JNI_TRACE("isEof? %s", mEof ? "yes" : "no"); 45 return mEof; 46 } 47 flush()48 int flush() { 49 JNIEnv* env = JniConstants::getJNIEnv(); 50 if (env == nullptr) { 51 return -1; 52 } 53 54 if (env->ExceptionCheck()) { 55 JNI_TRACE("BioStream::flush called with pending exception"); 56 return -1; 57 } 58 59 env->CallVoidMethod(mStream, JniConstants::outputStream_flushMethod); 60 if (env->ExceptionCheck()) { 61 return -1; 62 } 63 64 return 1; 65 } 66 67 protected: getStream()68 jobject getStream() { 69 return mStream; 70 } 71 setEof(bool eof)72 void setEof(bool eof) { 73 mEof = eof; 74 } 75 76 private: 77 jobject mStream; 78 bool mEof; 79 }; 80 81 } // namespace conscrypt 82 83 #endif // CONSCRYPT_BIOSTREAM_H_ 84