frostfs-sdk-java/models/src/main/java/info/frostfs/sdk/mappers/SessionMapper.java
Bruk Ori 05ac5bb070
Some checks failed
DCO / DCO (pull_request) Failing after 27s
[#XX] provide additional security
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2024-07-24 14:41:42 +03:00

42 lines
1.2 KiB
Java

package info.frostfs.sdk.mappers;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.InvalidProtocolBufferException;
import frostfs.session.Types;
import java.io.IOException;
import static java.util.Objects.isNull;
public class SessionMapper {
private SessionMapper() {
}
public static byte[] serialize(Types.SessionToken token) {
if (isNull(token)) {
throw new IllegalArgumentException("Token is not present");
}
try {
byte[] bytes = new byte[token.getSerializedSize()];
CodedOutputStream stream = CodedOutputStream.newInstance(bytes);
token.writeTo(stream);
return bytes;
} catch (IOException exp) {
throw new IllegalArgumentException(exp.getMessage());
}
}
public static Types.SessionToken deserializeSessionToken(byte[] bytes) {
if (isNull(bytes) || bytes.length == 0) {
throw new IllegalArgumentException("Token is not present");
}
try {
return Types.SessionToken.newBuilder().mergeFrom(bytes).build();
} catch (InvalidProtocolBufferException exp) {
throw new IllegalArgumentException(exp.getMessage());
}
}
}