Replace Base64Encoder with Base64Utils
Replace the custom Base64Encoder class with Spring's Base64Utils which since 4.2 can encode bytes using Java 8's encoder, commons-digest or Java 6's JAX-B encoder. Closes gh-4189pull/4189/merge
parent
fb63e887bb
commit
300387d995
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.devtools.livereload;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Simple Base64 Encoder.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
final class Base64Encoder {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private static final String ALPHABET_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static final byte[] ALPHABET = ALPHABET_CHARS.getBytes(UTF_8);
|
||||
|
||||
private static final byte EQUALS_SIGN = '=';
|
||||
|
||||
private Base64Encoder() {
|
||||
}
|
||||
|
||||
public static String encode(String string) {
|
||||
return encode(string.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String encode(byte[] bytes) {
|
||||
byte[] encoded = new byte[bytes.length / 3 * 4 + (bytes.length % 3 == 0 ? 0 : 4)];
|
||||
for (int i = 0; i < bytes.length; i += 3) {
|
||||
encodeBlock(bytes, i, Math.min((bytes.length - i), 3), encoded, i / 3 * 4);
|
||||
}
|
||||
return new String(encoded, UTF_8);
|
||||
}
|
||||
|
||||
private static void encodeBlock(byte[] src, int srcPos, int blockLen, byte[] dest,
|
||||
int destPos) {
|
||||
int inBuff = (blockLen > 0 ? ((src[srcPos] << 24) >>> 8) : 0)
|
||||
| (blockLen > 1 ? ((src[srcPos + 1] << 24) >>> 16) : 0)
|
||||
| (blockLen > 2 ? ((src[srcPos + 2] << 24) >>> 24) : 0);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
dest[destPos + i] = (i > blockLen ? EQUALS_SIGN
|
||||
: ALPHABET[(inBuff >>> (6 * (3 - i))) & 0x3f]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.devtools.livereload;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link Base64Encoder}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class Base64EncoderTests {
|
||||
|
||||
private static final String TEXT = "Man is distinguished, not only by his reason, "
|
||||
+ "but by this singular passion from other animals, which is a lust of the "
|
||||
+ "mind, that by a perseverance of delight in the continued and indefatigable "
|
||||
+ "generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
|
||||
|
||||
private static final String ENCODED = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5I"
|
||||
+ "GhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbm"
|
||||
+ "ltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmF"
|
||||
+ "uY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVy"
|
||||
+ "YXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55I"
|
||||
+ "GNhcm5hbCBwbGVhc3VyZS4=";
|
||||
|
||||
@Test
|
||||
public void encodeText() {
|
||||
assertThat(Base64Encoder.encode(TEXT), equalTo(ENCODED));
|
||||
assertThat(Base64Encoder.encode("pleasure."), equalTo("cGxlYXN1cmUu"));
|
||||
assertThat(Base64Encoder.encode("leasure."), equalTo("bGVhc3VyZS4="));
|
||||
assertThat(Base64Encoder.encode("easure."), equalTo("ZWFzdXJlLg=="));
|
||||
assertThat(Base64Encoder.encode("asure."), equalTo("YXN1cmUu"));
|
||||
assertThat(Base64Encoder.encode("sure."), equalTo("c3VyZS4="));
|
||||
}
|
||||
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Simple Base64 Encoder.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
final class Base64Encoder {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private static final String ALPHABET_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static final byte[] ALPHABET = ALPHABET_CHARS.getBytes(UTF_8);
|
||||
|
||||
private static final byte EQUALS_SIGN = '=';
|
||||
|
||||
private Base64Encoder() {
|
||||
}
|
||||
|
||||
public static String encode(String string) {
|
||||
return encode(string.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String encode(byte[] bytes) {
|
||||
byte[] encoded = new byte[bytes.length / 3 * 4 + (bytes.length % 3 == 0 ? 0 : 4)];
|
||||
for (int i = 0; i < bytes.length; i += 3) {
|
||||
encodeBlock(bytes, i, Math.min((bytes.length - i), 3), encoded, i / 3 * 4);
|
||||
}
|
||||
return new String(encoded, UTF_8);
|
||||
}
|
||||
|
||||
private static void encodeBlock(byte[] src, int srcPos, int blockLen, byte[] dest,
|
||||
int destPos) {
|
||||
int inBuff = (blockLen > 0 ? ((src[srcPos] << 24) >>> 8) : 0)
|
||||
| (blockLen > 1 ? ((src[srcPos + 1] << 24) >>> 16) : 0)
|
||||
| (blockLen > 2 ? ((src[srcPos + 2] << 24) >>> 24) : 0);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
dest[destPos + i] = (i > blockLen ? EQUALS_SIGN
|
||||
: ALPHABET[(inBuff >>> (6 * (3 - i))) & 0x3f]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link Base64Encoder}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class Base64EncoderTests {
|
||||
|
||||
private static final String TEXT = "Man is distinguished, not only by his reason, "
|
||||
+ "but by this singular passion from other animals, which is a lust of the "
|
||||
+ "mind, that by a perseverance of delight in the continued and indefatigable "
|
||||
+ "generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
|
||||
|
||||
private static final String ENCODED = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5I"
|
||||
+ "GhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbm"
|
||||
+ "ltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmF"
|
||||
+ "uY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVy"
|
||||
+ "YXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55I"
|
||||
+ "GNhcm5hbCBwbGVhc3VyZS4=";
|
||||
|
||||
@Test
|
||||
public void encodeText() {
|
||||
assertThat(Base64Encoder.encode(TEXT), equalTo(ENCODED));
|
||||
assertThat(Base64Encoder.encode("pleasure."), equalTo("cGxlYXN1cmUu"));
|
||||
assertThat(Base64Encoder.encode("leasure."), equalTo("bGVhc3VyZS4="));
|
||||
assertThat(Base64Encoder.encode("easure."), equalTo("ZWFzdXJlLg=="));
|
||||
assertThat(Base64Encoder.encode("asure."), equalTo("YXN1cmUu"));
|
||||
assertThat(Base64Encoder.encode("sure."), equalTo("c3VyZS4="));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue