keycloak
クラス | 静的公開メンバ関数 | 静的公開変数類 | 非公開メンバ関数 | 静的非公開メンバ関数 | 静的非公開変数類 | 全メンバ一覧
org.keycloak.common.util.Base64 クラス
org.keycloak.common.util.Base64 連携図
Collaboration graph

クラス

class  InputStream
 
class  OutputStream
 

静的公開メンバ関数

static void encode (java.nio.ByteBuffer raw, java.nio.ByteBuffer encoded)
 
static void encode (java.nio.ByteBuffer raw, java.nio.CharBuffer encoded)
 
static String encodeObject (java.io.Serializable serializableObject) throws java.io.IOException
 
static String encodeObject (java.io.Serializable serializableObject, int options) throws java.io.IOException
 
static String encodeBytes (byte[] source)
 
static String encodeBytes (byte[] source, int options) throws java.io.IOException
 
static String encodeBytes (byte[] source, int off, int len)
 
static String encodeBytes (byte[] source, int off, int len, int options) throws java.io.IOException
 
static byte [] encodeBytesToBytes (byte[] source)
 
static byte [] encodeBytesToBytes (byte[] source, int off, int len, int options) throws java.io.IOException
 
static byte [] decode (byte[] source) throws java.io.IOException
 
static byte [] decode (byte[] source, int off, int len, int options) throws java.io.IOException
 
static byte [] decode (String s) throws java.io.IOException
 
static byte [] decode (String s, int options) throws java.io.IOException
 

静的公開変数類

static final int NO_OPTIONS = 0
 
static final int ENCODE = 1
 
static final int DECODE = 0
 
static final int GZIP = 2
 
static final int DONT_GUNZIP = 4
 
static final int DO_BREAK_LINES = 8
 
static final int URL_SAFE = 16
 
static final int ORDERED = 32
 

非公開メンバ関数

 Base64 ()
 

静的非公開メンバ関数

static final byte [] getAlphabet (int options)
 
static final byte [] getDecodabet (int options)
 
static byte [] encode3to4 (byte[] b4, byte[] threeBytes, int numSigBytes, int options)
 
static byte [] encode3to4 (byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, int options)
 
static int decode4to3 (byte[] source, int srcOffset, byte[] destination, int destOffset, int options)
 

静的非公開変数類

static final int MAX_LINE_LENGTH = 76
 
static final byte EQUALS_SIGN = (byte)'='
 
static final byte NEW_LINE = (byte)'\n'
 
static final String PREFERRED_ENCODING = "US-ASCII"
 
static final byte WHITE_SPACE_ENC = -5
 
static final byte EQUALS_SIGN_ENC = -1
 
static final byte [] _STANDARD_ALPHABET
 
static final byte [] _STANDARD_DECODABET
 
static final byte [] _URL_SAFE_ALPHABET
 
static final byte [] _URL_SAFE_DECODABET
 
static final byte [] _ORDERED_ALPHABET
 
static final byte [] _ORDERED_DECODABET
 

詳解

Encodes and decodes to and from Base64 notation.

Homepage: http://iharder.net/base64.

Example:

String encoded = Base64.encode( myByteArray );
byte[] myByteArray = Base64.decode( encoded );

The options parameter, which appears in a few places, is used to pass several pieces of information to the encoder. In the "higher level" methods such as encodeBytes( bytes, options ) the options parameter can be used to indicate such things as first gzipping the bytes before encoding them, not inserting linefeeds, and encoding using the URL-safe and Ordered dialects.

Note, according to RFC3548, Section 2.1, implementations should not add line feeds unless explicitly told to do so. I've got Base64 set to this behavior now, although earlier versions broke lines by default.

The constants defined in Base64 can be OR-ed together to combine options, so you might make a call like this:

String encoded = Base64.encodeBytes( mybytes, Base64.GZIP | Base64.DO_BREAK_LINES );

to compress the data before encoding it and then making the output have newline characters.

Also...

String encoded = Base64.encodeBytes( crazyString.getBytes() );

Change Log:

I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/base64 periodically to check for updates or to contribute improvements.

著者
Robert Harder
rob@i.nosp@m.hard.nosp@m.er.ne.nosp@m.t
バージョン
2.3.7

構築子と解体子

◆ Base64()

org.keycloak.common.util.Base64.Base64 ( )
inlineprivate

Defeats instantiation.

443 {}

関数詳解

◆ decode() [1/4]

static byte [] org.keycloak.common.util.Base64.decode ( byte []  source) throws java.io.IOException
inlinestatic

Low-level access to decoding ASCII characters in the form of a byte array. Ignores GUNZIP option, if it's set. This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint (and aren't gzipping), consider this method.

