[Minor] Removed BaseEncoding as JDK now has Base64

This commit is contained in:
Robert von Burg 2023-04-04 07:15:53 +02:00
parent e09574cc16
commit 5c49921c9e
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 0 additions and 2549 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,187 +0,0 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package li.strolch.utils.helper;
import static li.strolch.utils.helper.BaseEncoding.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/
@SuppressWarnings("nls")
public class BaseDecodingTest {
public static final String PROP_RUN_PERF_TESTS = "li.strolch.utils.test.runPerfTests"; //$NON-NLS-1$
private static final Logger logger = LoggerFactory.getLogger(BaseDecodingTest.class);
public static boolean isSkipPerfTests() {
String context = BaseDecodingTest.class.getSimpleName();
String key = PROP_RUN_PERF_TESTS;
boolean runPerfTests = PropertiesHelper.getPropertyBool(context, key, Boolean.FALSE);
return !runPerfTests;
}
@Test
public void testBase64() {
assertEquals("", fromBase64(""));
assertEquals("f", fromBase64("Zg=="));
assertEquals("fo", fromBase64("Zm8="));
assertEquals("foo", fromBase64("Zm9v"));
assertEquals("foob", fromBase64("Zm9vYg=="));
assertEquals("fooba", fromBase64("Zm9vYmE="));
assertEquals("foobar", fromBase64("Zm9vYmFy"));
}
@Test
public void testBase32() {
assertEquals("", fromBase32(""));
assertEquals("f", fromBase32("MY======"));
assertEquals("fo", fromBase32("MZXQ===="));
assertEquals("foo", fromBase32("MZXW6==="));
assertEquals("foob", fromBase32("MZXW6YQ="));
assertEquals("fooba", fromBase32("MZXW6YTB"));
assertEquals("foobar", fromBase32("MZXW6YTBOI======"));
}
@Test
public void testBase32Hex() {
assertEquals("", fromBase32Hex(""));
assertEquals("f", fromBase32Hex("CO======"));
assertEquals("fo", fromBase32Hex("CPNG===="));
assertEquals("foo", fromBase32Hex("CPNMU==="));
assertEquals("foob", fromBase32Hex("CPNMUOG="));
assertEquals("fooba", fromBase32Hex("CPNMUOJ1"));
assertEquals("foobar", fromBase32Hex("CPNMUOJ1E8======"));
}
@Test
public void testBase32Dmedia() {
assertEquals("", fromBase32Dmedia(""));
assertEquals("binary foo", fromBase32Dmedia("FCNPVRELI7J9FUUI"));
assertEquals("f", fromBase32Dmedia("FR======"));
assertEquals("fo", fromBase32Dmedia("FSQJ===="));
assertEquals("foo", fromBase32Dmedia("FSQPX==="));
assertEquals("foob", fromBase32Dmedia("FSQPXRJ="));
assertEquals("fooba", fromBase32Dmedia("FSQPXRM4"));
assertEquals("foobar", fromBase32Dmedia("FSQPXRM4HB======"));
}
@Test
public void testBase16() {
assertEquals("", fromBase16(""));
assertEquals("f", fromBase16("66"));
assertEquals("fo", fromBase16("666F"));
assertEquals("foo", fromBase16("666F6F"));
assertEquals("foob", fromBase16("666F6F62"));
assertEquals("fooba", fromBase16("666F6F6261"));
assertEquals("foobar", fromBase16("666F6F626172"));
}
@Test
public void testBase64Perf() {
if (isSkipPerfTests()) {
return;
}
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 'Z';
}
long start = System.nanoTime();
for (int i = 0; i < 200; i++) {
fromBase64(bytes);
}
long end = System.nanoTime();
logger.info("Decoding 200MB Base64 took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32Perf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 'M';
}
long start = System.nanoTime();
for (int i = 0; i < 200; i++) {
fromBase32(bytes);
}
long end = System.nanoTime();
logger.info("Decoding 200MB Base32 took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32HexPerf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 'C';
}
long start = System.nanoTime();
for (int i = 0; i < 200; i++) {
fromBase32Hex(bytes);
}
long end = System.nanoTime();
logger.info("Decoding 200MB Base32Hex took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32DmediaPerf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase32Hex(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base32Dmedia took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase16Perf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = '6';
}
long start = System.nanoTime();
for (int i = 0; i < 200; i++) {
fromBase16(bytes);
}
long end = System.nanoTime();
logger.info("Decoding 200MB Base16 took " + StringHelper.formatNanoDuration(end - start));
}
}

View File

