当前位置: 首页 > 知识库问答 >
问题:

JavaNIO通过ByteBuffer扫描某些字节和带部分的字

阙阳
2023-03-14

好吧,所以我试图做一些看起来应该相当简单的事情,但是有了这些新的NIO接口,事情让我很困惑!这是我想做的,我需要扫描文件作为字节,直到遇到某些字节!当我遇到那些特定的字节时,需要抓住数据的那一部分并对其做一些事情,然后继续前进并再次这样做。我本以为在ByteBuffer中有了所有这些标记、位置和限制,我就能做到这一点,但我似乎无法让它工作!这是我到目前为止...

测验正文:

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语句,但是我不知道如何将刚才查看的那部分字节放入一个单独的字节数组中使用!我试过拼接和各种翻转,也试过按照上面代码所示的方式进行换行,但似乎无法正常工作,两个缓冲区都有完整的文件,我拼接或换行的任何内容都是如此!

我只需要一个字节一个字节地循环文件,每次查看某个部分,然后我的最终目标是,当我查看并找到正确的位置时,我想在正确的位置插入一些数据!我需要在“LINE:”处输出的lineBuffer,以便只包含到目前为止我循环使用的部分字节!帮帮忙,谢谢你!

共有3个答案

党建义
2023-03-14

我需要类似的东西,但比拆分单个缓冲区更一般。在我的例子中,我有多个缓冲区;事实上,我的代码是对Spring StringDecoder的修改,它可以转换通量

https://stackoverflow.com/a/48111196/839733

通寂离
2023-03-14

下面是我最终得到的解决方案,每次都使用ByteBuffer的bulk relative 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(); }
}
易烨磊
2023-03-14

撇开I/O不谈,一旦您在ByteBuffer中有了内容,通过asCharBuffer()将其转换为CharBuffer会简单得多。然后,CharBuffer实现了CharSequence,这为您提供了许多可使用的String和regex方法

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

  • 我创建了一个简单的扫描器,用于计算中的字符串数。txt文件。每个字符串位于下一行。它算错了,每次它给我数字297,甚至有超过20000个字符串。这个txt文件是由我编写的另一个程序创建的,它从网站获取链接,并使用FileWriter和BufferedWriter将其保存到。txt文件。可能有什么问题? 编辑:字符串示例:

  • 问题 假设我的工作是扫描从直接字节缓冲区读回的所有字节,那么对我来说最快的方法是什么? 我最初问“...利用sun.misc.unsafe”,但这可能是错误的假设。 null 这不同于“我可以使用Unsafe来更快地迭代一个字节[]吗?”问题是,如果没有必要,我甚至不打算在内部将字节拉到byte[]中。 谢谢你抽出时间;只是好奇如果有人(彼得?)做这样的事不安全。

  • 问题内容: 如何用另一部分替换字符串的特定部分? 输入字符串: 如何用其他方式更改字符串中的全部? 我认为我需要一个循环,但是我不确定如何使用它。 问题答案: 或者更精确地回答您的问题:

  • 我试图使用Jenkins、FxCop和Sonarqube分析一个C#项目。目前,我使用的构建步骤是“用于MSBuild-Begin Analysis的Sonarqube Scanner”、“FxCop Exec.”、“使用MSBuild构建Visual Studio项目或解决方案”和“用于MSBuild-End Analysis的Sonarqube Scanner”。当FxCop和SonarQub

  • 我有一个xml,我想提取其中的一部分。但我无法得到它。如果我使用变量并将每个键放入变量中,我可以得到那个部分,但这是一个非常漫长的过程。那幺,是否有任何简短的流程? 下面是 XML : 我想要< code>network link集合中的xml。