引数
sourceThe Base64 encoded data
戻り値
decoded data
から
2.3.1
1119  {
1120  byte[] decoded = null;
1121 // try {
1122  decoded = decode( source, 0, source.length, Base64.NO_OPTIONS );
1123 // } catch( java.io.IOException ex ) {
1124 // assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage();
1125 // }
1126  return decoded;
1127  }
Base64()
Definition: Base64.java:443
static byte [] decode(byte[] source)
Definition: Base64.java:1118

◆ decode() [2/4]

static byte [] org.keycloak.common.util.Base64.decode ( byte []  source,
int  off,
int  len,
int  options 
) throws java.io.IOException
inlinestatic

Low-level access to decoding ASCII characters in the form of a byte array. Ignores GUNZIP option, if it's set. This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint (and aren't gzipping), consider this method.

引数
sourceThe Base64 encoded data
offThe offset of where to begin decoding
lenThe length of characters to decode
optionsCan specify options such as alphabet type to use
戻り値
decoded data
例外
java.io.IOExceptionIf bogus characters exist in source data
から
1.3
1149  {
1150 
1151  // Lots of error checking and exception throwing
1152  if( source == null ){
1153  throw new NullPointerException( "Cannot decode null source array." );
1154  } // end if
1155  if( off < 0 || off + len > source.length ){
1156  throw new IllegalArgumentException( String.format(
1157  "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len ) );
1158  } // end if
1159 
1160  if( len == 0 ){
1161  return new byte[0];
1162  }else if( len < 4 ){
1163  throw new IllegalArgumentException(
1164  "Base64-encoded string must have at least four characters, but length specified was " + len );
1165  } // end if
1166 
1167  byte[] DECODABET = getDecodabet( options );
1168 
1169  int len34 = len * 3 / 4; // Estimate on array size
1170  byte[] outBuff = new byte[ len34 ]; // Upper limit on size of output
1171  int outBuffPosn = 0; // Keep track of where we're writing
1172 
1173  byte[] b4 = new byte[4]; // Four byte buffer from source, eliminating white space
1174  int b4Posn = 0; // Keep track of four byte input buffer
1175  int i = 0; // Source array counter
1176  byte sbiDecode = 0; // Special value from DECODABET
1177 
1178  for( i = off; i < off+len; i++ ) { // Loop through source
1179 
1180  sbiDecode = DECODABET[ source[i]&0xFF ];
1181 
1182  // White space, Equals sign, or legit Base64 character
1183  // Note the values such as -5 and -9 in the
1184  // DECODABETs at the top of the file.
1185  if( sbiDecode >= WHITE_SPACE_ENC ) {
1186  if( sbiDecode >= EQUALS_SIGN_ENC ) {
1187  b4[ b4Posn++ ] = source[i]; // Save non-whitespace
1188  if( b4Posn > 3 ) { // Time to decode?
1189  outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options );
1190  b4Posn = 0;
1191 
1192  // If that was the equals sign, break out of 'for' loop
1193  if( source[i] == EQUALS_SIGN ) {
1194  break;
1195  } // end if: equals sign
1196  } // end if: quartet built
1197  } // end if: equals sign or better
1198  } // end if: white space, equals sign or better
1199  else {
1200  // There's a bad input character in the Base64 stream.
1201  throw new java.io.IOException( String.format(
1202  "Bad Base64 input character decimal %d in array position %d", ((int)source[i])&0xFF, i ) );
1203  } // end else:
1204  } // each input character
1205 
1206  byte[] out = new byte[ outBuffPosn ];
1207  System.arraycopy( outBuff, 0, out, 0, outBuffPosn );
1208  return out;
1209  } // end decode
static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset, int options)
Definition: Base64.java:1030
static final byte EQUALS_SIGN_ENC
Definition: Base64.java:215
static final byte EQUALS_SIGN
Definition: Base64.java:203
static final byte [] getDecodabet(int options)
Definition: Base64.java:430
static final byte WHITE_SPACE_ENC
Definition: Base64.java:214

◆ decode() [3/4]

static byte [] org.keycloak.common.util.Base64.decode ( String  s) throws java.io.IOException
inlinestatic

Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.

引数
sthe string to decode
戻り値
the decoded data
例外
java.io.IOExceptionIf there is a problem
から
1.4
1223  {
1224  return decode( s, NO_OPTIONS );
1225  }
static byte [] decode(byte[] source)
Definition: Base64.java:1118
static final int NO_OPTIONS
Definition: Base64.java:157

◆ decode() [4/4]

static byte [] org.keycloak.common.util.Base64.decode ( String  s,
int  options 
) throws java.io.IOException
inlinestatic

Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.

