简体   繁体   中英

ByteBuffer over SocketChannel

I am trying to get File moved over IP (SocketChannel) by starting to send the file name. The issue I believe is in the reading of the ByteBuffer but I am not sure how to correct it.

FileSender Part:

    public void sendFile(SocketChannel socketChannel) {
    try {
        File file = new File("B:\\Software\\OLD.zip");

        String filename = file.getName();
        byte[] nameBytes = filename.getBytes();

        ByteBuffer nameBuffer = ByteBuffer.wrap(nameBytes);
        socketChannel.write(nameBuffer);

        //FileChannel inChannel = aFile.getChannel();
        FileChannel inChannel = FileChannel.open(file.toPath());
        ByteBuffer buffer = ByteBuffer.allocate(32 * 1024); // 32.76800 kilobytes

        System.out.println(" Sendding nameBuffer: "+nameBuffer);

        int bytesread = inChannel.read(buffer);

        while (bytesread != -1) {
            buffer.flip();
            socketChannel.write(buffer);
            buffer.compact();
            bytesread = inChannel.read(buffer);
        }

        Thread.sleep(1000);
        System.out.println(" System Info! @LargeFileSender - End of file reached!");
        socketChannel.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

Here is the Part of the FileReceiver that I think is causing the issue.

    public void readFileFromSocket(SocketChannel socketChannel) {
    long startTime = System.currentTimeMillis();

    try {

        ByteBuffer namebuff = ByteBuffer.allocate(512);
        socketChannel.read(namebuff);

        System.out.println(" Receiving namebuff: " + namebuff);

        byte[] namebyte = new byte[512];
        String filename = "";
        int position = namebuff.position();

        System.out.println(" Receiving position: " + position);

        while (namebuff.hasRemaining()) {
            namebyte[position] = namebuff.get();
            position = namebuff.position();
        }

        filename = new String(namebyte, 0, position);

        System.out.println(" File namebyte: " + namebyte[7]);
        System.out.println(" File Name: " + filename);

        File file = new File(filename);

        ByteBuffer buffer = ByteBuffer.allocate(32 * 1024); // 32.76800

        FileOutputStream aFile = new FileOutputStream(file);

        FileChannel fileChannel = aFile.getChannel();

        BigDecimal bigDecimal = new BigDecimal(0.0);
        BigDecimal kilobyteDecimal = new BigDecimal(32.76);

        while (socketChannel.read(buffer) > 0) {
            buffer.flip();
            fileChannel.write(buffer);
            buffer.compact();
            bigDecimal = bigDecimal.add(kilobyteDecimal);
        }

        Thread.sleep(1000);
        fileChannel.close();
        buffer.clear();
        ProgressDial.main("false");
        Thread.sleep(2000);
        System.out.println(" System Info! @FileReceiver - Transfer completed!");

        socketChannel.close();
        aFile.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

personally I believe that the issue is here as the File Name is "null"

            while (namebuff.hasRemaining()) {
            namebyte[position] = namebuff.get();
            position = namebuff.position();
        }

        filename = new String(namebyte, 0, position);

Any help would be greatly appreciated.

many Thanks

After allot of testing and trying I have finally fixed my issue using StringBuilder and the result is exactly what I wanted: FileName:

        try {

        ByteBuffer namebuff = ByteBuffer.allocate(256);
        socketChannel.read(namebuff);
        int position = namebuff.position();
        namebuff.rewind();
        String filename = "";
        int startPosition = 0;

        System.out.println(" Receiving position: "+position);
        StringBuilder sb = new StringBuilder();

        while (startPosition < position) {
            sb.append((char)namebuff.get());
            startPosition++;
        }

        filename = sb.toString();
        System.out.println(" FileName: "+filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM