From 04e14c55b20f7b9a3b40bbfb6e39e1887df8237a Mon Sep 17 00:00:00 2001 From: "shanming.sun" Date: Sat, 22 Oct 2022 00:36:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../poco/service/impl/SocketClientImpl.java | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/cloud/sonic/driver/poco/service/impl/SocketClientImpl.java b/src/main/java/org/cloud/sonic/driver/poco/service/impl/SocketClientImpl.java index 1b69ff5..aa24160 100644 --- a/src/main/java/org/cloud/sonic/driver/poco/service/impl/SocketClientImpl.java +++ b/src/main/java/org/cloud/sonic/driver/poco/service/impl/SocketClientImpl.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -45,41 +46,34 @@ public SocketClientImpl(int port, Logger logger) { @Override public Object sendAndReceive(JSONObject jsonObject) throws SonicRespException { - int len = jsonObject.toJSONString().length(); - ByteBuffer header = ByteBuffer.allocate(4); - header.put(intToByteArray(len), 0, 4); - header.flip(); - ByteBuffer body = ByteBuffer.allocate(len); - body.put(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8), 0, len); - body.flip(); - ByteBuffer total = ByteBuffer.allocate(len + 4); - total.put(header.array()); - total.put(body.array()); - total.flip(); + byte[] data = jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8); + byte[] header = intToByteArray(data.length); + synchronized (SocketClientImpl.class) { try { - outputStream.write(total.array()); + outputStream.write(header); + outputStream.write(data); + outputStream.flush(); byte[] head = new byte[4]; + byte[] buffer = new byte[8192]; inputStream.read(head); int headLen = toInt(head); - StringBuilder s = new StringBuilder(); + ByteBuffer rData = ByteBuffer.allocate(headLen); while (poco.isConnected() && !Thread.interrupted()) { - byte[] buffer = new byte[1024]; + int realLen; realLen = inputStream.read(buffer); - if (buffer.length != realLen && realLen >= 0) { - buffer = subByteArray(buffer, 0, realLen); - } - if (realLen >= 0) { - s.append(new String(buffer)); - if (s.toString().getBytes(StandardCharsets.UTF_8).length == headLen) { - JSONObject re = JSON.parseObject(s.toString()); - logger.info(re.toJSONString()); - if (re.getString("id").equals(jsonObject.getString("id"))) { - return re.get("result"); - } else { - throw new SonicRespException("id not found!"); - } + rData.put(buffer, 0, realLen); + + if (rData.position() == headLen) { + rData.flip(); + String sData = new String(rData.array(),StandardCharsets.UTF_8 ); + JSONObject re = JSON.parseObject(sData); + logger.info(sData); + if (re.getString("id").equals(jsonObject.getString("id"))) { + return re.get("result"); + } else { + throw new SonicRespException("id not found!"); } } } @@ -160,4 +154,9 @@ private int toInt(byte[] b) { } return res; } -} \ No newline at end of file + + public static void main(String[] args) throws UnsupportedEncodingException { + System.out.printf("xx中文x".length()+ ","); + System.out.printf("xx中文x".getBytes(StandardCharsets.UTF_8).length+ ""); + } +}