引数
sthe string to decode
optionsencode options such as URL_SAFE
戻り値
the decoded data
例外
java.io.IOExceptionif there is an error
NullPointerExceptionif s is null
から
1.4
1240  {
1241 
1242  if( s == null ){
1243  throw new NullPointerException( "Input string was null." );
1244  } // end if
1245 
1246  byte[] bytes;
1247  try {
1248  bytes = s.getBytes( PREFERRED_ENCODING );
1249  } // end try
1250  catch( java.io.UnsupportedEncodingException uee ) {
1251  bytes = s.getBytes();
1252  } // end catch
1253  //</change>
1254 
1255  // Decode
1256  bytes = decode( bytes, 0, bytes.length, options );
1257 
1258  // Check to see if it's gzip-compressed
1259  // GZIP Magic Two-Byte Number: 0x8b1f (35615)
1260  boolean dontGunzip = (options & DONT_GUNZIP) != 0;
1261  if( (bytes != null) && (bytes.length >= 4) && (!dontGunzip) ) {
1262 
1263  int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
1264  if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head ) {
1265  java.io.ByteArrayInputStream bais = null;
1266  java.util.zip.GZIPInputStream gzis = null;
1267  java.io.ByteArrayOutputStream baos = null;
1268  byte[] buffer = new byte[2048];
1269  int length = 0;
1270 
1271  try {
1272  baos = new java.io.ByteArrayOutputStream();
1273  bais = new java.io.ByteArrayInputStream( bytes );
1274  gzis = new java.util.zip.GZIPInputStream( bais );
1275 
1276  while( ( length = gzis.read( buffer ) ) >= 0 ) {
1277  baos.write(buffer,0,length);
1278  } // end while: reading input
1279 
1280  // No error? Get new bytes.
1281  bytes = baos.toByteArray();
1282 
1283  } // end try
1284  catch( java.io.IOException e ) {
1285  if (e.getMessage().equals("Unsupported compression method")) {
1286  System.out.println("Base64 decoding: Ignoring GZIP header and just returning originally-decoded bytes."); // Better to log as debug, but jboss logging not available in the module :/
1287  } else {
1288  e.printStackTrace();
1289  }
1290 
1291  // Just return originally-decoded bytes
1292  } // end catch
1293  finally {
1294  try{ baos.close(); } catch( Exception e ){}
1295  try{ gzis.close(); } catch( Exception e ){}
1296  try{ bais.close(); } catch( Exception e ){}
1297  } // end finally
1298 
1299  } // end if: gzipped
1300  } // end if: bytes.length >= 2
1301 
1302  return bytes;
1303  } // end decode
static byte [] decode(byte[] source)
Definition: Base64.java:1118
static final String PREFERRED_ENCODING
Definition: Base64.java:211
static final int DONT_GUNZIP
Definition: Base64.java:171

◆ decode4to3()

static int org.keycloak.common.util.Base64.decode4to3 ( byte []  source,
int  srcOffset,
byte []  destination,
int  destOffset,
int  options 
)
inlinestaticprivate

Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 4 for the source array or destOffset + 3 for the destination array. This method returns the actual number of bytes that were converted from the Base64 encoding.

This is the lowest level of the decoding methods with all possible parameters.

引数
sourcethe array to convert
srcOffsetthe index where conversion begins
destinationthe array to hold the conversion
destOffsetthe index where output will be put
optionsalphabet type is pulled from this (standard, url-safe, ordered)
戻り値
the number of decoded bytes converted
例外
NullPointerExceptionif source or destination arrays are null
IllegalArgumentExceptionif srcOffset or destOffset are invalid or there is not enough room in the array.
から
1.3
1032  {
1033 
1034  // Lots of error checking and exception throwing
1035  if( source == null ){
1036  throw new NullPointerException( "Source array was null." );
1037  } // end if
1038  if( destination == null ){
1039  throw new NullPointerException( "Destination array was null." );
1040  } // end if
1041  if( srcOffset < 0 || srcOffset + 3 >= source.length ){
1042  throw new IllegalArgumentException( String.format(
1043  "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset ) );
1044  } // end if
1045  if( destOffset < 0 || destOffset +2 >= destination.length ){
1046  throw new IllegalArgumentException( String.format(
1047  "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset ) );
1048  } // end if
1049 
1050 
1051  byte[] DECODABET = getDecodabet( options );
1052 
1053  // Example: Dk==
1054  if( source[ srcOffset + 2] == EQUALS_SIGN ) {
1055  // Two ways to do the same thing. Don't know which way I like best.
1056  //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
1057  // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
1058  int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
1059  | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 );
1060 
1061  destination[ destOffset ] = (byte)( outBuff >>> 16 );
1062  return 1;
1063  }
1064 
1065  // Example: DkL=
1066  else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {
1067  // Two ways to do the same thing. Don't know which way I like best.
1068  //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
1069  // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
1070  // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
1071  int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
1072  | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
1073  | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 );
1074 
1075  destination[ destOffset ] = (byte)( outBuff >>> 16 );
1076  destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 );
1077  return 2;
1078  }
1079 
1080  // Example: DkLE
1081  else {
1082  // Two ways to do the same thing. Don't know which way I like best.
1083  //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
1084  // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
1085  // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
1086  // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
1087  int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
1088  | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
1089  | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6)
1090  | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) );
1091 
1092 
1093  destination[ destOffset ] = (byte)( outBuff >> 16 );
1094  destination[ destOffset + 1 ] = (byte)( outBuff >> 8 );
1095  destination[ destOffset + 2 ] = (byte)( outBuff );
1096 
1097  return 3;
1098  }
1099  } // end decodeToBytes
static final byte EQUALS_SIGN
Definition: Base64.java:203
static final byte [] getDecodabet(int options)
Definition: Base64.java:430

