Tabnine Logo
Base64
Code IndexAdd Tabnine to your IDE (free)

How to use
Base64
in
org.apache.commons.codec.binary

Best Java code snippets using org.apache.commons.codec.binary.Base64 (Showing top 20 results out of 14,877)

origin: commons-codec/commons-codec

@Override
protected byte[] doDecoding(final byte[] bytes) {
  if (bytes == null) {
    return null;
  }
  return Base64.decodeBase64(bytes);
}
origin: commons-codec/commons-codec

@Override
protected byte[] doEncoding(final byte[] bytes) {
  if (bytes == null) {
    return null;
  }
  return Base64.encodeBase64(bytes);
}
origin: SonarSource/sonarqube

@Override
public String encrypt(String clearText) {
 return Base64.encodeBase64String(clearText.getBytes(StandardCharsets.UTF_8));
}
origin: commons-codec/commons-codec

/**
 * Decodes Base64 data into octets.
 * <p>
 * <b>Note:</b> this method seamlessly handles data encoded in URL-safe or normal mode.
 * </p>
 *
 * @param base64Data
 *            Byte array containing Base64 data
 * @return Array containing decoded data.
 */
public static byte[] decodeBase64(final byte[] base64Data) {
  return new Base64().decode(base64Data);
}
origin: commons-codec/commons-codec

@Test
public void testPairs() {
  assertEquals("AAA=", new String(Base64.encodeBase64(new byte[] { 0, 0 })));
  for (int i = -128; i <= 127; i++) {
    final byte test[] = { (byte) i, (byte) i };
    assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test))));
  }
}
origin: commons-codec/commons-codec

@Test
public void testEncodeDecodeRandom() {
  for (int i = 1; i < 5; i++) {
    final byte[] data = new byte[this.getRandom().nextInt(10000) + 1];
    this.getRandom().nextBytes(data);
    final byte[] enc = Base64.encodeBase64(data);
    assertTrue(Base64.isBase64(enc));
    final byte[] data2 = Base64.decodeBase64(enc);
    assertTrue(Arrays.equals(data, data2));
  }
}
origin: commons-codec/commons-codec

private void testEncodeDecode(final String plainText) {
  final String encodedText = Base64.encodeBase64String(StringUtils.getBytesUtf8(plainText));
  final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
  assertEquals(plainText, decodedText);
}
origin: commons-codec/commons-codec

@Test
public void testObjectEncodeWithValidParameter() throws Exception {
  final String original = "Hello World!";
  final Object origObj = original.getBytes(CHARSET_UTF8);
  final Base64 b64 = new Base64();
  final Object oEncoded = b64.encode(origObj);
  final byte[] bArray = Base64.decodeBase64((byte[]) oEncoded);
  final String dest = new String(bArray);
  assertEquals("dest string does not equal original", original, dest);
}
origin: commons-codec/commons-codec

@Test
public void testObjectDecodeWithValidParameter() throws Exception {
  final String original = "Hello World!";
  final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8));
  final Base64 b64 = new Base64();
  final Object oDecoded = b64.decode(o);
  final byte[] baDecoded = (byte[]) oDecoded;
  final String dest = new String(baDecoded);
  assertEquals("dest string does not equal original", original, dest);
}
origin: commons-codec/commons-codec

@Test
public void testByteToStringVariations() throws DecoderException {
  final Base64 base64 = new Base64(0);
  final byte[] b1 = StringUtils.getBytesUtf8("Hello World");
  final byte[] b2 = new byte[0];
  final byte[] b3 = null;
  final byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090"); // for
                                            // url-safe
                                            // tests
  assertEquals("byteToString Hello World", "SGVsbG8gV29ybGQ=", base64.encodeToString(b1));
  assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1));
  assertEquals("byteToString \"\"", "", base64.encodeToString(b2));
  assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2));
  assertEquals("byteToString null", null, base64.encodeToString(b3));
  assertEquals("byteToString static null", null, Base64.encodeBase64String(b3));
  assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4));
  assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", Base64.encodeBase64String(b4));
  assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA",
      Base64.encodeBase64URLSafeString(b4));
}
origin: commons-codec/commons-codec

@Test
public void testObjectEncodeWithInvalidParameter() throws Exception {
  final Base64 b64 = new Base64();
  try {
    b64.encode("Yadayadayada");
    fail("encode(Object) didn't throw an exception when passed a String object");
  } catch (final EncoderException e) {
    // Expected
  }
}
origin: auth0/java-jwt

  private String sign() throws SignatureGenerationException {
    String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
    String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));

    byte[] signatureBytes = algorithm.sign(header.getBytes(StandardCharsets.UTF_8), payload.getBytes(StandardCharsets.UTF_8));
    String signature = Base64.encodeBase64URLSafeString((signatureBytes));

    return String.format("%s.%s.%s", header, payload, signature);
  }
}
origin: commons-codec/commons-codec

/**
 * Creates a Base64InputStream such that all data read is either Base64-encoded or Base64-decoded from the original
 * provided InputStream.
 *
 * @param in
 *            InputStream to wrap.
 * @param doEncode
 *            true if we should encode all data read from us, false if we should decode.
 */
public Base64InputStream(final InputStream in, final boolean doEncode) {
  super(in, new Base64(false), doEncode);
}
origin: commons-codec/commons-codec

