Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.util.function.Function.identity;
- import static java.util.stream.Collectors.counting;
- import static java.util.stream.Collectors.groupingBy;
- import java.io.*;
- import java.nio.*;
- import java.util.*;
- import java.nio.channels.FileChannel;
- import java.util.function.Supplier;
- import java.util.stream.Stream;
- public class Main {
- public static void main(String... args) {
- try (RandomAccessFile f = new RandomAccessFile(new File("/tmp/wtf"), "r")) {
- long fileSize = f.length();
- MappedByteBuffer mem = f.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
- Map<Integer, Long> freqStat = Stream.generate(unsignedByteSupplier(mem))
- .limit(fileSize)
- .collect(groupingBy(identity(), counting()));
- System.out.println(freqStat);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- static Supplier<Integer> unsignedByteSupplier(ByteBuffer buff) {
- return () -> Byte.toUnsignedInt(buff.get());
- }
- }
Add Comment
Please, Sign In to add comment