Skip to content

Commit

Permalink
解决中文粘贴板问题
Browse files Browse the repository at this point in the history
  • Loading branch information
SpringStudent committed Dec 20, 2024
1 parent 64aaa56 commit 22b0f55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

### 未来规划

* 添加文件传输功能。
* 支持粘贴板文字和图片(已完成)
* 客户端信息持久化
* 分布式。。。可能吧

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
**/
public class CmdClipboardText extends Cmd {
private String payload;

private String controllType;

public CmdClipboardText(String payload, String controllType) {
Expand All @@ -26,7 +25,9 @@ public CmdType getType() {

@Override
public int getWireSize() {
return 8 + payload.length() + controllType.length();
// 修改为根据UTF-8字符集来计算字节数
return 4 + payload.getBytes(StandardCharsets.UTF_8).length +
4 + controllType.getBytes(StandardCharsets.UTF_8).length;
}

@Override
Expand All @@ -40,17 +41,23 @@ public String getPayload() {

@Override
public void encode(ByteBuf out) throws IOException {
out.writeInt(payload.length());
out.writeCharSequence(payload, StandardCharsets.UTF_8);
out.writeInt(controllType.length());
out.writeCharSequence(controllType, StandardCharsets.UTF_8);
byte[] payloadBytes = payload.getBytes(StandardCharsets.UTF_8);
out.writeInt(payloadBytes.length);
out.writeBytes(payloadBytes);
byte[] controllTypeBytes = controllType.getBytes(StandardCharsets.UTF_8);
out.writeInt(controllTypeBytes.length);
out.writeBytes(controllTypeBytes);
}

public static CmdClipboardText decode(ByteBuf in) {
int payloadLength = in.readInt();
String payload = in.readCharSequence(payloadLength, StandardCharsets.UTF_8).toString();
byte[] payloadBytes = new byte[payloadLength];
in.readBytes(payloadBytes);
String payload = new String(payloadBytes, StandardCharsets.UTF_8);
int controllTypeLength = in.readInt();
String controllType = in.readCharSequence(controllTypeLength, StandardCharsets.UTF_8).toString();
byte[] controllTypeBytes = new byte[controllTypeLength];
in.readBytes(controllTypeBytes);
String controllType = new String(controllTypeBytes, StandardCharsets.UTF_8);
return new CmdClipboardText(payload, controllType);
}
}
}

0 comments on commit 22b0f55

Please sign in to comment.