1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-07-16 16:54:51 +00:00

Use duplicate() instead of rewind()

It's just more confusing having to keep track of where the ByteBuffer is
at. In this case, I think we were forgetting to rewind after computing
the digest.

Hopefully we'll be able to drop some of these in 1.17 as Java 16 has
a few more ByteBuffer methods

Fixes #992
This commit is contained in:
Jonathan Coates 2021-12-18 11:23:12 +00:00
parent b048b6666d
commit 0477b2742c
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
5 changed files with 9 additions and 14 deletions

View File

@ -173,7 +173,6 @@ public void onFilesDrop( @Nonnull List<Path> files )
return; return;
} }
buffer.rewind();
toUpload.add( new FileUpload( name, buffer, digest ) ); toUpload.add( new FileUpload( name, buffer, digest ) );
} }
catch( IOException e ) catch( IOException e )

View File

@ -56,7 +56,7 @@ public interface IContainerComputer
void continueUpload( @Nonnull UUID uploadId, @Nonnull List<FileSlice> slices ); void continueUpload( @Nonnull UUID uploadId, @Nonnull List<FileSlice> slices );
/** /**
* Finish off an upload. This either writes the uploaded files or * Finish off an upload. This either writes the uploaded files or informs the user that files will be overwritten.
* *
* @param uploader The player uploading files. * @param uploader The player uploading files.
* @param uploadId The unique ID of this upload. * @param uploadId The unique ID of this upload.

View File

@ -53,10 +53,9 @@ public void apply( List<FileUpload> files )
return; return;
} }
bytes.rewind(); ByteBuffer other = file.duplicate();
file.position( offset ); other.position( offset ); // TODO: In 1.17 we can use a separate put(idx, _) method.
file.put( bytes ); other.put( bytes );
file.rewind();
if( bytes.remaining() != 0 ) throw new IllegalStateException( "Should have read the whole buffer" ); if( bytes.remaining() != 0 ) throw new IllegalStateException( "Should have read the whole buffer" );
} }

View File

@ -56,7 +56,7 @@ public byte[] getChecksum()
public boolean checksumMatches() public boolean checksumMatches()
{ {
// This is meant to be a checksum. Doesn't need to be cryptographically secure, hence non constant time. // This is meant to be a checksum. Doesn't need to be cryptographically secure, hence non-constant time.
byte[] digest = getDigest( bytes ); byte[] digest = getDigest( bytes );
return digest != null && Arrays.equals( checksum, digest ); return digest != null && Arrays.equals( checksum, digest );
} }
@ -66,9 +66,8 @@ public static byte[] getDigest( ByteBuffer bytes )
{ {
try try
{ {
bytes.rewind();
MessageDigest digest = MessageDigest.getInstance( "SHA-256" ); MessageDigest digest = MessageDigest.getInstance( "SHA-256" );
digest.update( bytes ); digest.update( bytes.duplicate() );
return digest.digest(); return digest.digest();
} }
catch( NoSuchAlgorithmException e ) catch( NoSuchAlgorithmException e )

View File

@ -5,7 +5,6 @@
*/ */
package dan200.computercraft.shared.network.server; package dan200.computercraft.shared.network.server;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.core.IContainerComputer; import dan200.computercraft.shared.computer.core.IContainerComputer;
import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.upload.FileSlice; import dan200.computercraft.shared.computer.upload.FileSlice;
@ -122,9 +121,9 @@ public void toBytes( @Nonnull PacketBuffer buf )
buf.writeByte( slice.getFileId() ); buf.writeByte( slice.getFileId() );
buf.writeVarInt( slice.getOffset() ); buf.writeVarInt( slice.getOffset() );
slice.getBytes().rewind(); ByteBuffer bytes = slice.getBytes().duplicate();
buf.writeShort( slice.getBytes().remaining() ); buf.writeShort( bytes.remaining() );
buf.writeBytes( slice.getBytes() ); buf.writeBytes( bytes );
} }
} }
@ -158,7 +157,6 @@ public static void send( int instanceId, List<FileUpload> files )
int canWrite = Math.min( remaining, capacity - currentOffset ); int canWrite = Math.min( remaining, capacity - currentOffset );
ComputerCraft.log.info( "Adding slice from {} to {}", currentOffset, currentOffset + canWrite - 1 );
contents.position( currentOffset ).limit( currentOffset + canWrite ); contents.position( currentOffset ).limit( currentOffset + canWrite );
slices.add( new FileSlice( fileId, currentOffset, contents.slice() ) ); slices.add( new FileSlice( fileId, currentOffset, contents.slice() ) );
currentOffset += canWrite; currentOffset += canWrite;