2121 * questions.
2222 */
2323
24+ /*
25+ * ===========================================================================
26+ * (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
27+ * ===========================================================================
28+ */
29+
2430/*
2531 * @test
2632 * @bug 8271308
@@ -53,10 +59,15 @@ public class Transfer2GPlus {
5359
5460 public static void main (String [] args ) throws IOException {
5561 Path src = Files .createTempFile ("src" , ".dat" );
56- src .toFile ().deleteOnExit ();
57- byte [] b = createSrcFile (src );
58- testToFileChannel (src , b );
59- testToWritableByteChannel (src , b );
62+ File srcFile = src .toFile ();
63+ srcFile .deleteOnExit ();
64+ try {
65+ byte [] b = createSrcFile (src );
66+ testToFileChannel (src , b );
67+ testToWritableByteChannel (src , b );
68+ } finally {
69+ srcFile .delete ();
70+ }
6071 }
6172
6273 // Create a file of size LENGTH with EXTRA random bytes at offset BASE.
@@ -77,38 +88,43 @@ private static byte[] createSrcFile(Path src)
7788 private static void testToFileChannel (Path src , byte [] expected )
7889 throws IOException {
7990 Path dst = Files .createTempFile ("dst" , ".dat" );
80- dst .toFile ().deleteOnExit ();
81- try (FileChannel srcCh = FileChannel .open (src )) {
82- try (FileChannel dstCh = FileChannel .open (dst ,
83- StandardOpenOption .READ , StandardOpenOption .WRITE )) {
84- long total = 0L ;
85- if ((total = srcCh .transferTo (0 , LENGTH , dstCh )) < LENGTH ) {
86- if (!Platform .isLinux ())
87- throw new RuntimeException ("Transfer too small: " + total );
91+ File dstFile = dst .toFile ();
92+ dstFile .deleteOnExit ();
93+ try {
94+ try (FileChannel srcCh = FileChannel .open (src )) {
95+ try (FileChannel dstCh = FileChannel .open (dst ,
96+ StandardOpenOption .READ , StandardOpenOption .WRITE )) {
97+ long total = 0L ;
98+ if ((total = srcCh .transferTo (0 , LENGTH , dstCh )) < LENGTH ) {
99+ if (!Platform .isLinux ())
100+ throw new RuntimeException ("Transfer too small: " + total );
88101
89- // If this point is reached we're on Linux which cannot
90- // transfer all LENGTH bytes in one call to sendfile(2),
91- // so loop to get the rest.
92- do {
93- long n = srcCh .transferTo (total , LENGTH , dstCh );
94- if (n == 0 )
95- break ;
96- total += n ;
97- } while (total < LENGTH );
98- }
102+ // If this point is reached we're on Linux which cannot
103+ // transfer all LENGTH bytes in one call to sendfile(2),
104+ // so loop to get the rest.
105+ do {
106+ long n = srcCh .transferTo (total , LENGTH , dstCh );
107+ if (n == 0 )
108+ break ;
109+ total += n ;
110+ } while (total < LENGTH );
111+ }
99112
100- if (dstCh .size () < LENGTH )
101- throw new RuntimeException ("Target file too small: " +
102- dstCh .size () + " < " + LENGTH );
113+ if (dstCh .size () < LENGTH )
114+ throw new RuntimeException ("Target file too small: " +
115+ dstCh .size () + " < " + LENGTH );
103116
104- System .out .println ("Transferred " + total + " bytes" );
117+ System .out .println ("Transferred " + total + " bytes" );
105118
106- dstCh .position (BASE );
107- ByteBuffer bb = ByteBuffer .allocate (EXTRA );
108- dstCh .read (bb );
109- if (!Arrays .equals (bb .array (), expected ))
110- throw new RuntimeException ("Unexpected values" );
119+ dstCh .position (BASE );
120+ ByteBuffer bb = ByteBuffer .allocate (EXTRA );
121+ dstCh .read (bb );
122+ if (!Arrays .equals (bb .array (), expected ))
123+ throw new RuntimeException ("Unexpected values" );
124+ }
111125 }
126+ } finally {
127+ dstFile .delete ();
112128 }
113129 }
114130
@@ -117,27 +133,31 @@ private static void testToWritableByteChannel(Path src, byte[] expected)
117133 throws IOException {
118134 File file = File .createTempFile ("dst" , ".dat" );
119135 file .deleteOnExit ();
120- try (FileChannel srcCh = FileChannel .open (src )) {
121- // The FileOutputStream is wrapped so that newChannel() does not
122- // return a FileChannelImpl and so make a faster path be taken.
123- try (DataOutputStream stream =
124- new DataOutputStream (new FileOutputStream (file ))) {
125- try (WritableByteChannel wbc = Channels .newChannel (stream )) {
126- long n ;
127- if ((n = srcCh .transferTo (0 , LENGTH , wbc )) < LENGTH )
128- throw new RuntimeException ("Too few bytes transferred: " +
129- n + " < " + LENGTH );
136+ try {
137+ try (FileChannel srcCh = FileChannel .open (src )) {
138+ // The FileOutputStream is wrapped so that newChannel() does not
139+ // return a FileChannelImpl and so make a faster path be taken.
140+ try (DataOutputStream stream =
141+ new DataOutputStream (new FileOutputStream (file ))) {
142+ try (WritableByteChannel wbc = Channels .newChannel (stream )) {
143+ long n ;
144+ if ((n = srcCh .transferTo (0 , LENGTH , wbc )) < LENGTH )
145+ throw new RuntimeException ("Too few bytes transferred: " +
146+ n + " < " + LENGTH );
130147
131- System .out .println ("Transferred " + n + " bytes" );
148+ System .out .println ("Transferred " + n + " bytes" );
132149
133- RandomAccessFile raf = new RandomAccessFile (file , "r" );
134- raf .seek (BASE );
135- byte [] b = new byte [EXTRA ];
136- raf .read (b );
137- if (!Arrays .equals (b , expected ))
138- throw new RuntimeException ("Unexpected values" );
150+ RandomAccessFile raf = new RandomAccessFile (file , "r" );
151+ raf .seek (BASE );
152+ byte [] b = new byte [EXTRA ];
153+ raf .read (b );
154+ if (!Arrays .equals (b , expected ))
155+ throw new RuntimeException ("Unexpected values" );
156+ }
139157 }
140158 }
159+ } finally {
160+ file .delete ();
141161 }
142162 }
143163}
0 commit comments