◆ encode() [1/2]

static void org.keycloak.common.util.Base64.encode ( java.nio.ByteBuffer  raw,
java.nio.ByteBuffer  encoded 
)
inlinestatic

Performs Base64 encoding on the raw ByteBuffer, writing it to the encoded ByteBuffer. This is an experimental feature. Currently it does not pass along any options (such as DO_BREAK_LINES or GZIP.

引数
rawinput buffer
encodedoutput buffer
から
2.3
557  {
558  byte[] raw3 = new byte[3];
559  byte[] enc4 = new byte[4];
560 
561  while( raw.hasRemaining() ){
562  int rem = Math.min(3,raw.remaining());
563  raw.get(raw3,0,rem);
564  Base64.encode3to4(enc4, raw3, rem, Base64.NO_OPTIONS);
565  encoded.put(enc4);
566  } // end input remaining
567  }
Base64()
Definition: Base64.java:443

◆ encode() [2/2]

static void org.keycloak.common.util.Base64.encode ( java.nio.ByteBuffer  raw,
java.nio.CharBuffer  encoded 
)
inlinestatic

Performs Base64 encoding on the raw ByteBuffer, writing it to the encoded CharBuffer. This is an experimental feature. Currently it does not pass along any options (such as DO_BREAK_LINES or GZIP.

引数
rawinput buffer
encodedoutput buffer
から
2.3
581  {
582  byte[] raw3 = new byte[3];
583  byte[] enc4 = new byte[4];
584 
585  while( raw.hasRemaining() ){
586  int rem = Math.min(3,raw.remaining());
587  raw.get(raw3,0,rem);
588  Base64.encode3to4(enc4, raw3, rem, Base64.NO_OPTIONS );
589  for( int i = 0; i < 4; i++ ){
590  encoded.put( (char)(enc4[i] & 0xFF) );
591  }
592  } // end input remaining
593  }
Base64()
Definition: Base64.java:443

◆ encode3to4() [1/2]

static byte [] org.keycloak.common.util.Base64.encode3to4 ( byte []  b4,
byte []  threeBytes,
int  numSigBytes,
int  options 
)
inlinestaticprivate

Encodes up to the first three bytes of array threeBytes and returns a four-byte array in Base64 notation. The actual number of significant bytes in your array is given by numSigBytes. The array threeBytes needs only be as big as numSigBytes. Code can reuse a byte array by passing a four-byte array as b4.

引数
b4A reusable byte array to reduce array instantiation
threeBytesthe array to convert
numSigBytesthe number of significant bytes in your array
戻り値
four byte array in Base64 notation.
から
1.5.1
466  {
467  encode3to4( threeBytes, 0, numSigBytes, b4, 0, options );
468  return b4;
469  } // end encode3to4
static byte [] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes, int options)
Definition: Base64.java:466

◆ encode3to4() [2/2]

static byte [] org.keycloak.common.util.Base64.encode3to4 ( byte []  source,
int  srcOffset,
int  numSigBytes,
byte []  destination,
int  destOffset,
int  options 
)
inlinestaticprivate

Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes.

This is the lowest level of the encoding methods with all possible parameters.

引数
sourcethe array to convert
srcOffsetthe index where conversion begins
numSigBytesthe number of significant bytes in your array
destinationthe array to hold the conversion
destOffsetthe index where output will be put
戻り値
the destination array
から
1.3
497  {
498 
499  byte[] ALPHABET = getAlphabet( options );
500 
501  // 1 2 3
502  // 01234567890123456789012345678901 Bit position
503  // --------000000001111111122222222 Array position from threeBytes
504  // --------| || || || | Six bit groups to index ALPHABET
505  // >>18 >>12 >> 6 >> 0 Right shift necessary
506  // 0x3f 0x3f 0x3f Additional AND
507 
508  // Create buffer with zero-padding if there are only one or two
509  // significant bytes passed in the array.
510  // We have to shift left 24 in order to flush out the 1's that appear
511  // when Java treats a value as negative that is cast from a byte to an int.
512  int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
513  | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
514  | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
515 
516  switch( numSigBytes )
517  {
518  case 3:
519  destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
520  destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
521  destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
522  destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
523  return destination;
524 
525  case 2:
526  destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
527  destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
528  destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
529  destination[ destOffset + 3 ] = EQUALS_SIGN;
530  return destination;
531 
532  case 1:
533  destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
534  destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
535  destination[ destOffset + 2 ] = EQUALS_SIGN;
536  destination[ destOffset + 3 ] = EQUALS_SIGN;
537  return destination;
538 
539  default:
540  return destination;
541  } // end switch
542  } // end encode3to4
static final byte [] getAlphabet(int options)
Definition: Base64.java:412
static final byte EQUALS_SIGN
Definition: Base64.java:203

◆ encodeBytes() [1/4]

static String org.keycloak.common.util.Base64.encodeBytes ( byte []  source)
inlinestatic

Encodes a byte array into Base64 notation. Does not GZip-compress data.

引数
sourceThe data to convert
戻り値
The data in Base64-encoded form
例外
NullPointerExceptionif source array is null
から
1.4
714  {
715  // Since we're not going to have the GZIP encoding turned on,
716  // we're not going to have an java.io.IOException thrown, so
717  // we should not force the user to have to catch it.
718  String encoded = null;
719  try {
720  encoded = encodeBytes(source, 0, source.length, NO_OPTIONS);
721  } catch (java.io.IOException ex) {
722  assert false : ex.getMessage();
723  } // end catch
724  assert encoded != null;
725  return encoded;
726  } // end encodeBytes
static final int NO_OPTIONS
Definition: Base64.java:157
static String encodeBytes(byte[] source)
Definition: Base64.java:714

◆ encodeBytes() [2/4]

static String org.keycloak.common.util.Base64.encodeBytes ( byte []  source,
int  options 
) throws java.io.IOException
inlinestatic

Encodes a byte array into Base64 notation.

Example options:

  GZIP: gzip-compresses object before encoding it.
  DO_BREAK_LINES: break lines at 76 characters
    Note: Technically, this makes your encoding non-compliant.

Example: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )

