• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #801

27 Oct 2025 09:37AM UTC coverage: 84.715% (-0.4%) from 85.118%
#801

push

raphw
Fix imports.

29586 of 34924 relevant lines covered (84.72%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.02
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/FileSystem.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.utility;
17

18
import net.bytebuddy.build.AccessControllerPlugin;
19
import net.bytebuddy.build.CachedReturnPlugin;
20
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
21
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
22
import net.bytebuddy.utility.dispatcher.JavaDispatcher;
23

24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.security.PrivilegedAction;
31

32
/**
33
 * A dispatcher to interact with the file system. If NIO2 is available, the API is used. Otherwise, byte streams are used.
34
 */
35
public abstract class FileSystem {
1✔
36

37
    /**
38
     * Returns the {@link FileSystem} instance to use.
39
     *
40
     * @return The {@link FileSystem} instance to use.
41
     */
42
    @CachedReturnPlugin.Enhance("INSTANCE")
43
    public static FileSystem getInstance() {
44
        try {
45
            Class.forName("java.nio.file.Files", false, ClassLoadingStrategy.BOOTSTRAP_LOADER);
1✔
46
            return new ForNio2CapableVm();
1✔
47
        } catch (ClassNotFoundException ignored) {
×
48
            return new ForLegacyVm();
1✔
49
        }
50
    }
51

52
    /**
53
     * A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
54
     *
55
     * @param action The action to execute from a privileged context.
56
     * @param <T>    The type of the action's resolved value.
57
     * @return The action's resolved value.
58
     */
59
    @AccessControllerPlugin.Enhance
60
    private static <T> T doPrivileged(PrivilegedAction<T> action) {
61
        return action.run();
×
62
    }
63

64
    /**
65
     * Copies a file.
66
     *
67
     * @param source The source file.
68
     * @param target The target file.
69
     * @throws IOException If an I/O exception occurs.
70
     */
71
    public abstract void copy(File source, File target) throws IOException;
72

73
    /**
74
     * Links a file as a hard-link. If linking is not supported, a copy is made.
75
     *
76
     * @param source The source file.
77
     * @param target The target file.
78
     * @throws IOException If an I/O exception occurs.
79
     */
80
    public void link(File source, File target) throws IOException {
81
        copy(source, target);
×
82
    }
×
83

84
    /**
85
     * Moves a file.
86
     *
87
     * @param source The source file.
88
     * @param target The target file.
89
     * @throws IOException If an I/O exception occurs.
90
     */
91
    public abstract void move(File source, File target) throws IOException;
92

93
    /**
94
     * A file system representation for a VM that does not support NIO2.
95
     */
96
    @HashCodeAndEqualsPlugin.Enhance
97
    protected static class ForLegacyVm extends FileSystem {
×
98

99
        @Override
100
        public void copy(File source, File target) throws IOException {
101
            InputStream inputStream = new FileInputStream(source);
×
102
            try {
103
                OutputStream outputStream = new FileOutputStream(target);
×
104
                try {
105
                    byte[] buffer = new byte[1024];
×
106
                    int length;
107
                    while ((length = inputStream.read(buffer)) != -1) {
×
108
                        outputStream.write(buffer, 0, length);
×
109
                    }
110
                } finally {
111
                    outputStream.close();
×
112
                }
113
            } finally {
114
                inputStream.close();
×
115
            }
116
        }
×
117

118
        @Override
119
        public void move(File source, File target) throws IOException {
120
            InputStream inputStream = new FileInputStream(source);
×
121
            try {
122
                OutputStream outputStream = new FileOutputStream(target);
×
123
                try {
124
                    byte[] buffer = new byte[1024];
×
125
                    int length;
126
                    while ((length = inputStream.read(buffer)) != -1) {
×
127
                        outputStream.write(buffer, 0, length);
×
128
                    }
129
                } finally {
130
                    outputStream.close();
×
131
                }
132
            } finally {
133
                inputStream.close();
×
134
            }
135
            if (!source.delete()) {
×
136
                source.deleteOnExit();
×
137
            }
138
        }
×
139
    }
140

141
    /**
142
     * A file system representation for a VM that does support NIO2.
143
     */
144
    @HashCodeAndEqualsPlugin.Enhance
145
    protected static class ForNio2CapableVm extends FileSystem {
1✔
146

147
        /**
148
         * A dispatcher to resolve a {@link File} to a {@code java.nio.file.Path}.
149
         */
150
        private static final Dispatcher DISPATCHER = doPrivileged(JavaDispatcher.of(Dispatcher.class));
1✔
151

152
        /**
153
         * A dispatcher to resolve a dispatcher for {@code java.nio.file.Files}.
154
         */
155
        private static final Files FILES = doPrivileged(JavaDispatcher.of(Files.class));
1✔
156

157
        /**
158
         * A dispatcher to interact with {@code java.nio.file.StandardCopyOption}.
159
         */
160
        private static final StandardCopyOption STANDARD_COPY_OPTION = doPrivileged(JavaDispatcher.of(StandardCopyOption.class));
1✔
161

162
        @Override
163
        public void copy(File source, File target) throws IOException {
164
            Object[] option = STANDARD_COPY_OPTION.toArray(1);
1✔
165
            option[0] = STANDARD_COPY_OPTION.valueOf("REPLACE_EXISTING");
1✔
166
            FILES.copy(DISPATCHER.toPath(source), DISPATCHER.toPath(target), option);
1✔
167
        }
1✔
168

169
        @Override
170
        public void link(File source, File target) throws IOException {
171
            FILES.createLink(FILES.deleteIfExists(DISPATCHER.toPath(target)), DISPATCHER.toPath(source));
×
172
        }
×
173

174
        @Override
175
        public void move(File source, File target) throws IOException {
176
            Object[] option = STANDARD_COPY_OPTION.toArray(1);
1✔
177
            option[0] = STANDARD_COPY_OPTION.valueOf("REPLACE_EXISTING");
1✔
178
            FILES.move(DISPATCHER.toPath(source), DISPATCHER.toPath(target), option);
1✔
179
        }
1✔
180

181
        /**
182
         * A dispatcher to resolve a {@link File} to a {@code java.nio.file.Path}.
183
         */
184
        @JavaDispatcher.Proxied("java.io.File")
185
        protected interface Dispatcher {
186

187
            /**
188
             * Resolves a {@link File} to a {@code java.nio.file.Path}.
189
             *
190
             * @param value The file to convert.
191
             * @return The transformed {@code java.nio.file.Path}.
192
             * @throws IOException If an I/O exception occurs.
193
             */
194
            Object toPath(File value) throws IOException;
195
        }
196

197
        /**
198
         * A dispatcher to access the {@code java.nio.file.Files} API.
199
         */
200
        @JavaDispatcher.Proxied("java.nio.file.Files")
201
        protected interface Files {
202

203
            /**
204
             * Copies a file.
205
             *
206
             * @param source The source {@code java.nio.file.Path}.
207
             * @param target The target {@code java.nio.file.Path}.
208
             * @param option An array of copy options.
209
             * @return The copied file.
210
             * @throws IOException If an I/O exception occurs.
211
             */
212
            @JavaDispatcher.IsStatic
213
            Object copy(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
214
                        @JavaDispatcher.Proxied("java.nio.file.Path") Object target,
215
                        @JavaDispatcher.Proxied("java.nio.file.CopyOption") Object[] option) throws IOException;
216

217
            /**
218
             * Links a file.
219
             *
220
             * @param source The source {@code java.nio.file.Path}.
221
             * @param target The target {@code java.nio.file.Path}.
222
             * @return The copied file.
223
             * @throws IOException If an I/O exception occurs.
224
             */
225
            @JavaDispatcher.IsStatic
226
            Object createLink(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
227
                              @JavaDispatcher.Proxied("java.nio.file.Path") Object target) throws IOException;
228

229
            /**
230
             * Moves a file.
231
             *
232
             * @param source The source {@code java.nio.file.Path}.
233
             * @param target The target {@code java.nio.file.Path}.
234
             * @param option An array of copy options.
235
             * @return The moved file.
236
             * @throws IOException If an I/O exception occurs.
237
             */
238
            @JavaDispatcher.IsStatic
239
            Object move(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
240
                        @JavaDispatcher.Proxied("java.nio.file.Path") Object target,
241
                        @JavaDispatcher.Proxied("java.nio.file.CopyOption") Object[] option) throws IOException;
242

243
            /**
244
             * Deletes a file if it exists.
245
             *
246
             * @param file The {@code java.nio.file.Path} to delete if it exists.
247
             * @return The supplied file.
248
             * @throws IOException If an I/O exception occurs.
249
             */
250
            @JavaDispatcher.IsStatic
251
            Object deleteIfExists(@JavaDispatcher.Proxied("java.nio.file.Path") Object file) throws IOException;
252
        }
253

254
        /**
255
         * A dispatcher to interact with {@code java.nio.file.StandardCopyOption}.
256
         */
257
        @JavaDispatcher.Proxied("java.nio.file.StandardCopyOption")
258
        protected interface StandardCopyOption {
259

260
            /**
261
             * Creates an array of type {@code java.nio.file.StandardCopyOption}.
262
             *
263
             * @param size The array's size.
264
             * @return An array of type {@code java.nio.file.StandardCopyOption}.
265
             */
266
            @JavaDispatcher.Container
267
            Object[] toArray(int size);
268

269
            /**
270
             * Resolve an enumeration for {@code java.nio.file.StandardCopyOption}.
271
             *
272
             * @param name The enumeration name.
273
             * @return The enumeration value.
274
             */
275
            @JavaDispatcher.IsStatic
276
            Object valueOf(String name);
277
        }
278
    }
279
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc