Buffer简析

image.png

写模式

image.png
image.png

读模式

image.png
都读完:
image.png
读完了,想变成写模式,调用clear()函数
position到最开始的位置,limit到capacity位置

只读取一部分数据:
image.png
但是我又想回到写模式,这时候就调用compact()函数

Channel简析

image.png
image.png

代码演示几种拷贝文件的方法

  1. import java.io.*;
  2. import java.nio.ByteBuffer;
  3. import java.nio.channels.FileChannel;
  4. interface FileCopyRunner{
  5. void copyFile(File source, File target);
  6. }
  7. public class FileCopyDemo {
  8. private static final int ROUNDS = 5;
  9. private static void benchmark(FileCopyRunner test,File source, File target){
  10. long elapsed = 0L;
  11. for (int i =0; i < ROUNDS; i++){
  12. long startTime = System.currentTimeMillis();
  13. test.copyFile(source,target);
  14. elapsed += System.currentTimeMillis() - startTime;
  15. target.delete();
  16. }
  17. System.out.println(test + ": " + elapsed / ROUNDS);
  18. }
  19. private static void close(Closeable closeable){
  20. if (closeable != null){
  21. try {
  22. closeable.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  28. public static void main(String[] args) {
  29. FileCopyRunner noBufferStreamCopy = new FileCopyRunner() {
  30. @Override
  31. public void copyFile(File source, File target) {
  32. InputStream fin = null;
  33. OutputStream fout = null;
  34. try {
  35. fin = new FileInputStream(source);
  36. fout = new FileOutputStream(target);
  37. int result;
  38. while ((result = fin.read()) != -1){
  39. fout.write(result);
  40. }
  41. } catch (FileNotFoundException e) {
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }finally {
  46. close(fin);
  47. close(fout);
  48. }
  49. }
  50. @Override
  51. public String toString() {
  52. return "noBufferStreamCopy";
  53. }
  54. };
  55. FileCopyRunner bufferedStreamCopy = new FileCopyRunner() {
  56. @Override
  57. public void copyFile(File source, File target) {
  58. InputStream fin = null;
  59. OutputStream fout = null;
  60. try {
  61. fin = new BufferedInputStream(new FileInputStream(source));
  62. fout = new BufferedOutputStream(new FileOutputStream(target));
  63. byte[] buffer = new byte[1024];
  64. int result;
  65. while ((result = fin.read(buffer)) != -1){
  66. fout.write(buffer,0, result);
  67. }
  68. } catch (FileNotFoundException e) {
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }finally {
  73. close(fin);
  74. close(fout);
  75. }
  76. }
  77. @Override
  78. public String toString() {
  79. return "bufferedStreamCopy";
  80. }
  81. };
  82. FileCopyRunner nioBufferCopy = new FileCopyRunner() {
  83. @Override
  84. public void copyFile(File source, File target) {
  85. FileChannel fin = null;
  86. FileChannel fout = null;
  87. try {
  88. fin = new FileInputStream(source).getChannel();
  89. fout = new FileOutputStream(target).getChannel();
  90. ByteBuffer buffer = ByteBuffer.allocate(1024);
  91. while ((fin.read(buffer)) != -1){
  92. buffer.flip();
  93. while (buffer.hasRemaining()){
  94. fout.write(buffer);
  95. }
  96. buffer.clear();
  97. }
  98. } catch (FileNotFoundException e) {
  99. e.printStackTrace();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. @Override
  105. public String toString() {
  106. return "nioBufferCopy";
  107. }
  108. };
  109. FileCopyRunner nioTransferCopy = new FileCopyRunner() {
  110. @Override
  111. public void copyFile(File source, File target) {
  112. FileChannel fin = null;
  113. FileChannel fout = null;
  114. try {
  115. fin = new FileInputStream(source).getChannel();
  116. fout = new FileOutputStream(target).getChannel();
  117. long transferred = 0L;
  118. long size = fin.size();
  119. while (transferred != fin.size()){
  120. transferred += fin.transferTo(0, size, fout);
  121. }
  122. } catch (FileNotFoundException e) {
  123. e.printStackTrace();
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }finally {
  127. close(fin);
  128. close(fout);
  129. }
  130. }
  131. @Override
  132. public String toString() {
  133. return "nioTransferCopy";
  134. }
  135. };
  136. }
  137. }