ui/shared/src/main/java/com/squareup/subzero/shared/Base45.java
public static byte[] fromBase45(String input) {
    // input shrinks by a factor 1.5
    ByteArrayOutputStream output = new ByteArrayOutputStream((int) (input.length() / 1.5));
    int acc = 0;
    int counter = 0;

    // consume the input string
    for (int i = 0; i < input.length(); i++) {
      Integer inputValue = indexOf(input.charAt(i));
      if (inputValue == null) {
        throw new IllegalArgumentException(format("invalid char (%d) at offset %d", (int)input.charAt(i), i));
      }
      // read 3 chars at a time
      acc = acc * 45 + inputValue;
      counter++;
      if (counter == 3) {
        // emit acc and reset
        int s0 = acc / 256;
        int s1 = acc % 256;
        if (s0 >= 256) {
          throw new IllegalStateException(format("s0 should be <256, but is %d", s0));
        }
        output.write((byte) s0);
        output.write((byte) s1);

        acc = 0;
        counter = 0;
      }
    }

    if (counter > 0) {
      if (acc >= 256) {
        throw new IllegalStateException(format("acc should be <256, but is %d", acc));
      }
      output.write((byte) acc);
    }
    return output.toByteArray();
  }
/**
   * Decodes a Base45-encoded String back to a byte array.
   *
   * Throws IllegalArgumentException if the input contains invalid characters.
   */
Decodes a Base45-encoded String back to a byte array.
Throws IllegalArgumentException if the input contains invalid characters.
Decodes a Base45 - encoded String back to a byte array . Throws IllegalArgumentException if the input contains invalid characters .
public static byte[] fromBase45(String input) {
    ByteArrayOutputStream output = new ByteArrayOutputStream((int) (input.length() / 1.5));
    int acc = 0;
    int counter = 0;
    for (int i = 0; i < input.length(); i++) {
      Integer inputValue = indexOf(input.charAt(i));
      if (inputValue == null) {
        throw new IllegalArgumentException(format("invalid char (%d) at offset %d", (int)input.charAt(i), i));
      }
      acc = acc * 45 + inputValue;
      counter++;
      if (counter == 3) {
        int s0 = acc / 256;
        int s1 = acc % 256;
        if (s0 >= 256) {
          throw new IllegalStateException(format("s0 should be <256, but is %d", s0));
        }
        output.write((byte) s0);
        output.write((byte) s1);
        acc = 0;
        counter = 0;
      }
    }
    if (counter > 0) {
      if (acc >= 256) {
        throw new IllegalStateException(format("acc should be <256, but is %d", acc));
      }
      output.write((byte) acc);
    }
    return output.toByteArray();
  }
public static byte [ ] fromBase45 ( String input ) { ByteArrayOutputStream output = new ByteArrayOutputStream ( ( int ) ( input . length ( ) / 1.5 ) ) ; int acc = 0 ; int counter = 0 ; for ( int i = 0 ; i < input . length ( ) ; i ++ ) { Integer inputValue = indexOf ( input . charAt ( i ) ) ; if ( inputValue == null ) { throw new IllegalArgumentException ( format ( "invalid char (%d) at offset %d" , ( int ) input . charAt ( i ) , i ) ) ; } acc = acc * 45 + inputValue ; counter ++ ; if ( counter == 3 ) { int s0 = acc / 256 ; int s1 = acc % 256 ; if ( s0 >= 256 ) { throw new IllegalStateException ( format ( "s0 should be <256, but is %d" , s0 ) ) ; } output . write ( ( byte ) s0 ) ; output . write ( ( byte ) s1 ) ; acc = 0 ; counter = 0 ; } } if ( counter > 0 ) { if ( acc >= 256 ) { throw new IllegalStateException ( format ( "acc should be <256, but is %d" , acc ) ) ; } output . write ( ( byte ) acc ) ; } return output . toByteArray ( ) ; }
Decodes a Base45-encoded String back to a byte array.
Decodes a Base45 - encoded String back to a byte array .
// input shrinks by a factor 1.5 // consume the input string // read 3 chars at a time // emit acc and reset