@ -1,170 +0,0 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package li.strolch.utils.helper;
import static li.strolch.utils.helper.BaseDecodingTest.PROP_RUN_PERF_TESTS;
import static li.strolch.utils.helper.BaseDecodingTest.isSkipPerfTests;
import static li.strolch.utils.helper.BaseEncoding.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/
@SuppressWarnings("nls")
public class BaseEncodingTest {
private static final Logger logger = LoggerFactory.getLogger(BaseEncodingTest.class);
@Test
public void testBase64() {
assertEquals("", toBase64(""));
assertEquals("Zg==", toBase64("f"));
assertEquals("Zm8=", toBase64("fo"));
assertEquals("Zm9v", toBase64("foo"));
assertEquals("Zm9vYg==", toBase64("foob"));
assertEquals("Zm9vYmE=", toBase64("fooba"));
assertEquals("Zm9vYmFy", toBase64("foobar"));
}
@Test
public void testBase32() {
assertEquals("", toBase32(""));
assertEquals("MY======", toBase32("f"));
assertEquals("MZXQ====", toBase32("fo"));
assertEquals("MZXW6===", toBase32("foo"));
assertEquals("MZXW6YQ=", toBase32("foob"));
assertEquals("MZXW6YTB", toBase32("fooba"));
assertEquals("MZXW6YTBOI======", toBase32("foobar"));
}
@Test
public void testBase32Hex() {
assertEquals("", toBase32Hex(""));
assertEquals("CO======", toBase32Hex("f"));
assertEquals("CPNG====", toBase32Hex("fo"));
assertEquals("CPNMU===", toBase32Hex("foo"));
assertEquals("CPNMUOG=", toBase32Hex("foob"));
assertEquals("CPNMUOJ1", toBase32Hex("fooba"));
assertEquals("CPNMUOJ1E8======", toBase32Hex("foobar"));
}
@Test
public void testBase32Dmedia() {
assertEquals("", toBase32Dmedia(""));
assertEquals("FCNPVRELI7J9FUUI", toBase32Dmedia("binary foo"));
assertEquals("FR======", toBase32Dmedia("f"));
assertEquals("FSQJ====", toBase32Dmedia("fo"));
assertEquals("FSQPX===", toBase32Dmedia("foo"));
assertEquals("FSQPXRJ=", toBase32Dmedia("foob"));
assertEquals("FSQPXRM4", toBase32Dmedia("fooba"));
assertEquals("FSQPXRM4HB======", toBase32Dmedia("foobar"));
}
@Test
public void testBase16() {
assertEquals("", toBase16(""));
assertEquals("66", toBase16("f"));
assertEquals("666F", toBase16("fo"));
assertEquals("666F6F", toBase16("foo"));
assertEquals("666F6F62", toBase16("foob"));
assertEquals("666F6F6261", toBase16("fooba"));
assertEquals("666F6F626172", toBase16("foobar"));
}
@Test
public void testBase64Perf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase64(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base64 took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32Perf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase32(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base32 took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32HexPerf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase32Hex(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base32Hex took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase32DmediaPerf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase32Hex(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base32Dmedia took " + StringHelper.formatNanoDuration(end - start));
}
@Test
public void testBase16Perf() {
if (isSkipPerfTests()) {
logger.info("Not running performance tests as not enabled by system property " + PROP_RUN_PERF_TESTS);
return;
}
long start = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
for (int i = 0; i < 200; i++) {
toBase16(bytes);
}
long end = System.nanoTime();
logger.info("Encoding 200MB Base16 took " + StringHelper.formatNanoDuration(end - start));
}
}

View File

@ -1,71 +0,0 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package li.strolch.utils.helper;
import java.util.HashMap;
import java.util.Map;
/**
* Simple helper class to generate the reverse alphabets for {@link BaseEncoding}
*
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/
@SuppressWarnings("nls")
public class GenerateReverseBaseEncodingAlphabets {
public static void main(String[] args) {
System.out.println(generateReverseAlphabet("REV_BASE_16", BaseEncoding.BASE_16));
System.out.println(generateReverseAlphabet("REV_BASE_32", BaseEncoding.BASE_32));
System.out.println(generateReverseAlphabet("REV_BASE_32_CROCKFORD", BaseEncoding.BASE_32_CROCKFORD));
System.out.println(generateReverseAlphabet("REV_BASE_32_DMEDIA", BaseEncoding.BASE_32_DMEDIA));
System.out.println(generateReverseAlphabet("REV_BASE_32_HEX", BaseEncoding.BASE_32_HEX));
System.out.println(generateReverseAlphabet("REV_BASE_64", BaseEncoding.BASE_64));
System.out.println(generateReverseAlphabet("REV_BASE_64_SAFE", BaseEncoding.BASE_64_SAFE));
}
public static String generateReverseAlphabet(String name, byte[] alphabet) {
Map<Byte, Byte> valueToIndex = new HashMap<>();
for (byte i = 0; i < alphabet.length; i++) {
Byte value = Byte.valueOf(i);
Byte key = Byte.valueOf(alphabet[value]);
if (valueToIndex.containsKey(key))
throw new RuntimeException("Alphabet hast twice the same value " + key + " at index " + value);
valueToIndex.put(key, value);
}
StringBuilder sb = new StringBuilder();
sb.append("private static final byte[] " + name + " = { ");
Byte minusOne = Byte.valueOf((byte) -1);
for (int i = 0; i < 128; i++) {
Byte index = Byte.valueOf((byte) i);
Byte value = valueToIndex.get(index);
if (value == null)
sb.append(minusOne.toString());
else
sb.append(value.toString());
if (i < 127)
sb.append(", ");
}
sb.append(" };");
return sb.toString();
}
}