Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. Stream Write: 0.37
  2. Mapped Write: Exception in thread "main" java.nio.BufferOverflowException
  3. at java.nio.Buffer.nextPutIndex(Buffer.java:521)
  4. at java.nio.DirectIntBufferS.put(DirectIntBufferS.java:297)
  5. at
  6. com.thinkInJava.study.ioSystem.MappedIO$2.test(MappedIO.java:52)
  7. at
  8. com.thinkInJava.study.ioSystem.MappedIO$Tester.runTest(MappedIO.java:21)
  9. at
  10. com.thinkInJava.study.ioSystem.MappedIO.main(MappedIO.java:106)
  11.  
  12. import java.nio.*;
  13. import java.nio.channels.*;
  14. import java.io.*;
  15.  
  16. public class MappedIO {
  17. //Byte size
  18. private static int numOfInts = 4000000;
  19. private static int numOfUbuffInts = 200000;
  20. //Test run time
  21. private abstract static class Tester {
  22. private String name;
  23. public Tester(String name) { this.name = name; }
  24. public void runTest() {
  25. System.out.print(name + ": ");
  26. try {
  27. long start = System.nanoTime();
  28. test();
  29. double duration = System.nanoTime() - start;
  30. //Convert to seconds
  31. System.out.format("%.2fn", duration/1.0e9);
  32. } catch(IOException e) {
  33. throw new RuntimeException(e);
  34. }
  35. }
  36. public abstract void test() throws IOException;
  37. }
  38.  
  39. //Compare the performance of six io streams
  40. private static Tester[] tests = {
  41. new Tester("Stream Write") {
  42. public void test() throws IOException {
  43. DataOutputStream dos = new DataOutputStream(
  44. new BufferedOutputStream(
  45. new FileOutputStream(new File("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFiles\temp.tmp"))));
  46. for(int i = 0; i < numOfInts; i++)
  47. dos.writeInt(i);
  48. dos.close();
  49. }
  50. },
  51. new Tester("Mapped Write") {
  52. public void test() throws IOException {
  53. FileChannel fc =
  54. new RandomAccessFile("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFilestemp.tmp", "rw")
  55. .getChannel();
  56. IntBuffer ib = fc.map(
  57. FileChannel.MapMode.READ_WRITE, 0, fc.size())
  58. .asIntBuffer();
  59. for(int i = 0; i < numOfInts; i++)
  60. ib.put(i);
  61. fc.close();
  62. }
  63. },
  64. new Tester("Stream Read") {
  65. public void test() throws IOException {
  66. DataInputStream dis = new DataInputStream(
  67. new BufferedInputStream(
  68. new FileInputStream("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFilestemp.tmp")));
  69. for(int i = 0; i < numOfInts; i++)
  70. dis.readInt();
  71. dis.close();
  72. }
  73. },
  74. new Tester("Mapped Read") {
  75. public void test() throws IOException {
  76. FileChannel fc = new FileInputStream(
  77. new File("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFilestemp.tmp")).getChannel();
  78. IntBuffer ib = fc.map(
  79. FileChannel.MapMode.READ_ONLY, 0, fc.size())
  80. .asIntBuffer();
  81. while(ib.hasRemaining())
  82. ib.get();
  83. fc.close();
  84. }
  85. },
  86. new Tester("Stream Read/Write") {
  87. public void test() throws IOException {
  88. RandomAccessFile raf = new RandomAccessFile(
  89. new File("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFilestemp.tmp"), "rw");
  90. raf.writeInt(1);
  91. for(int i = 0; i < numOfUbuffInts; i++) {
  92. raf.seek(raf.length() - 4);
  93. raf.writeInt(raf.readInt());
  94. }
  95. raf.close();
  96. }
  97. },
  98. new Tester("Mapped Read/Write") {
  99. public void test() throws IOException {
  100. FileChannel fc = new RandomAccessFile(
  101. new File("X:\IntelliJ IDEA 2019.1.3\JavaCode\src\com\thinkInJava\study\ioSystem\TestFilestemp.tmp"), "rw").getChannel();
  102. IntBuffer ib = fc.map(
  103. FileChannel.MapMode.READ_WRITE, 0, fc.size())
  104. .asIntBuffer();
  105. ib.put(0);
  106. for(int i = 1; i < numOfUbuffInts; i++)
  107. ib.put(ib.get(i - 1));
  108. fc.close();
  109. }
  110. }
  111. };
  112. public static void main(String[] args) {
  113. for(Tester test : tests)
  114. test.runTest();
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement