OpenSSLBIOInputStream.java revision c1f6588cf2400b3118bb4fcc65f695491110a4f3
1/* 2 * Copyright (C) 2012 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 17package org.apache.harmony.xnet.provider.jsse; 18 19import java.io.FilterInputStream; 20import java.io.IOException; 21import java.io.InputStream; 22 23/** 24 * Provides an interface to OpenSSL's BIO system directly from a Java 25 * InputStream. It allows an OpenSSL API to read directly from something more 26 * flexible interface than a byte array. 27 */ 28public class OpenSSLBIOInputStream extends FilterInputStream { 29 private long ctx; 30 31 public OpenSSLBIOInputStream(InputStream is) { 32 super(is); 33 34 ctx = NativeCrypto.create_BIO_InputStream(this); 35 } 36 37 public long getBioContext() { 38 return ctx; 39 } 40 41 public int readLine(byte[] buffer) throws IOException { 42 if (buffer == null || buffer.length == 0) { 43 return 0; 44 } 45 46 int offset = 0; 47 int inputByte = read(); 48 while (offset < buffer.length && inputByte != '\n' && inputByte != -1) { 49 buffer[offset++] = (byte) inputByte; 50 inputByte = read(); 51 } 52 53 if (inputByte == '\n') { 54 buffer[offset++] = '\n'; 55 } 56 57 return offset; 58 } 59} 60