Skip to content

Commit 2b5cc19

Browse files
committed
Fix the potential data loss issue.
1 parent 0052a9d commit 2b5cc19

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

src/main/java/custom/application/v1/smalltalk.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,18 @@ public String upload() throws ApplicationException {
767767
final BufferedOutputStream bout = new BufferedOutputStream(out);
768768
final BufferedInputStream bs = new BufferedInputStream(new ByteArrayInputStream(file.get()));
769769
) {
770-
byte[] keyBytes = keyBytes(meetingCode.toString()); // Generate key bytes
771-
int bytesRead;
772-
byte[] buffer = new byte[1024];
773-
while ((bytesRead = bs.read(buffer)) != -1) {
774-
for (int i = 0; i < bytesRead; i++) {
775-
buffer[i] = (byte) (buffer[i] ^ keyBytes[i % keyBytes.length]); // XOR operation
770+
final byte[] bytes = new byte[1024];
771+
byte[] keys = meetingCode.toString().getBytes(StandardCharsets.UTF_8);
772+
int read;
773+
while ((read = bs.read(bytes)) != -1) {
774+
int min = Math.min(read, keys.length);
775+
for (int i = 0; i < min; i++) {
776+
bytes[i] = (byte) (bytes[i] ^ keys[i]);
776777
}
777-
bout.write(buffer, 0, bytesRead);
778+
bout.write(bytes, 0, read);
778779
}
780+
bout.close();
781+
bs.close();
779782

780783
builders.add(builder);
781784
System.out.printf("File %s being uploaded to %s%n", file.getFilename(), path);
@@ -815,10 +818,15 @@ public byte[] download(String fileName, boolean encoded) throws ApplicationExcep
815818

816819
arr = Files.readAllBytes(path);
817820
if (encoded) {
818-
byte[] keyBytes = keyBytes(meetingCode.toString()); // Generate key bytes
819-
for (int i = 0; i < arr.length; i++) {
820-
arr[i] = (byte) (arr[i] ^ keyBytes[i % keyBytes.length]);
821-
}
821+
byte[] keys = meetingCode.toString().getBytes(StandardCharsets.UTF_8);
822+
int blocks = (arr.length - arr.length % 1024) / 1024;
823+
int i = 0;
824+
do {
825+
int min = Math.min(keys.length, arr.length);
826+
for (int j = 0; j < min; j++) {
827+
arr[i * 1024 + j] = (byte) (arr[i * 1024 + j] ^ keys[j]);
828+
}
829+
} while (i++ < blocks);
822830
}
823831
} catch (IOException e) {
824832
throw new ApplicationException("Error reading the file: " + e.getMessage(), e);
@@ -827,10 +835,6 @@ public byte[] download(String fileName, boolean encoded) throws ApplicationExcep
827835
return arr;
828836
}
829837

830-
private byte[] keyBytes(String meetingCode) {
831-
return meetingCode.getBytes(StandardCharsets.UTF_8);
832-
}
833-
834838
@Action("files")
835839
public byte[] download(String fileName) throws ApplicationException {
836840
return this.download(fileName, true);

0 commit comments

Comments
 (0)