As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.

引数
sourceThe data to convert
optionsSpecified options
戻り値
The Base64-encoded data as a String
参照
Base64::GZIP
Base64::DO_BREAK_LINES
例外
java.io.IOExceptionif there is an error
NullPointerExceptionif source array is null
から
2.0
759  {
760  return encodeBytes( source, 0, source.length, options );
761  } // end encodeBytes
static String encodeBytes(byte[] source)
Definition: Base64.java:714

◆ encodeBytes() [3/4]

static String org.keycloak.common.util.Base64.encodeBytes ( byte []  source,
int  off,
int  len 
)
inlinestatic

Encodes a byte array into Base64 notation. Does not GZip-compress data.

As of v 2.3, if there is an error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.

引数
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
戻り値
The Base64-encoded data as a String
例外
NullPointerExceptionif source array is null
IllegalArgumentExceptionif source array, offset, or length are invalid
から
1.4
782  {
783  // Since we're not going to have the GZIP encoding turned on,
784  // we're not going to have an java.io.IOException thrown, so
785  // we should not force the user to have to catch it.
786  String encoded = null;
787  try {
788  encoded = encodeBytes( source, off, len, NO_OPTIONS );
789  } catch (java.io.IOException ex) {
790  assert false : ex.getMessage();
791  } // end catch
792  assert encoded != null;
793  return encoded;
794  } // end encodeBytes
static final int NO_OPTIONS
Definition: Base64.java:157
static String encodeBytes(byte[] source)
Definition: Base64.java:714

◆ encodeBytes() [4/4]

static String org.keycloak.common.util.Base64.encodeBytes ( byte []  source,
int  off,
int  len,
int  options 
) throws java.io.IOException
inlinestatic

Encodes a byte array into Base64 notation.

Example options:

  GZIP: gzip-compresses object before encoding it.
  DO_BREAK_LINES: break lines at 76 characters
    Note: Technically, this makes your encoding non-compliant.

Example: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )

As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.

引数
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
optionsSpecified options
戻り値
The Base64-encoded data as a String
参照
Base64::GZIP
Base64::DO_BREAK_LINES
例外
java.io.IOExceptionif there is an error
NullPointerExceptionif source array is null
IllegalArgumentExceptionif source array, offset, or length are invalid
から
2.0
830  {
831  byte[] encoded = encodeBytesToBytes( source, off, len, options );
832 
833  // Return value according to relevant encoding.
834  try {
835  return new String( encoded, PREFERRED_ENCODING );
836  } // end try
837  catch (java.io.UnsupportedEncodingException uue) {
838  return new String( encoded );
839  } // end catch
840 
841  } // end encodeBytes
static final String PREFERRED_ENCODING
Definition: Base64.java:211
static byte [] encodeBytesToBytes(byte[] source)
Definition: Base64.java:857

◆ encodeBytesToBytes() [1/2]

static byte [] org.keycloak.common.util.Base64.encodeBytesToBytes ( byte []  source)
inlinestatic

Similar to encodeBytes(byte[]) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.

