Java-从socketchannel读取

Java-从socketchannel读取,java,socketchannel,Java,Socketchannel,因此,我使用java中的Socketchannes来发送和接收项目的数据。该项目与第三方进行交互,第三方需要数据的字节缓冲,但其前缀为4字节,即其长度 当接收时,我将接收一个字节缓冲符,我需要从前端解构4字节的长度并提取数据。我收到的信息不是固定长度的 我目前的执行情况如下: public PedResponse send(ByteBuffer message) { String returnString; try { message.f

因此,我使用java中的Socketchannes来发送和接收项目的数据。该项目与第三方进行交互,第三方需要数据的字节缓冲,但其前缀为4字节,即其长度

当接收时,我将接收一个字节缓冲符,我需要从前端解构4字节的长度并提取数据。我收到的信息不是固定长度的

我目前的执行情况如下:

public PedResponse send(ByteBuffer message) {
        String returnString;
        try {

            message.flip();

            while (message.hasRemaining()) {
                socketChannel.write(message);
            }

            ByteBuffer readBuffer = ByteBuffer.allocate(5000);
            int bufferSize = socketChannel.read(readBuffer);
            if (bufferSize == -1) {
                socketChannel.close();
            } else {
                readBuffer.flip();
            }

            returnString = new String(deconstructMessage(readBuffer.array()), "UTF-8").trim();

            response = parser.parseResponse(returnString);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

private ByteBuffer constructMessage(byte[] data) {

        // Construct a buffer where we'll put the data to send
        ByteBuffer sendBuffer = ByteBuffer.allocate(4 + data.length);

        // it's the default, but included for clarity
        sendBuffer.order(ByteOrder.BIG_ENDIAN);

        // Put the 4-byte length, then the data
        sendBuffer.putInt(data.length);
        sendBuffer.put(data);

        // Extract the actual bytes from our sendBuffer
        return sendBuffer;
    } 

public byte[] deconstructMessage(byte[] data) {

        byte[] filteredByteArray = Arrays.copyOfRange(data, 4, data.length - 4);

        return filteredByteArray;
    }

//Currently not being used.
private byte[] deconstructMessage(byte[] data) {

        // Construct a buffer where we'll put the data to send
        ByteBuffer receiveBuffer = ByteBuffer.allocate(data.length);

        // it's the default, but included for clarity
        receiveBuffer.order(ByteOrder.BIG_ENDIAN);

        // Get the 4-byte length, then the data
        receiveBuffer.getInt(data.length);
        receiveBuffer.get(data);

        // Extract the actual bytes from our receivedBuffer
        byte[] dataReceived = receiveBuffer.array();
        return dataReceived;
    }
如你所见,我正在创建一个新的ByteBuffer,大小为5000,但理想情况下我不想这样做。所以我的问题是,我怎么能不使用这个超大的ByteBuffer,而只是读取我收到的数据,这样我就可以使用我未使用的解构消息方法呢?

ByteBuffer有,它从当前位置返回字节。 因此,您可以先读取4个字节并获得大小。

ByteBuffer有,它从当前位置返回字节。
因此,您可以先读取4个字节并获得大小。

因此在send方法中,我创建了一个名为readBuffer的新ByteBuffer。所以理想情况下我想摆脱它。我的最佳选择是什么?据我所知,ByteBuffer中没有unwrap()方法。无论如何,您需要自己的字节[]来从缓冲区复制数据,而不是5000,您可以使用方法remaining()??ByteBuffer缓冲区=null;缓冲器位置(0);缓冲限值(1);而(buffer.haslaining()){socketChannel.read(buffer);}buffer.rewind()。。。检查此链接可能会帮助您在send方法中,创建一个名为readBuffer的新ByteBuffer。所以理想情况下我想摆脱它。我的最佳选择是什么?据我所知,ByteBuffer中没有unwrap()方法。无论如何,您需要自己的字节[]来从缓冲区复制数据,而不是5000,您可以使用方法remaining()??ByteBuffer缓冲区=null;缓冲器位置(0);缓冲限值(1);而(buffer.haslaining()){socketChannel.read(buffer);}buffer.rewind()。。。查看此链接,它可能会帮助您解决问题