Name serialization
This commit is contained in:
45
core/src/main/groovy/com/muwire/core/Name.groovy
Normal file
45
core/src/main/groovy/com/muwire/core/Name.groovy
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.muwire.core
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A name of persona, file or search term
|
||||||
|
*/
|
||||||
|
public class Name {
|
||||||
|
final String name
|
||||||
|
|
||||||
|
Name(String name) {
|
||||||
|
this.name = name
|
||||||
|
}
|
||||||
|
|
||||||
|
Name(InputStream nameStream) throws IOException {
|
||||||
|
DataInputStream dis = new DataInputStream(nameStream)
|
||||||
|
int length = dis.readUnsignedShort()
|
||||||
|
byte [] nameBytes = new byte[length]
|
||||||
|
dis.readFully(nameBytes)
|
||||||
|
this.name = new String(nameBytes, StandardCharsets.UTF_8)
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(OutputStream out) throws IOException {
|
||||||
|
DataOutputStream dos = new DataOutputStream(out)
|
||||||
|
dos.writeShort(name.length())
|
||||||
|
dos.writeBuffer(name.getBytes(StandardCharsets.UTF_8))
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName() {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
name.hashCode()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof Name))
|
||||||
|
return false
|
||||||
|
Name other = (Name)o
|
||||||
|
name.equals(other.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user