小编典典

Java NIO通过ByteBuffer扫描某些字节和带节的单词

java

好的,所以我试图做的事情看起来应该很简单,但是有了这些新的NIO接口,事情就让我感到困惑!这是我要尝试的操作,我需要以字节为单位扫描文件,直到遇到某些字节为止!当我遇到这些特定字节时,需要获取该数据段并对其进行处理,然后继续并再次执行此操作。我本以为有了ByteBuffer中的所有这些标记,位置和限制,我就可以做到这一点,但是我似乎无法使其正常工作!到目前为止,这就是我所拥有的..

test.text:

this is a line of text a
this is line 2b
line 3
line 4
line etc.etc.etc.

Test.java:

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Test {
    public static final Charset ENCODING = Charset.forName("UTF-8");
    public static final byte[] NEWLINE_BYTE = {0x0A, 0x0D};

    public Test() {

        String pathString = "test.txt";

        //the path to the file
        Path path = Paths.get(pathString);

        try (FileChannel fc = FileChannel.open(path, 
                StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {            
            if (fc.size() > 0) {
                int n;
                ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
                do {                    
                    n = fc.read(buffer);
                } while (n != -1 && buffer.hasRemaining());
                buffer.flip();
                int pos = 0;
                System.out.println("FILE LOADED: |" + new String(buffer.array(), ENCODING) + "|");
                do {
                    byte b = buffer.get();
                    if (b == NEWLINE_BYTE[0] || b == NEWLINE_BYTE[1]) {
                        System.out.println("POS: " + pos);
                        System.out.println("POSITION: " + buffer.position());
                        System.out.println("LENGTH: " + Integer.toString(buffer.position() - pos));
                        ByteBuffer lineBuffer = ByteBuffer.wrap(buffer.array(), pos + 1, buffer.position() - pos);
                        System.out.println("LINE: |" + new String(lineBuffer.array(), ENCODING) + "|");
                        pos = buffer.position();
                    }
                } while (buffer.hasRemaining());
            } 
        } catch (IOException ioe) {
           ioe.printStackTrace();
        }
    }
    public static void main(String args[]) {
        Test t = new Test();
    }
}

因此,第一部分开始工作,fc.read(buffer)函数仅运行一次并将整个文件拉入ByteBuffer。然后在第二个do循环中,我可以逐个字节地循环播放,当它命中\
n(或\
r)时,它确实命中了if语句,但后来我不知道该如何获取我刚刚浏览过的字节的一部分进入了一个单独的字节数组以供使用!我已经尝试了拼接和各种翻转,并且尝试了如上面的代码中所示的wrap,但是似乎无法使其正常工作,两个缓冲区都始终具有完整的文件,因此我拼接或结束的所有内容也是如此!

我只需要逐字节地遍历文件,一次查看某个部分,然后是我的最终目标,当我查看并找到正确的位置时,我想在正确的位置插入一些数据!我需要lineBuffer输出的lineBuffer,以使到目前为止只有我循环的那部分字节!帮帮忙,谢谢!


阅读 242

收藏
2020-11-26

共1个答案

小编典典

这是我最终得到的解决方案,每次使用ByteBuffer的批量相对get函数来获取块。我想我正在按预期使用mark()功能,尽管我正在使用其他变量(pos)来跟踪标记,因为我无法在ByteBuffer中找到函数来返回标记本身的相对位置。另外,我还有显式的功能可以按顺序查找\
r,\ n或两者。请记住,此代码仅适用于UTF-8编码的数据。我希望这可以帮助其他人。

public class Test {
    public static final Charset ENCODING = Charset.forName("UTF-8");
    public static final byte[] NEWLINE_BYTES = {0x0A, 0x0D};

    public Test() {
        //test text file sequence of any strings followed by newline
        String pathString = "test.txt";
        Path path = Paths.get(pathString);

        try (FileChannel fc = FileChannel.open(path, 
                StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {

            if (fc.size() > 0) {
                int n;
                ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
                do {                    
                    n = fc.read(buffer);
                } while (n != -1 && buffer.hasRemaining());
                buffer.flip();
                int newlineByteCount = 0;
                buffer.mark();
                do {
                    //get one byte at a time
                    byte b = buffer.get();

                    if (b == NEWLINE_BYTES[0] || b == NEWLINE_BYTES[1]) {
                        newlineByteCount++;

                        byte nextByte = buffer.get();
                        if (nextByte == NEWLINE_BYTES[1]) {
                            newlineByteCount++;
                        } else {
                            buffer.position(buffer.position() - 1);
                        }

                        int pos = buffer.position();
                        //reset the buffer back to the mark() position
                        buffer.reset();
                        //create an array just the right length and get the bytes we just measured out 
                        int length = pos - buffer.position() - newlineByteCount;
                        byte[] lineBytes = new byte[length];
                        buffer.get(lineBytes, 0, length);

                        String lineString = new String(lineBytes, ENCODING);
                        System.out.println("LINE: " + lineString);

                        buffer.position(buffer.position() + newlineByteCount);

                        buffer.mark();
                        newlineByteCount = 0;
                    } else if (newlineByteCount > 0) {

                    }
                } while (buffer.hasRemaining());
            } 
        } catch (IOException ioe) { ioe.printStackTrace(); }
    }
    public static void main(String args[]) { new Test(); }
}
2020-11-26