private static byte[] readOneBlock(InputStream is) throws IOException {
  int c=is.read();
  if (c == -1) {
    return null;
  }
  if (c == DerValue.tag_Sequence) {
    ByteArrayOutputStream bout=new ByteArrayOutputStream(2048);
    bout.write(c);
    readBERInternal(is,bout,c);
    return bout.toByteArray();
  }
 else {
    char[] data=new char[2048];
    int pos=0;
    int hyphen=(c == '-') ? 1 : 0;
    int last=(c == '-') ? -1 : c;
    while (true) {
      int next=is.read();
      if (next == -1) {
        return null;
      }
      if (next == '-') {
        hyphen++;
      }
 else {
        hyphen=0;
        last=next;
      }
      if (hyphen == 5 && (last == -1 || last == '\r' || last == '\n')) {
        break;
      }
    }
    int end;
    StringBuilder header=new StringBuilder("-----");
    while (true) {
      int next=is.read();
      if (next == -1) {
        throw new IOException("Incomplete data");
      }
      if (next == '\n') {
        end='\n';
        break;
      }
      if (next == '\r') {
        next=is.read();
        if (next == -1) {
          throw new IOException("Incomplete data");
        }
        if (next == '\n') {
          end='\n';
        }
 else {
          end='\r';
          data[pos++]=(char)next;
        }
        break;
      }
      header.append((char)next);
    }
    while (true) {
      int next=is.read();
      if (next == -1) {
        throw new IOException("Incomplete data");
      }
      if (next != '-') {
        data[pos++]=(char)next;
        if (pos >= data.length) {
          data=Arrays.copyOf(data,data.length + 1024);
        }
      }
 else {
        break;
      }
    }
    StringBuilder footer=new StringBuilder("-");
    while (true) {
      int next=is.read();
      if (next == -1 || next == end || next == '\n') {
        break;
      }
      if (next != '\r')       footer.append((char)next);
    }
    checkHeaderFooter(header.toString(),footer.toString());
    return Pem.decode(new String(data,0,pos));
  }
}

private static byte [ ] readOneBlock ( InputStream is ) throws IOException { int c = is . read ( ) ; if ( c == - 1 ) { return null ; } if ( c == DerValue . tag_Sequence ) { ByteArrayOutputStream bout = new ByteArrayOutputStream ( 2048 ) ; bout . write ( c ) ; readBERInternal ( is , bout , c ) ; return bout . toByteArray ( ) ; } else { char [ ] data = new char [ 2048 ] ; int pos = 0 ; int hyphen = ( c == '-' ) ? 1 : 0 ; int last = ( c == '-' ) ? - 1 : c ; while ( true ) { int next = is . read ( ) ; if ( next == - 1 ) { return null ; } if ( next == '-' ) { hyphen ++ ; } else { hyphen = 0 ; last = next ; } if ( hyphen == 5 && ( last == - 1 || last == '\r' || last == '\n' ) ) { break ; } } int end ; StringBuilder header = new StringBuilder ( "-----" ) ; while ( true ) { int next = is . read ( ) ; if ( next == - 1 ) { throw new IOException ( "Incomplete data" ) ; } if ( next == '\n' ) { end = '\n' ; break ; } if ( next == '\r' ) { next = is . read ( ) ; if ( next == - 1 ) { throw new IOException ( "Incomplete data" ) ; } if ( next == '\n' ) { end = '\n' ; } else { end = '\r' ; data [ pos ++ ] = ( char ) next ; } break ; } header . append ( ( char ) next ) ; } while ( true ) { int next = is . read ( ) ; if ( next == - 1 ) { throw new IOException ( "Incomplete data" ) ; } if ( next != '-' ) { data [ pos ++ ] = ( char ) next ; if ( pos >= data . length ) { data = Arrays . copyOf ( data , data . length + 1024 ) ; } } else { break ; } } StringBuilder footer = new StringBuilder ( "-" ) ; while ( true ) { int next = is . read ( ) ; if ( next == - 1 || next == end || next == '\n' ) { break ; } if ( next != '\r' ) footer . append ( ( char ) next ) ; } checkHeaderFooter ( header . toString ( ) , footer . toString ( ) ) ; return Pem . decode ( new String ( data , 0 , pos ) ) ; } }
returns an asn . 1 sequence from a stream , which might be a ber - encoded binary block or a pem - style base64 - encoded ascii data . in the latter case , it ' s de - base64 ' ed before return . after the reading , the input stream pointer is after the ber block , or after the newline character after the - - - - - end something - - - - - line .
