public static byte[] decode(String encoded, int length) {
		Preconditions.checkArgument(length > 0);

		ByteArrayOutputStream os = new ByteArrayOutputStream(length);
		int offset = 0, encodedLength = encoded.length(), decodedLength = 0;

		while (offset < encodedLength - 1 && decodedLength < length) {
			byte first = decodeCharacter(encoded.charAt(offset++));
			byte second = decodeCharacter(encoded.charAt(offset++));
			if (first == -1 || second == -1) {
				break;
			}
			byte decoded = (byte) (first << 2);
			decoded |= (second & 0x30) >> 4;
			os.write(decoded);
			if (++decodedLength >= length || offset >= encodedLength) {
				break;
			}
			byte next = decodeCharacter(encoded.charAt(offset++));
			if (next == -1) {
				break;
			}
			decoded = (byte) ((second & 0x0F) << 4);
			decoded |= (next & 0x3c) >> 2;
			os.write(decoded);
			if (++decodedLength >= length || offset >= encodedLength) {
				break;
			}
			byte last = decodeCharacter(encoded.charAt(offset++));
			decoded = (byte) ((next & 0x03) << 6);
			decoded |= last;
			os.write(decoded);
			++decodedLength;
		}

		return os.toByteArray();
	}