/**
 * Tests a lineSeparator much bigger than DEFAULT_BUFFER_SIZE.
 *
 * @see "<a href='http://mail-archives.apache.org/mod_mbox/commons-dev/201202.mbox/%3C4F3C85D7.5060706@snafu.de%3E'>dev@commons.apache.org</a>"
 */
@Test
@Ignore
public void testHugeLineSeparator() {
  final int BaseNCodec_DEFAULT_BUFFER_SIZE = 8192;
  final int Base64_BYTES_PER_ENCODED_BLOCK = 4;
  final byte[] baLineSeparator = new byte[BaseNCodec_DEFAULT_BUFFER_SIZE * 4 - 3];
  final Base64 b64 = new Base64(Base64_BYTES_PER_ENCODED_BLOCK, baLineSeparator);
  final String strOriginal = "Hello World";
  final String strDecoded = new String(b64.decode(b64.encode(StringUtils.getBytesUtf8(strOriginal))));
  assertEquals("testDEFAULT_BUFFER_SIZE", strOriginal, strDecoded);
}
origin: commons-codec/commons-codec

@Test
public void testNonBase64Test() throws Exception {
  final byte[] bArray = { '%' };
  assertFalse("Invalid Base64 array was incorrectly validated as " + "an array of Base64 encoded data",
      Base64.isBase64(bArray));
  try {
    final Base64 b64 = new Base64();
    final byte[] result = b64.decode(bArray);
    assertEquals("The result should be empty as the test encoded content did "
        + "not contain any valid base 64 characters", 0, result.length);
  } catch (final Exception e) {
    fail("Exception was thrown when trying to decode "
        + "invalid base64 encoded data - RFC 2045 requires that all "
        + "non base64 character be discarded, an exception should not" + " have been thrown");
  }
}
origin: commons-codec/commons-codec

/**
 * Tests isUrlSafe.
 */
@Test
public void testIsUrlSafe() {
  final Base64 base64Standard = new Base64(false);
  final Base64 base64URLSafe = new Base64(true);
  assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe());
  assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe());
  final byte[] whiteSpace = { ' ', '\n', '\r', '\t' };
  assertTrue("Base64.isBase64(whiteSpace)=true", Base64.isBase64(whiteSpace));
}
origin: commons-codec/commons-codec

@Test
public void testDecodeWithWhitespace() throws Exception {
  final String orig = "I am a late night coder.";
  final byte[] encodedArray = Base64.encodeBase64(orig.getBytes(CHARSET_UTF8));
  final StringBuilder intermediate = new StringBuilder(new String(encodedArray));
  intermediate.insert(2, ' ');
  intermediate.insert(5, '\t');
  intermediate.insert(10, '\r');
  intermediate.insert(15, '\n');
  final byte[] encodedWithWS = intermediate.toString().getBytes(CHARSET_UTF8);
  final byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
  final String dest = new String(decodedWithWS);
  assertEquals("Dest string doesn't equal the original", orig, dest);
}
origin: commons-codec/commons-codec

@Test
public void testEncodeDecodeSmall() {
  for (int i = 0; i < 12; i++) {
    final byte[] data = new byte[i];
    this.getRandom().nextBytes(data);
    final byte[] enc = Base64.encodeBase64(data);
    assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc));
    final byte[] data2 = Base64.decodeBase64(enc);
    assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2));
  }
}
origin: commons-codec/commons-codec

private void testDecodeEncode(final String encodedText) {
  final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
  final String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText));
  assertEquals(encodedText, encodedText2);
}
origin: commons-codec/commons-codec

@Test
public void testObjectEncode() throws Exception {
  final Base64 b64 = new Base64();
  assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(CHARSET_UTF8))));
}
org.apache.commons.codec.binaryBase64

Javadoc

Provides Base64 encoding and decoding as defined by RFC 2045.

This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.

Most used methods

  • decodeBase64
    Decodes Base64 data into octects
  • encodeBase64
    Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blo
  • encodeBase64String
    Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the b
  • <init>
    Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode. When e
  • encodeBase64URLSafeString
    Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output
  • decode
    Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least t
  • encode
    Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twi
  • encodeToString
  • isBase64
    Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Cur
  • encodeAsString
  • encodeBase64URLSafe
    Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output
  • encodeBase64Chunked
    Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character block
  • encodeBase64URLSafe,
  • encodeBase64Chunked,
  • isArrayByteBase64,
  • containsAlphabetOrPad,
  • ensureBufferSize,
  • getEncodedLength,
  • isWhiteSpace,
  • toIntegerBytes,
  • discardNonBase64,
  • discardWhitespace

Popular in Java

  • Making http requests using okhttp
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • getSharedPreferences (Context)
  • getExternalFilesDir (Context)
  • FlowLayout (java.awt)
    A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. F
  • BufferedImage (java.awt.image)
    The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All
  • NumberFormat (java.text)
    The abstract base class for all number formats. This class provides the interface for formatting and
  • Executor (java.util.concurrent)
    An object that executes submitted Runnable tasks. This interface provides a way of decoupling task s
  • TimeUnit (java.util.concurrent)
    A TimeUnit represents time durations at a given unit of granularity and provides utility methods to
  • JTextField (javax.swing)
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now