SHA256
122 lines
6.5 KiB
Java
122 lines
6.5 KiB
Java
package blockchain.body;
|
|
|
|
import blockchain.MsgSubType;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.nio.charset.CharacterCodingException;
|
|
import java.nio.charset.CodingErrorAction;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* TEXT_REPLY / TEXT_EDIT_REPLY.
|
|
* REPLY external target: login + forkNumber + blockNumber + hash32.
|
|
* EDIT_REPLY compact own-chain target: blockNumber + hash32.
|
|
*/
|
|
public final class TextReplyBody implements BodyRecord, BodyHasTarget {
|
|
public static final short TYPE = 1;
|
|
public static final short VER = 1;
|
|
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
|
|
|
public final short subType;
|
|
public final short version;
|
|
public final String toLogin;
|
|
public final Integer toForkNumber;
|
|
public final int toBlockGlobalNumber;
|
|
public final byte[] toBlockHash32;
|
|
public final String message;
|
|
|
|
public TextReplyBody(short subType, short version, byte[] bodyBytes) {
|
|
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
|
this.subType = subType;
|
|
this.version = version;
|
|
if ((version & 0xFFFF) != (VER & 0xFFFF)) throw new IllegalArgumentException("TextReplyBody version must be 1");
|
|
int st = subType & 0xFFFF;
|
|
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) throw new IllegalArgumentException("Bad reply subtype");
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
|
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
|
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "REPLY", 2);
|
|
this.toLogin = t.login();
|
|
this.toForkNumber = t.forkNumber();
|
|
this.toBlockGlobalNumber = t.blockNumber();
|
|
this.toBlockHash32 = t.blockHash32();
|
|
} else {
|
|
BodyTargetSupport.ensureMin(bb, 4 + 32 + 2, "EDIT_REPLY too short");
|
|
this.toLogin = null;
|
|
this.toForkNumber = null;
|
|
this.toBlockGlobalNumber = bb.getInt();
|
|
this.toBlockHash32 = new byte[32];
|
|
bb.get(this.toBlockHash32);
|
|
BodyTargetSupport.validateCompact(this.toBlockGlobalNumber, this.toBlockHash32, "EDIT_REPLY");
|
|
}
|
|
this.message = readText(bb, st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF));
|
|
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected TextReplyBody tail");
|
|
}
|
|
|
|
public TextReplyBody(short subType, String toLogin, Integer toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
|
Objects.requireNonNull(message, "message == null");
|
|
int st = subType & 0xFFFF;
|
|
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) throw new IllegalArgumentException("Bad reply subtype");
|
|
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
|
if (message.isBlank()) throw new IllegalArgumentException("REPLY message is blank");
|
|
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
|
} else {
|
|
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_REPLY must not contain login/fork target");
|
|
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_REPLY");
|
|
}
|
|
this.subType = subType;
|
|
this.version = VER;
|
|
this.toLogin = toLogin;
|
|
this.toForkNumber = toForkNumber;
|
|
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
|
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
|
this.message = message;
|
|
}
|
|
|
|
@Override public TextReplyBody check() {
|
|
int st = subType & 0xFFFF;
|
|
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
|
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
|
if (message == null || message.isBlank()) throw new IllegalArgumentException("REPLY message is blank");
|
|
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
|
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_REPLY must not contain login/fork target");
|
|
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_REPLY");
|
|
if (message == null) throw new IllegalArgumentException("EDIT_REPLY message is null");
|
|
} else throw new IllegalArgumentException("Bad reply subtype");
|
|
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
|
return this;
|
|
}
|
|
|
|
@Override public byte[] toBytes() {
|
|
check();
|
|
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
|
boolean reply = (subType & 0xFFFF) == (MsgSubType.TEXT_REPLY & 0xFFFF);
|
|
int targetSize = reply ? BodyTargetSupport.externalSize(toLogin) : 4 + 32;
|
|
ByteBuffer bb = ByteBuffer.allocate(targetSize + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
|
if (reply) BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
|
else bb.putInt(toBlockGlobalNumber).put(toBlockHash32);
|
|
bb.putShort((short) text.length).put(text);
|
|
return bb.array();
|
|
}
|
|
|
|
@Override public String toLogin() { return toLogin; }
|
|
@Override public Integer toForkNumber() { return toForkNumber; }
|
|
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
|
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
|
public boolean isEditReply() { return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF); }
|
|
|
|
private static String readText(ByteBuffer bb, boolean allowEmpty) {
|
|
if (bb.remaining() < 2) throw new IllegalArgumentException("Text length missing");
|
|
int len = Short.toUnsignedInt(bb.getShort());
|
|
if (len == 0) { if (allowEmpty) return ""; throw new IllegalArgumentException("Text is empty"); }
|
|
if (bb.remaining() < len) throw new IllegalArgumentException("Text payload too short");
|
|
byte[] bytes = new byte[len]; bb.get(bytes);
|
|
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
|
try { String s = dec.decode(ByteBuffer.wrap(bytes)).toString(); if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException("Text is blank"); return s; }
|
|
catch (CharacterCodingException e) { throw new IllegalArgumentException("Text invalid UTF-8", e); }
|
|
}
|
|
}
|