Skip to content

Commit fb1ab48

Browse files
committed
src: reduce code duplication for files' checksum calculation
1 parent 76205dd commit fb1ab48

3 files changed

Lines changed: 47 additions & 48 deletions

File tree

src/main/java/org/dcache/nearline/cta/CtaCli.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.dcache.nearline.cta;
22

3+
import static org.dcache.nearline.cta.Utils.calculateChecksum;
34
import static org.dcache.nearline.cta.CtaNearlineStorage.CTA_ENDPOINT;
45
import static org.dcache.nearline.cta.CtaNearlineStorage.CTA_GROUP;
56
import static org.dcache.nearline.cta.CtaNearlineStorage.CTA_INSTANCE;
@@ -13,14 +14,11 @@
1314
import com.google.common.util.concurrent.Futures;
1415
import com.google.common.util.concurrent.ListenableFuture;
1516
import cta.admin.CtaAdmin;
16-
import diskCacheV111.util.Adler32;
1717
import io.grpc.InsecureChannelCredentials;
1818
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
1919
import io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoopGroup;
2020
import io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel;
2121
import org.dcache.pool.nearline.spi.FlushRequest;
22-
import org.dcache.util.Checksum;
23-
import org.dcache.util.ChecksumType;
2422
import org.dcache.vehicles.FileAttributes;
2523
import org.slf4j.Logger;
2624
import org.slf4j.LoggerFactory;
@@ -29,8 +27,6 @@
2927
import java.io.IOException;
3028
import java.net.InetAddress;
3129
import java.net.URI;
32-
import java.nio.ByteBuffer;
33-
import java.nio.channels.FileChannel;
3430
import java.nio.file.Files;
3531
import java.nio.file.Path;
3632
import java.nio.file.attribute.PosixFileAttributeView;
@@ -252,25 +248,6 @@ public void completed(Set<URI> uris) {
252248
}
253249
};
254250
}
255-
256-
257-
private static Checksum calculateChecksum(File file) throws IOException {
258-
259-
ByteBuffer bb = ByteBuffer.allocate(8192);
260-
var adler = new Adler32();
261-
try (FileChannel fc = FileChannel.open(file.toPath())) {
262-
while (true) {
263-
bb.clear();
264-
int n = fc.read(bb);
265-
if (n < 0) {
266-
break;
267-
}
268-
bb.flip();
269-
adler.update(bb);
270-
}
271-
return new Checksum(ChecksumType.ADLER32, adler.digest());
272-
}
273-
}
274251
}
275252

276253

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.dcache.nearline.cta;
2+
3+
import diskCacheV111.util.Adler32;
4+
import org.dcache.util.Checksum;
5+
import org.dcache.util.ChecksumType;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.ByteBuffer;
10+
import java.nio.channels.FileChannel;
11+
12+
/**
13+
* This class is a collection of utility methods to reduce code duplication.
14+
*/
15+
public class Utils {
16+
private Utils(){ /* prevent instantiation */ }
17+
18+
19+
/**
20+
* Calculates the ADLER32 checksum of a file.
21+
*
22+
* @param file the file to calculate the checksum for
23+
* @return the ADLER32 checksum of the file
24+
* @throws IOException if an I/O error occurs
25+
*/
26+
public static Checksum calculateChecksum(File file) throws IOException {
27+
28+
ByteBuffer bb = ByteBuffer.allocate(8192);
29+
var adler = new Adler32();
30+
31+
try (FileChannel fc = FileChannel.open(file.toPath())) {
32+
while (true) {
33+
bb.clear();
34+
int n = fc.read(bb);
35+
if (n < 0) {
36+
break;
37+
}
38+
bb.flip();
39+
adler.update(bb);
40+
}
41+
42+
return new Checksum(ChecksumType.ADLER32, adler.digest());
43+
}
44+
}
45+
}

src/main/java/org/dcache/nearline/cta/xrootd/DataServerHandler.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.dcache.nearline.cta.xrootd;
1818

19+
import static org.dcache.nearline.cta.Utils.calculateChecksum;
1920
import static org.dcache.xrootd.protocol.XrootdProtocol.kXR_ArgInvalid;
2021
import static org.dcache.xrootd.protocol.XrootdProtocol.kXR_FileNotOpen;
2122
import static org.dcache.xrootd.protocol.XrootdProtocol.kXR_IOError;
@@ -30,15 +31,13 @@
3031

3132
import com.google.common.net.InetAddresses;
3233
import com.sun.nio.file.ExtendedOpenOption;
33-
import diskCacheV111.util.Adler32;
3434
import diskCacheV111.util.CacheException;
3535
import io.netty.channel.ChannelHandlerContext;
3636
import java.io.File;
3737
import java.io.FileNotFoundException;
3838
import java.io.IOException;
3939
import java.net.InetSocketAddress;
4040
import java.net.URI;
41-
import java.nio.ByteBuffer;
4241
import java.nio.channels.ClosedChannelException;
4342
import java.nio.channels.FileChannel;
4443
import java.nio.charset.StandardCharsets;
@@ -48,7 +47,6 @@
4847
import java.time.Instant;
4948
import java.util.ArrayList;
5049
import java.util.Base64;
51-
import java.util.EnumSet;
5250
import java.util.HashSet;
5351
import java.util.List;
5452
import java.util.Set;
@@ -63,7 +61,6 @@
6361
import org.dcache.pool.nearline.spi.NearlineRequest;
6462
import org.dcache.pool.nearline.spi.StageRequest;
6563
import org.dcache.util.Checksum;
66-
import org.dcache.util.ChecksumType;
6764
import org.dcache.util.Strings;
6865
import org.dcache.util.TimeUtils;
6966
import org.dcache.xrootd.core.XrootdException;
@@ -599,26 +596,6 @@ private FileStatus statusByHandle(int handle) throws XrootdException {
599596
}
600597
}
601598

602-
private static Checksum calculateChecksum(File file) throws IOException {
603-
604-
ByteBuffer bb = ByteBuffer.allocate(8192);
605-
var adler = new Adler32();
606-
607-
try (FileChannel fc = FileChannel.open(file.toPath())) {
608-
while (true) {
609-
bb.clear();
610-
int n = fc.read(bb);
611-
if (n < 0) {
612-
break;
613-
}
614-
bb.flip();
615-
adler.update(bb);
616-
}
617-
618-
return new Checksum(ChecksumType.ADLER32, adler.digest());
619-
}
620-
}
621-
622599
private String getPnfsId(NearlineRequest<?> request) {
623600

624601
if (request instanceof StageRequest) {

0 commit comments

Comments
 (0)