Read the lines between BEGIN and END marker and convert
the Base64 encoded content into binary byte array.

@return DER encoded octet stream
@throws IOException
7466
private static byte[] readBytes(BufferedReader reader, String endMarker) throws IOException
    {
        String          line = null;
        StringBuilder   buf = new StringBuilder();

        while ((line = reader.readLine()) != null)
        {
            if (line.contains(endMarker)) {
                return Base64.getDecoder().decode(buf.toString());
            }

            buf.append(line.trim());        
        }

        throw new IOException("Invalid PEM file: No end marker");
    }
1310392
public String readLineUntilBoundary() throws IOException {
		StringBuilder bldr = new StringBuilder();
		while (true) {
			int b = read();
			if (bldr.indexOf(boundary) == 0) {
				leftover = ByteBuffer.wrap(boundaryBytes);
				return null;
			} else if (b == -1 || b == 10) {
				break;
			} else if (b != '\r') {
				bldr.append((char) ((byte) b));
			}
		}
		return bldr.toString();
	}
376210
public static byte[] readLine(InputStream in) throws IOException {
		int BUFF_LENGTH = 256;
		byte[] buf = new byte[BUFF_LENGTH];

		int i = 0;
		int loops = 0;
		int ch = 0;

		while (true) {
			ch = in.read();
			if (ch < 0) {
				break;
			}
			loops++;

			// skip a leading LF's
			if (loops == 1 && ch == LF) {
				continue;
			}

			if (ch == CR || ch == LF) {
				break;
			}

			if (i >= buf.length) {
				byte[] oldBuf = buf;
				buf = new byte[oldBuf.length + BUFF_LENGTH];
				System.arraycopy(oldBuf, 0, buf, 0, oldBuf.length);
			}
			buf[i++] = (byte) ch;
		}

		if (ch == -1 && i == 0) {
			return null;
		}

		// skip a trailing LF if it exists
		if (ch == CR && in.available() >= 1 && in.markSupported()) {
			in.mark(1);
			ch = in.read();
			if (ch != LF) {
				in.reset();
			}
		}

		byte[] oldBuf = buf;
		buf = new byte[i];
		System.arraycopy(oldBuf, 0, buf, 0, i);

		return buf;
	}
491204
public DerInputStream toDerInputStream() throws IOException {
        if (tag == tag_Sequence || tag == tag_Set)
            return new DerInputStream(buffer);
        throw new IOException("toDerInputStream rejects tag type " + tag);
    }
683495
byte []readBinary()
    throws IOException
  {
    TempOutputStream tos = new TempOutputStream();
    
    while (true) {
      int ch = read();
      int len;
    
      switch (ch) {
      default:
        _peek = ch;
        return tos.toByteArray();
      
      case 0x20: case 0x21: case 0x22: case 0x23:
      case 0x24: case 0x25: case 0x26: case 0x27:
      case 0x28: case 0x29: case 0x2a: case 0x2b:
      case 0x2c: case 0x2d: case 0x2e: case 0x2f:
        len = ch - 0x20;
        readBinary(tos, len);
        return tos.toByteArray();
      
      case 0x34: case 0x35: case 0x36: case 0x37:
        len = ((ch - 0x34) << 8) + read();
        readBinary(tos, len);
        return tos.toByteArray();
        
      case 'B':
        len = readShort();
        readBinary(tos, len);
        return tos.toByteArray();
        
      case 'A':
        len = readShort();
        readBinary(tos, len);
        break;
      }
    }
  }
171825
private void encodeThis() throws IOException {
        if (maps == null || maps.isEmpty()) {
            this.extensionValue = null;
            return;
        }
        DerOutputStream os = new DerOutputStream();
        DerOutputStream tmp = new DerOutputStream();

        for (CertificatePolicyMap map : maps) {
            map.encode(tmp);
        }

        os.write(DerValue.tag_Sequence, tmp);
        this.extensionValue = os.toByteArray();
    }
1246784
public void getBytes(byte[] val) throws IOException {
        if ((val.length != 0) && (buffer.read(val) != val.length)) {
            throw new IOException("short read of DER octet string");
        }
    }
837966
public int read4LE() throws OtpErlangDecodeException {
        final byte[] b = new byte[4];
        try {
            super.read(b);
        } catch (final IOException e) {
            throw new OtpErlangDecodeException("Cannot read from input stream");
        }
        return (b[3] << 24 & 0xff000000) + (b[2] << 16 & 0xff0000)
                + (b[1] << 8 & 0xff00) + (b[0] & 0xff);
    }
