Skip to content

Commit 48e271c

Browse files
committed
Simplify DigestInputStream builder
1 parent f2c849f commit 48e271c

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/main/java/me/desair/tus/server/util/TusServletRequest.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.InputStream;
55
import java.security.DigestInputStream;
66
import java.security.MessageDigest;
7+
import java.util.Arrays;
8+
import java.util.Collections;
79
import java.util.EnumMap;
810
import java.util.List;
911
import java.util.Map;
@@ -25,7 +27,7 @@ public class TusServletRequest extends HttpServletRequestWrapper {
2527

2628
private CountingInputStream countingInputStream;
2729
private Map<ChecksumAlgorithm, DigestInputStream> digestInputStreamMap = new EnumMap<>(ChecksumAlgorithm.class);
28-
private DigestInputStream singleDigestInputStream = null;
30+
2931
private InputStream contentInputStream = null;
3032
private boolean isChunkedTransferDecodingEnabled = true;
3133

@@ -71,36 +73,34 @@ public InputStream getContentInputStream() throws IOException {
7173
ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.forUploadChecksumHeader(
7274
getHeader(HttpHeader.UPLOAD_CHECKSUM));
7375

76+
List<ChecksumAlgorithm> algorithms;
77+
7478
if (isChunked) {
7579
//Since the Checksum header can still come at the end, keep track of all checksums
76-
for (ChecksumAlgorithm algorithm : ChecksumAlgorithm.values()) {
77-
DigestInputStream is = new DigestInputStream(contentInputStream, algorithm.getMessageDigest());
78-
digestInputStreamMap.put(algorithm, is);
79-
80-
contentInputStream = is;
81-
}
80+
algorithms = Arrays.asList(ChecksumAlgorithm.values());
8281
} else if (checksumAlgorithm != null) {
83-
singleDigestInputStream = new DigestInputStream(contentInputStream,
84-
checksumAlgorithm.getMessageDigest());
85-
86-
contentInputStream = singleDigestInputStream;
82+
algorithms = Collections.singletonList(checksumAlgorithm);
83+
} else {
84+
algorithms = Collections.emptyList();
8785
}
8886

87+
for (ChecksumAlgorithm algorithm : algorithms) {
88+
DigestInputStream is = new DigestInputStream(contentInputStream, algorithm.getMessageDigest());
89+
digestInputStreamMap.put(algorithm, is);
90+
91+
contentInputStream = is;
92+
}
8993
}
9094

9195
return contentInputStream;
9296
}
9397

94-
private boolean hasChunkedTransferEncoding() {
95-
return StringUtils.equalsIgnoreCase("chunked", getHeader(HttpHeader.TRANSFER_ENCODING));
96-
}
97-
9898
public long getBytesRead() {
9999
return countingInputStream == null ? 0 : countingInputStream.getByteCount();
100100
}
101101

102102
public boolean hasCalculatedChecksum() {
103-
return singleDigestInputStream != null || !digestInputStreamMap.isEmpty();
103+
return !digestInputStreamMap.isEmpty();
104104
}
105105

106106
public String getCalculatedChecksum(ChecksumAlgorithm algorithm) {
@@ -109,14 +109,12 @@ public String getCalculatedChecksum(ChecksumAlgorithm algorithm) {
109109
DatatypeConverter.printBase64Binary(messageDigest.digest());
110110
}
111111

112-
private MessageDigest getMessageDigest(ChecksumAlgorithm algorithm) {
113-
if (digestInputStreamMap.containsKey(algorithm)) {
114-
return digestInputStreamMap.get(algorithm).getMessageDigest();
115-
} else if (singleDigestInputStream != null) {
116-
return singleDigestInputStream.getMessageDigest();
117-
} else {
118-
return null;
119-
}
112+
/**
113+
* Get the set of checksum algorithms that are actively calculated within this request
114+
* @return The set of active checksum algorithms
115+
*/
116+
public Set<ChecksumAlgorithm> getEnabledChecksums() {
117+
return digestInputStreamMap.keySet();
120118
}
121119

122120
@Override
@@ -140,4 +138,16 @@ public boolean isProcessedBy(TusExtension processor) {
140138
public void addProcessor(TusExtension processor) {
141139
processedBySet.add(processor.getName());
142140
}
141+
142+
private boolean hasChunkedTransferEncoding() {
143+
return StringUtils.equalsIgnoreCase("chunked", getHeader(HttpHeader.TRANSFER_ENCODING));
144+
}
145+
146+
private MessageDigest getMessageDigest(ChecksumAlgorithm algorithm) {
147+
if (digestInputStreamMap.containsKey(algorithm)) {
148+
return digestInputStreamMap.get(algorithm).getMessageDigest();
149+
} else {
150+
return null;
151+
}
152+
}
143153
}

0 commit comments

Comments
 (0)