Repository navigation
Expand file tree
/
Copy pathFileUtils.java
More file actions
161 lines (149 loc) · 4.96 KB
/
Copy pathFileUtils.java
File metadata and controls
161 lines (149 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2015 Metamarkets Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metamx.common;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Collection;
public class FileUtils
{
/**
* Useful for retry functionality that doesn't want to stop Throwables, but does want to retry on Exceptions
*/
public static final Predicate<Throwable> IS_EXCEPTION = new Predicate<Throwable>()
{
@Override
public boolean apply(Throwable input)
{
return input instanceof Exception;
}
};
/**
* Copy input byte source to outFile. If outFile exists, it is attempted to be deleted.
*
* @param byteSource Supplier for an input stream that is to be copied. The resulting stream is closed each iteration
* @param outFile Where the file should be written to.
* @param shouldRetry Predicate indicating if an error is recoverable and should be retried.
* @param maxAttempts The maximum number of assumed recoverable attempts to try before completely failing.
*
* @throws java.lang.RuntimeException wrapping the inner exception on failure.
*/
public static FileCopyResult retryCopy(
final ByteSource byteSource,
final File outFile,
final Predicate<Throwable> shouldRetry,
final int maxAttempts
)
{
try {
StreamUtils.retryCopy(
byteSource,
Files.asByteSink(outFile),
shouldRetry,
maxAttempts
);
return new FileCopyResult(outFile);
}
catch (Exception e) {
throw Throwables.propagate(e);
}
}
/**
* Keeps results of a file copy, including children and total size of the resultant files.
* This class is NOT thread safe.
* Child size is eagerly calculated and any modifications to the file after the child is added are not accounted for.
* As such, this result should be considered immutable, even though it has no way to force that property on the files.
*/
public static class FileCopyResult
{
private final Collection<File> files = Lists.newArrayList();
private long size = 0l;
public Collection<File> getFiles()
{
return ImmutableList.copyOf(files);
}
// Only works for immutable children contents
public long size()
{
return size;
}
public FileCopyResult(File... files)
{
this(files == null ? ImmutableList.<File>of() : Arrays.asList(files));
}
public FileCopyResult(Collection<File> files)
{
this.addSizedFiles(files);
}
protected void addSizedFiles(Collection<File> files)
{
if (files == null || files.isEmpty()) {
return;
}
long size = 0l;
for (File file : files) {
size += file.length();
}
this.files.addAll(files);
this.size += size;
}
public void addFiles(Collection<File> files)
{
this.addSizedFiles(files);
}
public void addFile(File file)
{
this.addFiles(ImmutableList.of(file));
}
}
/**
* Fully maps a file read-only in to memory as per
* {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
*
* <p>Files are mapped from offset 0 to its length.
*
* <p>This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* <p>Similar to {@link Files#map(File)}, but returns {@link MappedByteBufferHandler}, that makes it easier to unmap
* the buffer within try-with-resources pattern:
* <pre>{@code
* try (MappedByteBufferHandler fileMappingHandler = FileUtils.map(file)) {
* ByteBuffer fileMapping = fileMappingHandler.get();
* // use mapped buffer
* }}</pre>
*
* @param file the file to map
* @return a {@link MappedByteBufferHandler}, wrapping a read-only buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
*
* @see FileChannel#map(FileChannel.MapMode, long, long)
*/
public static MappedByteBufferHandler map(File file) throws IOException
{
MappedByteBuffer mappedByteBuffer = Files.map(file);
return new MappedByteBufferHandler(mappedByteBuffer);
}
}