712853
public SF readStructuredField() throws IOException {
		
		int buf = 0;
		
		long thisOffset = offset;

		if(leadingLengthBytes == -1) {
			// we haven't tested for mvs download leading length bytes
			// we don't need them and don't want to see them
			int leadingLength = 0;
			do {
				buf = read(); offset++;
				leadingLength++;
			} while((buf & 0xff) != 0x5a && buf != -1 && leadingLength < 5);
			
			if((buf & 0xff) != 0x5a) {
			    has5a = false;
			    leadingLength = 1;
			    // so try if byte 3 is 0xd3 -> so this would be an afp without 5a magic byte
			    offset = 2;
			    buf = read();
			    if(buf == -1) return null;
			    if((buf & 0xff) != 0xd3) {
		            throw new IOException("cannot find 5a magic byte nor d3 -> this is no AFP");			        
			    }
			    offset = 0;
			}
			
			leadingLengthBytes = leadingLength-1;
			
//			if(buf == -1 && leadingLength > 1)
//				throw new IOException("found trailing garbage at the end of file.");
		} else {
			if(leadingLengthBytes > 0) read(data, 0, leadingLengthBytes); // just throw away those
			if(has5a) {
			    buf = read();
			    offset++;
			}
		}
		
		if(buf == -1) {
			return null;
		}
		
		if(has5a && (buf & 0xff) != 0x5a) {
			throw new IOException("cannot find 5a magic byte");
		}
		data[0] = 0x5a; // (byte) (buf & 0xff);
		
		buf = read(); offset++;
		if(buf == -1 && !has5a) {
		    return null; 
		}
		
		if(buf == -1) {
			throw new IOException("premature end of file.");
		}
		data[1] = (byte) (buf & 0xff);

		length = (byte) buf << 8;

		buf = read(); offset++;
		if(buf == -1)
			throw new IOException("premature end of file.");
		data[2] = (byte) (buf & 0xff);

		length |= (byte) buf & 0xff;
		
		length -= 2;
		
		if(length > data.length)
			throw new IOException("length of structured field is too large: "+length);
				
		int read = read(data, 3, length);
		offset += read;
		
		if(read < length)
			throw new IOException("premature end of file.");
		
		SF sf = factory.sf(data, 0, getLength() + 2);
		sf.setLength(length + 3);
		sf.setOffset(thisOffset);
		sf.setNumber(number++);
		
		return sf;
	}
796485
private static String readLine(InputStream is) throws IOException {
        StringBuilder builder = new StringBuilder();
        while (true) {
            int ch = is.read();
            if (ch == '\r') {
                ch = is.read();
                if (ch == '\n') {
                    break;
                } else {
                    throw new IOException("unexpected char after \\r: " + ch);
                }
            } else if (ch == -1) {
                if (builder.length() > 0) {
                    throw new IOException("unexpected end of stream");
                }
                break;
            }
            builder.append((char) ch);
        }
        return builder.toString();
    }
1093416
private String readRawLine() throws IOException {
    int b = inputStream.read();
    if (b == -1) {
      return null;
    } else {
      StringBuilder buffer = new StringBuilder();
      for (; b != -1; b = inputStream.read()) {
        buffer.append((char) b);
        if (b == '\n') {
          break;
        }
      }
      return buffer.toString();
    }
  }
970774
protected String readCRLFLine() throws IOException
	{
		int i = 0;
		while (i<1024)
		{
			byte aByte = (byte) read();
			if (aByte=='\n') break;
			if (aByte=='\r') continue;
			crlfBuffer[i++] = aByte;
		}
		return charsetDecoder.decode(ByteBuffer.wrap(crlfBuffer, 0, i)).toString();
	}
