@@ -767,25 +767,22 @@ public String upload() throws ApplicationException {
767767 final BufferedOutputStream bout = new BufferedOutputStream (out );
768768 final BufferedInputStream bs = new BufferedInputStream (new ByteArrayInputStream (file .get ()));
769769 ) {
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- for (int i = 0 ; i < keys . length ; i ++) {
775- bytes [i ] = (byte ) (bytes [i ] ^ keys [ i ]);
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
776776 }
777- bout .write (bytes , 0 , read );
777+ bout .write (buffer , 0 , bytesRead );
778778 }
779779
780- bout .close ();
781- bs .close ();
782-
783780 builders .add (builder );
784781 System .out .printf ("File %s being uploaded to %s%n" , file .getFilename (), path );
785782 } catch (FileNotFoundException e ) {
786- throw new ApplicationException (e .getMessage (), e );
783+ throw new ApplicationException ("File not found: " + e .getMessage (), e );
787784 } catch (IOException e ) {
788- throw new ApplicationException (e .getMessage (), e );
785+ throw new ApplicationException ("Error uploading file: " + e .getMessage (), e );
789786 }
790787 }
791788
@@ -818,20 +815,22 @@ public byte[] download(String fileName, boolean encoded) throws ApplicationExcep
818815
819816 arr = Files .readAllBytes (path );
820817 if (encoded ) {
821- byte [] keys = meetingCode .toString ().getBytes (StandardCharsets .UTF_8 );
822- for (int i = 0 ; i < arr .length ; i = i + 1024 ) {
823- for (int j = 0 ; j < keys .length ; j ++) {
824- arr [i + j ] = (byte ) (arr [i + j ] ^ keys [j ]);
825- }
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 ]);
826821 }
827822 }
828823 } catch (IOException e ) {
829- e . printStackTrace ( );
824+ throw new ApplicationException ( "Error reading the file: " + e . getMessage (), e );
830825 }
831826
832827 return arr ;
833828 }
834829
830+ private byte [] keyBytes (String meetingCode ) {
831+ return meetingCode .getBytes (StandardCharsets .UTF_8 );
832+ }
833+
835834 @ Action ("files" )
836835 public byte [] download (String fileName ) throws ApplicationException {
837836 return this .download (fileName , true );
0 commit comments