Chris Craik | a1265c3 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 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 | #ifndef RASTERMILL_STREAM_H |
| 18 | #define RASTERMILL_STREAM_H |
| 19 | |
| 20 | #include <jni.h> |
| 21 | #include <stdio.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | class Stream { |
| 25 | public: |
| 26 | Stream(); |
| 27 | virtual ~Stream(); |
| 28 | |
| 29 | size_t peek(void* buffer, size_t size); |
| 30 | size_t read(void* buffer, size_t size); |
| 31 | |
| 32 | protected: |
| 33 | virtual size_t doRead(void* buffer, size_t size) = 0; |
| 34 | |
| 35 | private: |
| 36 | char* mPeekBuffer; |
| 37 | size_t mPeekSize; |
| 38 | size_t mPeekOffset; |
| 39 | }; |
| 40 | |
| 41 | class MemoryStream : public Stream { |
| 42 | public: |
| 43 | MemoryStream(void* buffer, size_t size) : |
| 44 | mBuffer((char*)buffer), |
| 45 | mRemaining(size) {} |
| 46 | |
| 47 | protected: |
| 48 | virtual size_t doRead(void* buffer, size_t size); |
| 49 | |
| 50 | private: |
| 51 | char* mBuffer; |
| 52 | size_t mRemaining; |
| 53 | }; |
| 54 | |
| 55 | class FileStream : public Stream { |
| 56 | public: |
| 57 | FileStream(FILE* fd) : mFd(fd) {} |
| 58 | |
| 59 | protected: |
| 60 | virtual size_t doRead(void* buffer, size_t size); |
| 61 | |
| 62 | private: |
| 63 | FILE* mFd; |
| 64 | }; |
| 65 | |
| 66 | class JavaInputStream : public Stream { |
| 67 | public: |
| 68 | JavaInputStream(JNIEnv* env, jobject inputStream, jbyteArray byteArray) : |
| 69 | mEnv(env), |
| 70 | mInputStream(inputStream), |
| 71 | mByteArray(byteArray), |
| 72 | mByteArrayLength(env->GetArrayLength(byteArray)) {} |
| 73 | |
| 74 | protected: |
| 75 | virtual size_t doRead(void* buffer, size_t size); |
| 76 | |
| 77 | private: |
| 78 | JNIEnv* mEnv; |
| 79 | const jobject mInputStream; |
| 80 | const jbyteArray mByteArray; |
| 81 | const size_t mByteArrayLength; |
| 82 | }; |
| 83 | |
| 84 | jint JavaStream_OnLoad(JNIEnv* env); |
| 85 | |
| 86 | #endif //RASTERMILL_STREAM_H |