Base32String.java revision 7e3fa36d69ffee874dd364b8e3d9aa3cab9a273b
/*
* Copyright 2009 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Encodes arbitrary byte arrays as case-insensitive base-32 strings.
* <p>
* The implementation is slightly different than in RFC 4648. During encoding,
* padding is not added, and during decoding the last incomplete chunk is not
* taken into account. The result is that multiple strings decode to the same
* byte array, for example, string of sixteen 7s ("7...7") and seventeen 7s both
* decode to the same byte array.
* TODO(sarvar): Revisit this encoding and whether this ambiguity needs fixing.
*
* @author sweis@google.com (Steve Weis)
* @author Neal Gafter
*/
public class Base32String {
// singleton
private static final Base32String INSTANCE =
static Base32String getInstance() {
return INSTANCE;
}
// 32 alpha-numeric characters.
private char[] DIGITS;
private int MASK;
private int SHIFT;
}
}
}
// Remove whitespace and separators
// Remove padding. Note: the padding is used as hint to determine how many
// bits to decode from the last incomplete chunk (which is commented out
// below, so this may have been wrong to start with).
// Canonicalize to all upper case
return new byte[0];
}
int buffer = 0;
int next = 0;
int bitsLeft = 0;
for (char c : encoded.toCharArray()) {
if (!CHAR_MAP.containsKey(c)) {
throw new DecodingException("Illegal character: " + c);
}
if (bitsLeft >= 8) {
bitsLeft -= 8;
}
}
// We'll ignore leftover bits for now.
//
// if (next != outLength || bitsLeft >= SHIFT) {
// throw new DecodingException("Bits left: " + bitsLeft);
// }
return result;
}
}
return "";
}
// SHIFT is the number of bits per output character, so the length of the
// output is the length of the input multiplied by 8/SHIFT, rounded up.
// The computation below will fail, so don't do it.
throw new IllegalArgumentException();
}
int next = 1;
int bitsLeft = 8;
buffer <<= 8;
bitsLeft += 8;
} else {
}
}
}
}
// enforce that this class is a singleton
throw new CloneNotSupportedException();
}
public static class DecodingException extends Exception {
super(message);
}
}
}