引数
sourceThe data to convert
戻り値
The Base64-encoded data as a byte[] (of ASCII characters)
例外
NullPointerExceptionif source array is null
から
2.3.1
857  {
858  byte[] encoded = null;
859  try {
860  encoded = encodeBytesToBytes( source, 0, source.length, Base64.NO_OPTIONS );
861  } catch( java.io.IOException ex ) {
862  assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage();
863  }
864  return encoded;
865  }
Base64()
Definition: Base64.java:443
static byte [] encodeBytesToBytes(byte[] source)
Definition: Base64.java:857

◆ encodeBytesToBytes() [2/2]

static byte [] org.keycloak.common.util.Base64.encodeBytesToBytes ( byte []  source,
int  off,
int  len,
int  options 
) throws java.io.IOException
inlinestatic

Similar to encodeBytes(byte[], int, int, int) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.

引数
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
optionsSpecified options
戻り値
The Base64-encoded data as a String
参照
Base64::GZIP
Base64::DO_BREAK_LINES
例外
java.io.IOExceptionif there is an error
NullPointerExceptionif source array is null
IllegalArgumentExceptionif source array, offset, or length are invalid
から
2.3.1
886  {
887 
888  if( source == null ){
889  throw new NullPointerException( "Cannot serialize a null array." );
890  } // end if: null
891 
892  if( off < 0 ){
893  throw new IllegalArgumentException( "Cannot have negative offset: " + off );
894  } // end if: off < 0
895 
896  if( len < 0 ){
897  throw new IllegalArgumentException( "Cannot have length offset: " + len );
898  } // end if: len < 0
899 
900  if( off + len > source.length ){
901  throw new IllegalArgumentException(
902  String.format( "Cannot have offset of %d and length of %d with array of length %d", off,len,source.length));
903  } // end if: off < 0
904 
905 
906 
907  // Compress?
908  if( (options & GZIP) != 0 ) {
909  java.io.ByteArrayOutputStream baos = null;
910  java.util.zip.GZIPOutputStream gzos = null;
911  Base64.OutputStream b64os = null;
912 
913  try {
914  // GZip -> Base64 -> ByteArray
915  baos = new java.io.ByteArrayOutputStream();
916  b64os = new Base64.OutputStream( baos, ENCODE | options );
917  gzos = new java.util.zip.GZIPOutputStream( b64os );
918 
919  gzos.write( source, off, len );
920  gzos.close();
921  } // end try
922  catch( java.io.IOException e ) {
923  // Catch it and then throw it immediately so that
924  // the finally{} block is called for cleanup.
925  throw e;
926  } // end catch
927  finally {
928  try{ gzos.close(); } catch( Exception e ){}
929  try{ b64os.close(); } catch( Exception e ){}
930  try{ baos.close(); } catch( Exception e ){}
931  } // end finally
932 
933  return baos.toByteArray();
934  } // end if: compress
935 
936  // Else, don't compress. Better not to use streams at all then.
937  else {
938  boolean breakLines = (options & DO_BREAK_LINES) != 0;
939 
940  //int len43 = len * 4 / 3;
941  //byte[] outBuff = new byte[ ( len43 ) // Main 4:3
942  // + ( (len % 3) > 0 ? 4 : 0 ) // Account for padding
943  // + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; // New lines
944  // Try to determine more precisely how big the array needs to be.
945  // If we get it right, we don't have to do an array copy, and
946  // we save a bunch of memory.
947  int encLen = ( len / 3 ) * 4 + ( len % 3 > 0 ? 4 : 0 ); // Bytes needed for actual encoding
948  if( breakLines ){
949  encLen += encLen / MAX_LINE_LENGTH; // Plus extra newline characters
950  }
951  byte[] outBuff = new byte[ encLen ];
952 
953 
954  int d = 0;
955  int e = 0;
956  int len2 = len - 2;
957  int lineLength = 0;
958  for( ; d < len2; d+=3, e+=4 ) {
959  encode3to4( source, d+off, 3, outBuff, e, options );
960 
961  lineLength += 4;
962  if( breakLines && lineLength >= MAX_LINE_LENGTH )
963  {
964  outBuff[e+4] = NEW_LINE;
965  e++;
966  lineLength = 0;
967  } // end if: end of line
968  } // en dfor: each piece of array
969 
970  if( d < len ) {
971  encode3to4( source, d+off, len - d, outBuff, e, options );
972  e += 4;
973  } // end if: some padding needed
974 
975 
976  // Only resize array if we didn't guess it right.
977  if( e <= outBuff.length - 1 ){
978  // If breaking lines and the last byte falls right at
979  // the line length (76 bytes per line), there will be
980  // one extra byte, and the array will need to be resized.
981  // Not too bad of an estimate on array size, I'd say.
982  byte[] finalOut = new byte[e];
983  System.arraycopy(outBuff,0, finalOut,0,e);
984  //System.err.println("Having to resize array from " + outBuff.length + " to " + e );
985  return finalOut;
986  } else {
987  //System.err.println("No need to resize array.");
988  return outBuff;
989  }
990 
991  } // end else: don't compress
992 
993  } // end encodeBytesToBytes
static final int GZIP
Definition: Base64.java:168
static final byte NEW_LINE
Definition: Base64.java:207
Base64()
Definition: Base64.java:443
static byte [] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes, int options)
Definition: Base64.java:466
static final int DO_BREAK_LINES
Definition: Base64.java:175
static final int ENCODE
Definition: Base64.java:160
static final int MAX_LINE_LENGTH
Definition: Base64.java:199

