/** * Determines whether the data in the given byte array is base64-encoded data of PEM encoding. The determination is * made using as little data from the given array as necessary to make a reasonable determination about encoding. * * @param data Data to test for PEM encoding * * @return True if data appears to be PEM encoded, false otherwise. */ public static boolean isPem(final byte[] data) { final String start = new String(data, 0, 10, ByteUtil.ASCII_CHARSET).trim(); if (!start.startsWith(HEADER_BEGIN) && !start.startsWith(PROC_TYPE)) { // Check all bytes in first line to make sure they are in the range // of base64 character set encoding for (int i = 0; i < LINE_LENGTH; i++) { if (!isBase64Char(data[i])) { // Last two bytes may be padding character '=' (61) if (i > LINE_LENGTH - 3) { if (data[i] != 61) { return false; } } else { return false; } } } } return true; }