添加jar包org.apache.shiro:shiro-core:1.3.2
SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());
这个对象里的成员变量 cachedBase64是什么时候被赋值的。
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
如果不重写toString()方法:会得到输出:xxxx@xxxxxxx的类名加地址形式;
如果重写toString()方法,则输出重写后的值。
SimpleByteSource类中重写了toString()方法:
public String toString() {
return this.toBase64();
}
public int hashCode() {
return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o instanceof ByteSource) {
ByteSource bs = (ByteSource)o;
return Arrays.equals(this.getBytes(), bs.getBytes());
} else {
return false;
}
}
public String toBase64() {
if (this.cachedBase64 == null) {
this.cachedBase64 = Base64.encodeToString(this.getBytes());
}
return this.cachedBase64;
}
public class SimpleByteSourceTest implements ByteSource {
private final byte[] bytes;
private String cachedHex;
private String cachedBase64;
public SimpleByteSourceTest(byte[] bytes) {
this.bytes = bytes;
}
public SimpleByteSourceTest(char[] chars) {
this.bytes = CodecSupport.toBytes(chars);
}
public SimpleByteSourceTest(String string) {
this.bytes = CodecSupport.toBytes(string);
}
public SimpleByteSourceTest(ByteSource source) {
this.bytes = source.getBytes();
}
public SimpleByteSourceTest(File file) {
this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(file);
}
public SimpleByteSourceTest(InputStream stream) {
this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(stream);
}
public static boolean isCompatible(Object o) {
return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
}
@Override
public byte[] getBytes() {
return this.bytes;
}
@Override
public boolean isEmpty() {
return this.bytes == null || this.bytes.length == 0;
}
@Override
public String toHex() {
if (this.cachedHex == null) {
this.cachedHex = Hex.encodeToString(this.getBytes());
}
return this.cachedHex;
}
@Override
public String toBase64() {
if (this.cachedBase64 == null) {
this.cachedBase64 = Base64.encodeToString(this.getBytes());
}
return this.cachedBase64;
}
private static final class BytesHelper extends CodecSupport {
private BytesHelper() {
}
public byte[] getBytes(File file) {
return this.toBytes(file);
}
public byte[] getBytes(InputStream stream) {
return this.toBytes(stream);
}
}
}
运行结果图:
1.当调用 SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());
创建了SimpleByteSource 对象,并且给bytes属性赋值;
SimpleByteSource 类中成员变量如下:
private final byte[] bytes;
private String cachedHex;
private String cachedBase64;
调用的是有参构造,bytes属性就被赋值了赋值
public SimpleByteSource(byte[] bytes) {
this.bytes = bytes;
}
当执行simpleByteSource.toBase64()时或者调用simpleByteSource.toString(),成员变量 cachedBase64就被赋值了,SimpleByteSource的原码如下:
this.cachedBase64 = Base64.encodeToString(this.getBytes());
public String toBase64() {
if (this.cachedBase64 == null) {
this.cachedBase64 = Base64.encodeToString(this.getBytes());
}
return this.cachedBase64;
}
public String toString() {
return this.toBase64();
}