1173407
@Override
  public int read () throws IOException
  {
    // Do we need to get data?
    if (m_nPosition < 0)
    {
      if (m_bEncode)
      {
        final byte [] b3 = new byte [3];
        int numBinaryBytes = 0;
        for (int i = 0; i < 3; i++)
        {
          final int b = in.read ();

          // If end of stream, b is -1.
          if (b < 0)
            break;

          b3[i] = (byte) b;
          numBinaryBytes++;
        }

        if (numBinaryBytes > 0)
        {
          Base64._encode3to4 (b3, 0, numBinaryBytes, m_aBuffer, 0, m_nOptions);
          m_nPosition = 0;
          m_nNumSigBytes = 4;
        }
        else
        {
          return -1; // Must be end of stream
        }
      }
      else
      {
        // Else decoding
        final byte [] b4 = new byte [4];
        int i;
        for (i = 0; i < 4; i++)
        {
          // Read four "meaningful" bytes:
          int b;
          do
          {
            b = in.read ();
          } while (b >= 0 && m_aDecodabet[b & 0x7f] <= Base64.WHITE_SPACE_ENC);

          if (b < 0)
            break; // Reads a -1 if end of stream

          b4[i] = (byte) b;
        }

        if (i == 4)
        {
          m_nNumSigBytes = Base64._decode4to3 (b4, 0, m_aBuffer, 0, m_nOptions);
          m_nPosition = 0;
        }
        else
          if (i == 0)
          {
            return -1;
          }
          else
          {
            // Must have broken out from above.
            throw new IOException ("Improperly padded Base64 input.");
          } // end

      }
    }

    // Got data?
    if (m_nPosition >= 0)
    {
      if ( /* !encode && */m_nPosition >= m_nNumSigBytes)
      {
        return -1;
      }

      if (m_bEncode && m_bBreakLines && m_nLineLength >= Base64.MAX_LINE_LENGTH)
      {
        m_nLineLength = 0;
        return '\n';
      }
      {
        m_nLineLength++; // This isn't important when decoding
        // but throwing an extra "if" seems
        // just as wasteful.

        final int b = m_aBuffer[m_nPosition++];

        if (m_nPosition >= m_nBufferLength)
          m_nPosition = -1;

        return b & 0xFF; // This is how you "cast" a byte that's
                         // intended to be unsigned.
      }
    }

    // Else error
    throw new IOException ("Error in Base64 code reading stream.");
  }
184441
public byte[] read_integer_byte_array() throws OtpErlangDecodeException {
        int tag;
        byte[] nb;

        tag = read1skip_version();

        switch (tag) {
        case OtpExternal.smallIntTag:
            nb = new byte[2];
            nb[0] = 0;
            nb[1] = (byte) read1();
            break;

        case OtpExternal.intTag:
            nb = new byte[4];
            if (this.readN(nb) != 4) { // Big endian
                throw new OtpErlangDecodeException(
                        "Cannot read from intput stream");
            }
            break;

        case OtpExternal.smallBigTag:
        case OtpExternal.largeBigTag:
            int arity;
            int sign;
            if (tag == OtpExternal.smallBigTag) {
                arity = read1();
                sign = read1();
            } else {
                arity = read4BE();
                sign = read1();
                if (arity + 1 < 0) {
                    throw new OtpErlangDecodeException(
                            "Value of largeBig does not fit in BigInteger, arity "
                                    + arity + " sign " + sign);
                }
            }
            nb = new byte[arity + 1];
            // Value is read as little endian. The big end is augumented
            // with one zero byte to make the value 2's complement positive.
            if (this.readN(nb, 0, arity) != arity) {
                throw new OtpErlangDecodeException(
                        "Cannot read from intput stream");
            }
            // Reverse the array to make it big endian.
            for (int i = 0, j = nb.length; i < j--; i++) {
                // Swap [i] with [j]
                final byte b = nb[i];
                nb[i] = nb[j];
                nb[j] = b;
            }
            if (sign != 0) {
                // 2's complement negate the big endian value in the array
                int c = 1; // Carry
                for (int j = nb.length; j-- > 0;) {
                    c = (~nb[j] & 0xFF) + c;
                    nb[j] = (byte) c;
                    c >>= 8;
                }
            }
            break;

        default:
            throw new OtpErlangDecodeException("Not valid integer tag: " + tag);
        }

        return nb;
    }
1047705
private byte readByte()
            throws IOException
    {
        if (buffer.size() > position) {
            byte b = buffer.getByte(position);
            position++;
            return b;
        }
        else {
            nextBuffer();
            if (buffer.size() > 0) {
                byte b = buffer.getByte(0);
                position = 1;
                return b;
            }
            return readByte();
        }
    }
1037934
public byte[] getBitString() throws IOException {
        if (buffer.read() != DerValue.tag_BitString)
            throw new IOException("DER input not an bit string");

        return buffer.getBitString(getLength(buffer));
    }
900048
public int read2LE() throws OtpErlangDecodeException {
        final byte[] b = new byte[2];
        try {
            super.read(b);
        } catch (final IOException e) {
            throw new OtpErlangDecodeException("Cannot read from input stream");
        }
        return (b[1] << 8 & 0xff00) + (b[0] & 0xff);
    }