◆ encodeObject() [1/2]

static String org.keycloak.common.util.Base64.encodeObject ( java.io.Serializable  serializableObject) throws java.io.IOException
inlinestatic

Serializes an object and returns the Base64-encoded version of that serialized object.

As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.

The object is not GZip-compressed before being encoded.

引数
serializableObjectThe object to encode
戻り値
The Base64-encoded object
例外
java.io.IOExceptionif there is an error
NullPointerExceptionif serializedObject is null
から
1.4
617  {
618  return encodeObject( serializableObject, NO_OPTIONS );
619  } // end encodeObject
static String encodeObject(java.io.Serializable serializableObject)
Definition: Base64.java:616
static final int NO_OPTIONS
Definition: Base64.java:157

◆ encodeObject() [2/2]

static String org.keycloak.common.util.Base64.encodeObject ( java.io.Serializable  serializableObject,
int  options 
) throws java.io.IOException
inlinestatic

Serializes an object and returns the Base64-encoded version of that serialized object.

As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.

The object is not GZip-compressed before being encoded.

Example options:

  GZIP: gzip-compresses object before encoding it.
  DO_BREAK_LINES: break lines at 76 characters

Example: encodeObject( myObj, Base64.GZIP ) or

Example: encodeObject( myObj, Base64.GZIP | Base64.DO_BREAK_LINES )

引数
serializableObjectThe object to encode
optionsSpecified options
戻り値
The Base64-encoded object
参照
Base64::GZIP
Base64::DO_BREAK_LINES
例外
java.io.IOExceptionif there is an error
から
2.0
653  {
654 
655  if( serializableObject == null ){
656  throw new NullPointerException( "Cannot serialize a null object." );
657  } // end if: null
658 
659  // Streams
660  java.io.ByteArrayOutputStream baos = null;
661  java.io.OutputStream b64os = null;
662  java.util.zip.GZIPOutputStream gzos = null;
663  java.io.ObjectOutputStream oos = null;
664 
665 
666  try {
667  // ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream
668  baos = new java.io.ByteArrayOutputStream();
669  b64os = new Base64.OutputStream( baos, ENCODE | options );
670  if( (options & GZIP) != 0 ){
671  // Gzip
672  gzos = new java.util.zip.GZIPOutputStream(b64os);
673  oos = new java.io.ObjectOutputStream( gzos );
674  } else {
675  // Not gzipped
676  oos = new java.io.ObjectOutputStream( b64os );
677  }
678  oos.writeObject( serializableObject );
679  } // end try
680  catch( java.io.IOException e ) {
681  // Catch it and then throw it immediately so that
682  // the finally{} block is called for cleanup.
683  throw e;
684  } // end catch
685  finally {
686  try{ oos.close(); } catch( Exception e ){}
687  try{ gzos.close(); } catch( Exception e ){}
688  try{ b64os.close(); } catch( Exception e ){}
689  try{ baos.close(); } catch( Exception e ){}
690  } // end finally
691 
692  // Return value according to relevant encoding.
693  try {
694  return new String( baos.toByteArray(), PREFERRED_ENCODING );
695  } // end try
696  catch (java.io.UnsupportedEncodingException uue){
697  // Fall back to some Java default
698  return new String( baos.toByteArray() );
699  } // end catch
700 
701  } // end encode
static final int GZIP
Definition: Base64.java:168
Base64()
Definition: Base64.java:443
static final int ENCODE
Definition: Base64.java:160
static final String PREFERRED_ENCODING
Definition: Base64.java:211

◆ getAlphabet()

static final byte [] org.keycloak.common.util.Base64.getAlphabet ( int  options)
inlinestaticprivate

Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URLSAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked.

412  {
413  if ((options & URL_SAFE) == URL_SAFE) {
414  return _URL_SAFE_ALPHABET;
415  } else if ((options & ORDERED) == ORDERED) {
416  return _ORDERED_ALPHABET;
417  } else {
418  return _STANDARD_ALPHABET;
419  }
420  } // end getAlphabet
static final int ORDERED
Definition: Base64.java:192
static final byte [] _STANDARD_ALPHABET
Definition: Base64.java:222
static final int URL_SAFE
Definition: Base64.java:185
static final byte [] _URL_SAFE_ALPHABET
Definition: Base64.java:282
static final byte [] _ORDERED_ALPHABET
Definition: Base64.java:345

