Guest User

Untitled

a guest
Sep 14th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import static java.util.function.Function.identity;
  2. import static java.util.stream.Collectors.counting;
  3. import static java.util.stream.Collectors.groupingBy;
  4.  
  5. import java.io.*;
  6. import java.nio.*;
  7. import java.util.*;
  8. import java.nio.channels.FileChannel;
  9. import java.util.function.Supplier;
  10. import java.util.stream.Stream;
  11.  
  12. public class Main {
  13. public static void main(String... args) {
  14. try (RandomAccessFile f = new RandomAccessFile(new File("/tmp/wtf"), "r")) {
  15. long fileSize = f.length();
  16. MappedByteBuffer mem = f.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
  17. Map<Integer, Long> freqStat = Stream.generate(unsignedByteSupplier(mem))
  18. .limit(fileSize)
  19. .collect(groupingBy(identity(), counting()));
  20. System.out.println(freqStat);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. static Supplier<Integer> unsignedByteSupplier(ByteBuffer buff) {
  27. return () -> Byte.toUnsignedInt(buff.get());
  28. }
  29. }
Add Comment
Please, Sign In to add comment