简体   繁体   English

将InputStream转换为固定长度的字符串的Stream <String>

[英]Convert InputStream into Stream<String> of strings of fixed length

Like in Convert InputStream into Stream<String> given a Charset I want to convert an InputStream is into a Stream<String> stream . 就像在转换InputStream到Stream <String>中给定一个Charset我想将InputStream is转换为Stream<String> stream But this time instead of splitting the InputStream at the new line characters, I want to split it into parts of equal length. 但这次不是将InputStream拆分为新行字符,而是将其拆分为相等长度的部分。 So all strings of the stream would have the same length (with a possible exception on the last element of the stream, that may be shorter). 因此,流的所有字符串都具有相同的长度(在流的最后一个元素上可能有例外,可能更短)。

I don't think this is possible using class library methods only, so you'll have to write your own logic that follows the same pattern as BufferedReader.lines : 我不认为这只能使用类库方法,所以你必须编写自己的逻辑,遵循与BufferedReader.lines相同的模式:

  1. InputStreamReader - Start by creating an InputStreamReader InputStreamReader - 首先创建一个InputStreamReader
  2. Iterator<String> - Implement a custom Iterator subclass that splits the stream into pieces however you want. Iterator<String> - 实现一个自定义Iterator子类,可以根据需要将流拆分为多个部分。 It sounds like you want to implement hasNext() and next() to call a readPart() that reads at most N characters. 听起来你想要实现hasNext()next()来调用最多读取N个字符的readPart()
  3. Spliterators.spliteratorUnknownSize - Pass the iterator to this method to create a Spliterator. Spliterators.spliteratorUnknownSize - 将迭代器传递给此方法以创建Spliterator。
  4. StreamSupport.stream - Pass the Spliterator to this method to create a stream. StreamSupport.stream - 将Spliterator传递给此方法以创建流。

Ultimately, the class library just doesn't have builtins for reading from an input stream and converting into fixed-size strings, so you have to write those for #1/#2. 最终,类库没有用于从输入流中读取并转换为固定大小字符串的内置函数,因此您必须为#1 /#2编写它们。 After that, converting to a stream in #3/#4 isn't too bad since there are class library methods to help. 之后,转换为#3 /#4中的流并不算太糟糕,因为有类库方法可以提供帮助。

There is no direct support for this. 没有直接的支持。 You can create a straight-forward factory method: 您可以创建一个直接的工厂方法:

static Stream<String> strings(InputStream is, Charset cs, int size) {
    Reader r=new InputStreamReader(is, cs);
    CharBuffer cb=CharBuffer.allocate(size);
    return StreamSupport.stream(new Spliterators.AbstractSpliterator<String>(
        Long.MAX_VALUE, Spliterator.ORDERED|Spliterator.NONNULL) {
            public boolean tryAdvance(Consumer<? super String> action) {
                try { while(cb.hasRemaining() && r.read(cb)>0); }
                catch(IOException ex) { throw new UncheckedIOException(ex); }
                cb.flip();
                if(!cb.hasRemaining()) return false;
                action.accept(cb.toString());
                cb.clear();
                return true;
            }
        }, false).onClose(()->{
            try { r.close(); }catch(IOException ex) { throw new UncheckedIOException(ex); }
        });
}

It can be used like: 它可以像:

try(Stream<String> chunks=strings(is, StandardCharsets.UTF_8, 100)) {
    // perform operation with chunks
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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