1
+ package modern .challenge ;
2
+
3
+ import java .nio .MappedByteBuffer ;
4
+ import java .io .OutputStream ;
5
+ import java .io .InputStream ;
6
+ import java .io .BufferedInputStream ;
7
+ import java .io .BufferedOutputStream ;
8
+ import java .io .File ;
9
+ import java .io .FileInputStream ;
10
+ import java .io .FileOutputStream ;
11
+ import java .io .IOException ;
12
+ import java .nio .ByteBuffer ;
13
+ import java .nio .channels .FileChannel ;
14
+ import java .nio .file .Files ;
15
+ import java .nio .file .Path ;
16
+ import java .nio .file .Paths ;
17
+ import java .nio .file .StandardOpenOption ;
18
+ import java .util .EnumSet ;
19
+ import static java .nio .file .LinkOption .NOFOLLOW_LINKS ;
20
+ import java .util .Random ;
21
+ import java .util .concurrent .TimeUnit ;
22
+ import org .openjdk .jmh .annotations .Benchmark ;
23
+ import org .openjdk .jmh .annotations .BenchmarkMode ;
24
+ import org .openjdk .jmh .annotations .Fork ;
25
+ import org .openjdk .jmh .annotations .Measurement ;
26
+ import org .openjdk .jmh .annotations .Mode ;
27
+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
28
+ import org .openjdk .jmh .annotations .Scope ;
29
+ import org .openjdk .jmh .annotations .State ;
30
+
31
+ @ OutputTimeUnit (TimeUnit .SECONDS )
32
+ @ BenchmarkMode (Mode .AverageTime )
33
+ @ Fork (value = 1 , warmups = 1 ) //, jvmArgsPrepend = {"-Djdk.net.usePlainSocketImpl=true"})
34
+ @ Measurement (iterations = 1 , time = 1 )
35
+ @ State (Scope .Benchmark )
36
+ public class Main {
37
+
38
+ private static final Path COPY_FROM = Paths .get ("rafa_org/Rafa Best Shots.mp4" );
39
+ private static final Path COPY_TO = Paths .get ("rafa_copy/" );
40
+ private static final int BUFFER_SIZE_KB = 4 ;
41
+ private static final int BUFFER_SIZE = BUFFER_SIZE_KB * 1024 ;
42
+
43
+ private static final Random rnd = new Random ();
44
+
45
+ @ Benchmark
46
+ public static void fileChannelIndirectBuffer () {
47
+
48
+ System .out .println ("Using FileChannel and non-direct buffer ..." );
49
+
50
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
51
+ try (FileChannel fileChannel_from = (FileChannel .open (COPY_FROM , EnumSet .of (StandardOpenOption .READ )));
52
+ FileChannel fileChannel_to = (FileChannel .open (copyTo , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE )))) {
53
+
54
+ // Allocate an non-direct ByteBuffer
55
+ ByteBuffer bytebuffer = ByteBuffer .allocate (BUFFER_SIZE );
56
+
57
+ // Read data from file into ByteBuffer
58
+ while ((fileChannel_from .read (bytebuffer )) > 0 ) {
59
+
60
+ //flip the buffer which set the limit to current position, and position to 0
61
+ bytebuffer .flip ();
62
+
63
+ //write data from ByteBuffer to file
64
+ fileChannel_to .write (bytebuffer );
65
+
66
+ //for the next read
67
+ bytebuffer .clear ();
68
+ }
69
+ } catch (IOException ex ) {
70
+ System .err .println (ex );
71
+ }
72
+ }
73
+
74
+ @ Benchmark
75
+ public static void fileChannelDirectBuffer () {
76
+
77
+ System .out .println ("Using FileChannel and direct buffer ..." );
78
+
79
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
80
+ try (FileChannel fileChannel_from = (FileChannel .open (COPY_FROM , EnumSet .of (StandardOpenOption .READ )));
81
+ FileChannel fileChannel_to = (FileChannel .open (copyTo , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE )))) {
82
+
83
+ // Allocate an direct ByteBuffer
84
+ ByteBuffer bytebuffer = ByteBuffer .allocateDirect (BUFFER_SIZE );
85
+
86
+ // Read data from file into ByteBuffer
87
+ while ((fileChannel_from .read (bytebuffer )) > 0 ) {
88
+
89
+ //flip the buffer which set the limit to current position, and position to 0
90
+ bytebuffer .flip ();
91
+
92
+ //write data from ByteBuffer to file
93
+ fileChannel_to .write (bytebuffer );
94
+
95
+ //for the next read
96
+ bytebuffer .clear ();
97
+ }
98
+ } catch (IOException ex ) {
99
+ System .err .println (ex );
100
+ }
101
+ }
102
+
103
+ @ Benchmark
104
+ public static void fileChannelTransferTo () {
105
+
106
+ System .out .println ("Using FileChannel.transferTo method ..." );
107
+
108
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
109
+ try (FileChannel fileChannel_from = (FileChannel .open (
110
+ COPY_FROM , EnumSet .of (StandardOpenOption .READ )));
111
+ FileChannel fileChannel_to = (FileChannel .open (copyTo , EnumSet .of (
112
+ StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE )))) {
113
+
114
+ fileChannel_from .transferTo (0L , fileChannel_from .size (), fileChannel_to );
115
+
116
+ } catch (IOException ex ) {
117
+ System .err .println (ex );
118
+ }
119
+ }
120
+
121
+ @ Benchmark
122
+ public static void fileChannelTransferFrom () {
123
+
124
+ System .out .println ("Using FileChannel.transferFrom method ..." );
125
+
126
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
127
+ try (FileChannel fileChannel_from = (FileChannel .open (
128
+ COPY_FROM , EnumSet .of (StandardOpenOption .READ )));
129
+ FileChannel fileChannel_to = (FileChannel .open (copyTo , EnumSet .of (
130
+ StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE )))) {
131
+
132
+ fileChannel_to .transferFrom (fileChannel_from , 0L , (int ) fileChannel_from .size ());
133
+ } catch (IOException ex ) {
134
+ System .err .println (ex );
135
+ }
136
+ }
137
+
138
+ @ Benchmark
139
+ public static void fileChannelMap () {
140
+
141
+ System .out .println ("Using FileChannel.map method ..." );
142
+
143
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
144
+ try (FileChannel fileChannel_from = (FileChannel .open (COPY_FROM , EnumSet .of (StandardOpenOption .READ )));
145
+ FileChannel fileChannel_to = (FileChannel .open (copyTo , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE )))) {
146
+
147
+ MappedByteBuffer buffer = fileChannel_from .map (
148
+ FileChannel .MapMode .READ_ONLY , 0 , fileChannel_from .size ());
149
+
150
+ fileChannel_to .write (buffer );
151
+ buffer .clear ();
152
+
153
+ } catch (IOException ex ) {
154
+ System .err .println (ex );
155
+ }
156
+ }
157
+
158
+ @ Benchmark
159
+ public static void bufferedStreamIO () {
160
+
161
+ System .out .println ("Using buffered streams and byte array ..." );
162
+
163
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
164
+ File inFileStr = COPY_FROM .toFile ();
165
+ File outFileStr = copyTo .toFile ();
166
+
167
+ try (BufferedInputStream in = new BufferedInputStream (
168
+ new FileInputStream (inFileStr )); BufferedOutputStream out
169
+ = new BufferedOutputStream (new FileOutputStream (outFileStr ))) {
170
+
171
+ byte [] byteArray = new byte [BUFFER_SIZE ];
172
+ int bytesCount ;
173
+ while ((bytesCount = in .read (byteArray )) != -1 ) {
174
+ out .write (byteArray , 0 , bytesCount );
175
+ }
176
+ } catch (IOException ex ) {
177
+ System .err .println (ex );
178
+ }
179
+ }
180
+
181
+ @ Benchmark
182
+ public static void bufferedStreamByteArray () {
183
+
184
+ System .out .println ("Using un-buffered streams and byte array ..." );
185
+
186
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
187
+ File inFileStr = COPY_FROM .toFile ();
188
+ File outFileStr = copyTo .toFile ();
189
+
190
+ try (FileInputStream in = new FileInputStream (inFileStr );
191
+ FileOutputStream out = new FileOutputStream (outFileStr )) {
192
+
193
+ byte [] byteArray = new byte [BUFFER_SIZE ];
194
+ int bytesCount ;
195
+ while ((bytesCount = in .read (byteArray )) != -1 ) {
196
+ out .write (byteArray , 0 , bytesCount );
197
+ }
198
+ } catch (IOException ex ) {
199
+ System .err .println (ex );
200
+ }
201
+ }
202
+
203
+ @ Benchmark
204
+ public static void filesCopyPathToPath () {
205
+
206
+ System .out .println ("Using Files.copy (Path to Path) method ..." );
207
+
208
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
209
+ try {
210
+ Files .copy (COPY_FROM , copyTo , NOFOLLOW_LINKS );
211
+ } catch (IOException e ) {
212
+ System .err .println (e );
213
+ }
214
+ }
215
+
216
+ @ Benchmark
217
+ public static void filesCopyIStreamToPath () {
218
+
219
+ System .out .println ("Using Files.copy (InputStream to Path) ..." );
220
+
221
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
222
+ try (InputStream is = new FileInputStream (COPY_FROM .toFile ())) {
223
+ Files .copy (is , copyTo );
224
+ } catch (IOException e ) {
225
+ System .err .println (e );
226
+ }
227
+ }
228
+
229
+ @ Benchmark
230
+ public static void filesCopyPathToOStream () {
231
+
232
+ System .out .println ("Using Files.copy (Path to OutputStream) ..." );
233
+
234
+ Path copyTo = COPY_TO .resolve ("Rafa Best Shots " + rnd .nextInt () + ".mp4" );
235
+ try (OutputStream os = new FileOutputStream (copyTo .toFile ())) {
236
+ Files .copy (COPY_FROM , os );
237
+ } catch (IOException e ) {
238
+ System .err .println (e );
239
+ }
240
+ }
241
+
242
+ public static void main (String [] args ) throws IOException {
243
+
244
+ org .openjdk .jmh .Main .main (args );
245
+ }
246
+ }
0 commit comments