public static String decode(String encoded) { if (encoded == null) { return null; } byte[] decoded = new Base64().decode(encoded.getBytes()); return new String(decoded); }
/** * Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the * Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[]. * * @param pObject * Object to decode * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied. * @throws DecoderException * if the parameter supplied is not of type byte[] */ public Object decode(Object pObject) throws DecoderException { if (!(pObject instanceof byte[])) { throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]"); } return decode((byte[]) pObject); }
@Nullable public static MessageReference parse(String identity) { if (identity == null || identity.length() < 1 || identity.charAt(0) != IDENTITY_VERSION_1) { return null; } StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false); if (tokens.countTokens() < 3) { return null; } String accountUuid = Base64.decode(tokens.nextToken()); String folderServerId = Base64.decode(tokens.nextToken()); String uid = Base64.decode(tokens.nextToken()); if (!tokens.hasMoreTokens()) { return new MessageReference(accountUuid, folderServerId, uid, null); } Flag flag; try { flag = Flag.valueOf(tokens.nextToken()); } catch (IllegalArgumentException e) { return null; } return new MessageReference(accountUuid, folderServerId, uid, flag); }
/** * Closes this output stream, flushing any remaining bytes that must be encoded. The * underlying stream is flushed but not closed. */ @Override public void close() throws IOException { // Notify encoder of EOF (-1). if (doEncode) { base64.encode(singleByte, 0, -1); } else { base64.decode(singleByte, 0, -1); } flush(); }
/** * Decodes Base64 data into octets * * @param base64Data Byte array containing Base64 data * @return Array containing decoded data. */ public static byte[] decodeBase64(byte[] base64Data) { if (base64Data == null || base64Data.length == 0) { return base64Data; } Base64 b64 = new Base64(); long len = (base64Data.length * 3) / 4; byte[] buf = new byte[(int) len]; b64.setInitialBuffer(buf, 0, buf.length); b64.decode(base64Data, 0, base64Data.length); b64.decode(base64Data, 0, -1); // Notify decoder of EOF. // We have no idea what the line-length was, so we // cannot know how much of our array wasn't used. byte[] result = new byte[b64.pos]; b64.readResults(result, 0, result.length); return result; }
public static boolean shouldRetry(String response, String host) { String decodedResponse = Base64.decode(response); if (K9MailLib.isDebug()) { Timber.v("Challenge response: %s", decodedResponse); } try { JSONObject json = new JSONObject(decodedResponse); String status = json.getString("status"); if (!BAD_RESPONSE.equals(status)) { return false; } } catch (JSONException jsonException) { Timber.e("Error decoding JSON response from: %s. Response was: %s", host, decodedResponse); } return true; } }
/** * Writes <code>len</code> bytes from the specified * <code>b</code> array starting at <code>offset</code> to * this output stream. * * @param b source byte array * @param offset where to start reading the bytes * @param len maximum number of bytes to write * * @throws IOException if an I/O error occurs. * @throws NullPointerException if the byte array parameter is null * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid */ @Override public void write(byte b[], int offset, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (offset < 0 || len < 0 || offset + len < 0) { throw new IndexOutOfBoundsException(); } else if (offset > b.length || offset + len > b.length) { throw new IndexOutOfBoundsException(); } else if (len > 0) { if (doEncode) { base64.encode(b, offset, len); } else { base64.decode(b, offset, len); } flush(false); } }
String bodyLengthS = Base64.decode(tokenizer.nextToken()); try { identity.put(IdentityField.LENGTH, Integer.valueOf(bodyLengthS).toString()); identity.put(IdentityField.SIGNATURE, Base64.decode(tokenizer.nextToken())); identity.put(IdentityField.NAME, Base64.decode(tokenizer.nextToken())); identity.put(IdentityField.EMAIL, Base64.decode(tokenizer.nextToken())); identity.put(IdentityField.QUOTED_TEXT_MODE, Base64.decode(tokenizer.nextToken()));