◆ getDecodabet()

static final byte [] org.keycloak.common.util.Base64.getDecodabet ( int  options)
inlinestaticprivate

Returns one of the _SOMETHING_DECODABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URL_SAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked.

430  {
431  if( (options & URL_SAFE) == URL_SAFE) {
432  return _URL_SAFE_DECODABET;
433  } else if ((options & ORDERED) == ORDERED) {
434  return _ORDERED_DECODABET;
435  } else {
436  return _STANDARD_DECODABET;
437  }
438  } // end getAlphabet
static final byte [] _URL_SAFE_DECODABET
Definition: Base64.java:298
static final int ORDERED
Definition: Base64.java:192
static final byte [] _ORDERED_DECODABET
Definition: Base64.java:363
static final int URL_SAFE
Definition: Base64.java:185
static final byte [] _STANDARD_DECODABET
Definition: Base64.java:240

メンバ詳解

◆ _ORDERED_ALPHABET

final byte [] org.keycloak.common.util.Base64._ORDERED_ALPHABET
staticprivate
初期値:
= {
(byte)'-',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
(byte)'_',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z'
}

I don't get the point of this technique, but someone requested it, and it is described here: http://www.faqs.org/qa/rfcc-1940.html.

◆ _ORDERED_DECODABET

final byte [] org.keycloak.common.util.Base64._ORDERED_DECODABET
staticprivate

Used in decoding the "ordered" dialect of Base64.

◆ _STANDARD_ALPHABET

final byte [] org.keycloak.common.util.Base64._STANDARD_ALPHABET
staticprivate
初期値:
= {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
}

The 64 valid Base64 values.

◆ _STANDARD_DECODABET

final byte [] org.keycloak.common.util.Base64._STANDARD_DECODABET
staticprivate

Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning.

◆ _URL_SAFE_ALPHABET

final byte [] org.keycloak.common.util.Base64._URL_SAFE_ALPHABET
staticprivate
初期値:
= {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'-', (byte)'_'
}

Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. Notice that the last two bytes become "hyphen" and "underscore" instead of "plus" and "slash."

◆ _URL_SAFE_DECODABET

final byte [] org.keycloak.common.util.Base64._URL_SAFE_DECODABET
staticprivate

Used in decoding URL- and Filename-safe dialects of Base64.

◆ DECODE

final int org.keycloak.common.util.Base64.DECODE = 0
static

Specify decoding in first bit. Value is zero.

◆ DO_BREAK_LINES

final int org.keycloak.common.util.Base64.DO_BREAK_LINES = 8
static

Do break lines when encoding. Value is 8.

◆ DONT_GUNZIP

final int org.keycloak.common.util.Base64.DONT_GUNZIP = 4
static

Specify that gzipped data should not be automatically gunzipped.

◆ ENCODE

final int org.keycloak.common.util.Base64.ENCODE = 1
static

Specify encoding in first bit. Value is one.

◆ EQUALS_SIGN

final byte org.keycloak.common.util.Base64.EQUALS_SIGN = (byte)'='
staticprivate

The equals sign (=) as a byte.

◆ EQUALS_SIGN_ENC

final byte org.keycloak.common.util.Base64.EQUALS_SIGN_ENC = -1
staticprivate

◆ GZIP

final int org.keycloak.common.util.Base64.GZIP = 2
static

Specify that data should be gzip-compressed in second bit. Value is two.

◆ MAX_LINE_LENGTH

final int org.keycloak.common.util.Base64.MAX_LINE_LENGTH = 76
staticprivate

Maximum line length (76) of Base64 output.

◆ NEW_LINE

final byte org.keycloak.common.util.Base64.NEW_LINE = (byte)'\n'
staticprivate

The new line character (
) as a byte.

◆ NO_OPTIONS

final int org.keycloak.common.util.Base64.NO_OPTIONS = 0
static

No options specified. Value is zero.

◆ ORDERED

final int org.keycloak.common.util.Base64.ORDERED = 32
static

Encode using the special "ordered" dialect of Base64 described here: http://www.faqs.org/qa/rfcc-1940.html.

◆ PREFERRED_ENCODING

final String org.keycloak.common.util.Base64.PREFERRED_ENCODING = "US-ASCII"
staticprivate

Preferred encoding.

◆ URL_SAFE

final int org.keycloak.common.util.Base64.URL_SAFE = 16
static

Encode using Base64-like encoding that is URL- and Filename-safe as described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. It is important to note that data encoded this way is not officially valid Base64, or at the very least should not be called Base64 without also specifying that is was encoded using the URL- and Filename-safe dialect.

◆ WHITE_SPACE_ENC

final byte org.keycloak.common.util.Base64.WHITE_SPACE_ENC = -5
staticprivate

このクラス詳解は次のファイルから抽出されました: