26import java.nio.ByteBuffer;
41 private final String name_;
42 private final int base_;
43 private final String symbols_;
44 private final char padding64_;
50 this.padding64_ = passing64;
54 public final String
name() {
return name_; }
57 public final int base() {
return base_; }
60 public final String
symbols() {
return symbols_; }
63 public final char padding64() {
return padding64_; }
69 public final char charAt(
final int cp ) {
return symbols().charAt(cp); }
72 public boolean equals(
final Object o) {
85 return "Alphabet["+name_+
", base <= "+base_+
"]";
108 private static final String data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
112 if (
'A' <= c && c <=
'Z') {
114 }
else if (
'a' <= c && c <=
'z') {
116 }
else if (
'0' <= c && c <=
'9') {
118 }
else if (
'+' == c) {
120 }
else if (
'/' == c) {
128 super(
"base64", 64, data,
'=');
153 private static final String data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
157 if (
'A' <= c && c <=
'Z') {
159 }
else if (
'a' <= c && c <=
'z') {
161 }
else if (
'0' <= c && c <=
'9') {
163 }
else if (
'-' == c) {
165 }
else if (
'_' == c) {
173 super(
"base64url", 64, data,
'=');
197 private static final String data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
201 if (
'0' <= c && c <=
'9') {
203 }
else if (
'a' <= c && c <=
'z') {
205 }
else if (
'A' <= c && c <=
'Z') {
207 }
else if (
'-' == c) {
209 }
else if (
'_' == c) {
217 super(
"natural64", 64, data,
'=');
241 private static final String data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_!#%&()+,/:;<=>?@[]^{}~";
245 if (
'0' <= c && c <=
'9') {
247 }
else if (
'a' <= c && c <=
'z') {
249 }
else if (
'A' <= c && c <=
'Z') {
283 super(
"natural86", 86, data, (
char)0);
308 private static final String data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
312 if (
'0' <= c && c <=
'9') {
314 }
else if (
'A' <= c && c <=
'Z') {
316 }
else if (
'-' == c) {
318 }
else if (
'_' == c) {
326 super(
"ascii38", 38, data,
'=');
351 private static final String data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
355 if (
'0' <= c && c <=
'9') {
357 }
else if (
'A' <= c && c <=
'Z') {
359 }
else if (
'a' <= c && c <=
'z') {
361 }
else if (
'-' == c) {
363 }
else if (
'_' == c) {
371 super(
"ascii64", 64, data,
'=');
393 private static final String data =
"!#%&()+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{}~";
397 if (
'0' <= c && c <=
'9') {
399 }
else if (
'A' <= c && c <=
'Z') {
401 }
else if (
'a' <= c && c <=
'z') {
438 super(
"ascii86", 86, data, (
char)0);
460 public static String
encode(
int num,
final Alphabet aspec,
final int min_width) {
461 final int base = aspec.
base();
462 if( 0 > num || 1 >= base ) {
465 final StringBuilder res =
new StringBuilder();
467 res.insert( 0, aspec.
charAt( num % base ) );
469 }
while ( 0 != num );
471 final char s0 = aspec.
charAt(0);
472 for(
int i=res.length(); i<min_width; ++i) {
475 return res.toString();
496 public static String
encode(
long num,
final Alphabet aspec,
final int min_width) {
497 final long base = aspec.
base();
498 if( 0 > num || 1 >= base ) {
501 final StringBuilder res =
new StringBuilder();
503 res.insert( 0, aspec.
charAt( (
int)( num % base ) ) );
505 }
while ( 0 != num );
507 final char s0 = aspec.
charAt(0);
508 for(
int i=res.length(); i<min_width; ++i) {
511 return res.toString();
532 return encode(num, aspec, 0 );
553 return encode(num, aspec, 0 );
574 final int base = aspec.
base();
578 final int str_len = str.length();
580 for (
int i = 0; i < str_len; ++i) {
581 final int d = aspec.
code_point( str.charAt(i) );
585 res = res * base + d;
590 private static int to_int(
final byte b) {
return b & 0xff; }
604 public static StringBuilder
encode64(
final byte[] in_octets,
int in_pos,
int in_len,
final Alphabet aspec) {
605 if( 64 != aspec.
base() || in_pos + in_len > in_octets.length ) {
606 return new StringBuilder(0);
610 final int out_len = ( in_len + 2 ) / 3 * 4;
611 final StringBuilder res =
new StringBuilder(out_len);
613 while( 0 < in_len && 0 < out_len ) {
617 res.append( aspec.
charAt( ( to_int(in_octets[in_pos+0]) >> 2 ) & 0x3f ) );
618 if( 0 == --in_len ) {
621 res.append( aspec.
charAt( ( to_int(in_octets[in_pos+0]) << 4 ) & 0x3f ) );
630 res.append( aspec.
charAt( ( ( to_int(in_octets[in_pos+0]) << 4 ) + ( to_int(in_octets[in_pos+1]) >> 4) ) & 0x3f ) );
632 if( 0 == --in_len ) {
635 res.append( aspec.
charAt( ( to_int(in_octets[in_pos+1]) << 2 ) & 0x3f ) );
643 res.append( aspec.
charAt( ( ( to_int(in_octets[in_pos+1]) << 2 ) + ( to_int(in_octets[in_pos+2]) >> 6) ) & 0x3f ) );
645 res.append( aspec.
charAt( to_int(in_octets[in_pos+2]) & 0x3f ) );
664 if( 64 != aspec.
base() ) {
665 return ByteBuffer.allocate(0);
667 int in_len = in_code.length();
669 return ByteBuffer.allocate(0);
673 final int out_len = 3 * ( in_len / 4 ) + 2;
674 final ByteBuffer res = ByteBuffer.allocate(out_len);
677 while( in_len >= 2 ) {
678 final int cp0 = aspec.
code_point( in_code.charAt( in_pos + 0 ) );
679 final int cp1 = aspec.
code_point( in_code.charAt( in_pos + 1 ) );
680 if( 0 > cp0 || 0 > cp1 ) {
683 res.put( (
byte)(cp0 << 2 | cp1 >> 4) );
690 if( padding == in_code.charAt( in_pos + 2 ) ) {
694 if( padding != in_code.charAt( in_pos + 3 ) ) {
698 final int cp2 = aspec.
code_point( in_code.charAt( in_pos + 2 ) );
702 res.put( (
byte)( ( ( cp1 << 4 ) & 0xf0 ) | ( cp2 >> 2 ) ) );
709 if( padding == in_code.charAt( in_pos + 3 ) ) {
714 final int cp3 = aspec.
code_point( in_code.charAt( in_pos + 3 ) );
718 res.put( (
byte)( ( ( cp2 << 6 ) & 0xc0 ) | cp3 ) );
740 public static int insert_lf(
final StringBuilder str,
final int period) {
742 for(
int i = period; i < str.length(); i += period + 1) {
758 pos = str.indexOf(
"\n", 0);
759 while( 0 < pos && pos <= str.length() ) {
760 str.replace(pos, pos+1,
"");
762 pos = str.indexOf(
"\n", pos);
779 public static StringBuilder
encode64_pem(
final byte[] in_octets,
final int in_pos,
final int in_len,
final Alphabet aspec) {
780 final StringBuilder e =
encode64(in_octets, in_pos, in_len, aspec);
797 public static StringBuilder
encode64_mime(
final byte[] in_octets,
final int in_pos,
final int in_len,
final Alphabet aspec) {
798 final StringBuilder e =
encode64(in_octets, in_pos, in_len, aspec);
815 final StringBuilder e =
new StringBuilder(str);
817 return decode64(e.toString(), aspec);
833 return decode64(str.toString(), aspec);
Base Alphabet Specification providing the alphabet for encode() and decode().
final String name()
Human readable name for this alphabet instance.
abstract int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
final char charAt(final int cp)
Retrieve the character at given code-point of this alphabet.
final char padding64()
Padding symbol for base <= 64 and block encoding only.
final int base()
The fixed base used for this alphabet.
boolean equals(final Object o)
final String symbols()
The string of symbols of this alphabet.
Alphabet(final String name, final int base, final String symbols, final char passing64)
Safe base 38 alphabet with ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Safe base 64 alphabet with ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Base 86 alphabet with ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Safe canonical base64 alphabet, without ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Safe canonical base64url alphabet, without ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Safe natural base 64 alphabet, both without ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
Natural base 86 alphabet, without ASCII code-point sorting order.
int code_point(final char c)
Returns the code-point of the given character or -1 if not element of this alphabet.
static StringBuilder encode64_pem(final byte[] in_octets, final int in_pos, final int in_len, final Alphabet aspec)
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
static StringBuilder encode64_mime(final byte[] in_octets, final int in_pos, final int in_len, final Alphabet aspec)
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
static String encode(long num, final Alphabet aspec, final int min_width)
Encodes a given positive decimal number to a symbolic string representing given alphabet and its base...
static ByteBuffer decode64_lf(final StringBuilder str, final Alphabet aspec)
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
static String encode(final long num, final Alphabet aspec)
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
static long decode(final String str, final Alphabet aspec)
Decodes a given symbolic string representing a given alphabet and its base to a positive decimal numb...
static String encode(int num, final Alphabet aspec, final int min_width)
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
static StringBuilder encode64(final byte[] in_octets, int in_pos, int in_len, final Alphabet aspec)
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
static int remove_lf(final StringBuilder str)
Removes line feed character from str.
static ByteBuffer decode64_lf(final String str, final Alphabet aspec)
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
static int insert_lf(final StringBuilder str, final int period)
Inserts a line feed (LF) character \n (ASCII 0x0a) after every period of characters.
static ByteBuffer decode64(final String in_code, final Alphabet aspec)
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
static String encode(final int num, final Alphabet aspec)
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...