Skip to content

Commit

Permalink
Apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzaharovits committed Nov 28, 2020
1 parent 0848545 commit cf8c7fd
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ public long skip(long n) throws IOException {
return source.skip(n);
}
long remaining = n;
int size = (int)Math.min(2048, remaining);
int size = (int) Math.min(2048, remaining);
byte[] skipBuffer = new byte[size];
while (remaining > 0) {
// skipping translates to a read so that the skipped bytes are stored in the buffer,
// so they can possibly be replayed after a reset
int bytesRead = read(skipBuffer, 0, (int)Math.min(size, remaining));
int bytesRead = read(skipBuffer, 0, (int) Math.min(size, remaining));
if (bytesRead < 0) {
break;
}
Expand Down Expand Up @@ -261,8 +261,9 @@ public void mark(int readlimit) {
// readlimit is otherwise ignored but this defensively fails if the caller is expecting to be able to mark/reset more than this
// instance can accommodate in the fixed ring buffer
if (readlimit > ringBuffer.getBufferSize()) {
throw new IllegalArgumentException("Readlimit value [" + readlimit + "] exceeds the maximum value of [" +
ringBuffer.getBufferSize() + "]");
throw new IllegalArgumentException(
"Readlimit value [" + readlimit + "] exceeds the maximum value of [" + ringBuffer.getBufferSize() + "]"
);
} else if (readlimit < 0) {
throw new IllegalArgumentException("Readlimit value [" + readlimit + "] cannot be negative");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public int read() throws IOException {
return -1;
}


/**
* Reads up to {@code len} bytes of data into an array of bytes from this
* chaining input stream. If {@code len} is zero, then no bytes are read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ InputStream nextComponent(InputStream currentComponentIn) throws IOException {
if (false == hasNext) {
return null;
}
PrefixInputStream packetInputStream = new PrefixInputStream(source,
packetLength + GCM_IV_LENGTH_IN_BYTES + GCM_TAG_LENGTH_IN_BYTES,
false);
PrefixInputStream packetInputStream = new PrefixInputStream(
source,
packetLength + GCM_IV_LENGTH_IN_BYTES + GCM_TAG_LENGTH_IN_BYTES,
false
);
int currentPacketLength = decrypt(packetInputStream);
// only the last packet is shorter, so this must be the last packet
if (currentPacketLength != packetLength) {
Expand All @@ -115,8 +117,7 @@ public boolean markSupported() {
}

@Override
public void mark(int readlimit) {
}
public void mark(int readlimit) {}

@Override
public void reset() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

public final class EncryptedRepositoryPlugin extends Plugin implements RepositoryPlugin, ReloadablePlugin {

public EncryptedRepositoryPlugin(final Settings settings) {
}
public EncryptedRepositoryPlugin(final Settings settings) {}

@Override
public List<Setting<?>> getSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public final class EncryptionPacketsInputStream extends ChainingInputStream {
*/
public static long getEncryptionLength(long plaintextLength, int packetLength) {
return plaintextLength + (plaintextLength / packetLength + 1) * (EncryptedRepository.GCM_TAG_LENGTH_IN_BYTES
+ EncryptedRepository.GCM_IV_LENGTH_IN_BYTES);
+ EncryptedRepository.GCM_IV_LENGTH_IN_BYTES);
}

public EncryptionPacketsInputStream(InputStream source, SecretKey secretKey, int nonce, int packetLength) {
Expand All @@ -100,8 +100,8 @@ public EncryptionPacketsInputStream(InputStream source, SecretKey secretKey, int
this.packetIv = ByteBuffer.allocate(EncryptedRepository.GCM_IV_LENGTH_IN_BYTES).order(ByteOrder.LITTLE_ENDIAN);
// nonce takes the first 4 bytes of the IV
this.packetIv.putInt(0, nonce);
this.encryptedPacketLength =
packetLength + EncryptedRepository.GCM_IV_LENGTH_IN_BYTES + EncryptedRepository.GCM_TAG_LENGTH_IN_BYTES;
this.encryptedPacketLength = packetLength + EncryptedRepository.GCM_IV_LENGTH_IN_BYTES
+ EncryptedRepository.GCM_TAG_LENGTH_IN_BYTES;
this.counter = EncryptedRepository.PACKET_START_COUNTER;
this.markCounter = null;
this.markSourceOnNextPacket = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public void testResetWithoutMarkFails() throws Exception {
BufferOnMarkInputStream test = new BufferOnMarkInputStream(mockSourceTuple.v2(), 1 + Randomness.get().nextInt(1024));
// maybe read some bytes
test.readNBytes(randomFrom(0, randomInt(31)));
IOException e = expectThrows(IOException.class, () -> {
test.reset();
});
IOException e = expectThrows(IOException.class, () -> { test.reset(); });
assertThat(e.getMessage(), Matchers.is("Mark not called or has been invalidated"));
}

Expand All @@ -54,18 +52,14 @@ public void testMarkAndBufferReadLimitsCheck() throws Exception {
// maybe read some bytes
test.readNBytes(randomFrom(0, randomInt(32)));
int wrongLargeReadLimit = bufferSize + randomIntBetween(1, 8);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
test.mark(wrongLargeReadLimit);
});
assertThat(e.getMessage(), Matchers.is("Readlimit value [" + wrongLargeReadLimit + "] exceeds the maximum value of ["
+ bufferSize + "]"));
e = expectThrows(IllegalArgumentException.class, () -> {
test.mark(-1 - randomInt(1));
});
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { test.mark(wrongLargeReadLimit); });
assertThat(
e.getMessage(),
Matchers.is("Readlimit value [" + wrongLargeReadLimit + "] exceeds the maximum value of [" + bufferSize + "]")
);
e = expectThrows(IllegalArgumentException.class, () -> { test.mark(-1 - randomInt(1)); });
assertThat(e.getMessage(), Matchers.containsString("cannot be negative"));
e = expectThrows(IllegalArgumentException.class, () -> {
new BufferOnMarkInputStream(mock(InputStream.class), 0 - randomInt(1));
});
e = expectThrows(IllegalArgumentException.class, () -> { new BufferOnMarkInputStream(mock(InputStream.class), 0 - randomInt(1)); });
assertThat(e.getMessage(), Matchers.is("The buffersize constructor argument must be a strictly positive value"));
}

Expand All @@ -78,26 +72,18 @@ public void testCloseRejectsSuccessiveCalls() throws Exception {
test.readNBytes(randomFrom(0, Randomness.get().nextInt(32)));
test.close();
int bytesReadBefore = bytesRead.get();
IOException e = expectThrows(IOException.class, () -> {
test.read();
});
IOException e = expectThrows(IOException.class, () -> { test.read(); });
assertThat(e.getMessage(), Matchers.is("Stream has been closed"));
e = expectThrows(IOException.class, () -> {
byte[] b = new byte[1 + Randomness.get().nextInt(32)];
test.read(b, 0, 1 + Randomness.get().nextInt(b.length));
});
assertThat(e.getMessage(), Matchers.is("Stream has been closed"));
e = expectThrows(IOException.class, () -> {
test.skip(1 + Randomness.get().nextInt(32));
});
e = expectThrows(IOException.class, () -> { test.skip(1 + Randomness.get().nextInt(32)); });
assertThat(e.getMessage(), Matchers.is("Stream has been closed"));
e = expectThrows(IOException.class, () -> {
test.available();
});
e = expectThrows(IOException.class, () -> { test.available(); });
assertThat(e.getMessage(), Matchers.is("Stream has been closed"));
e = expectThrows(IOException.class, () -> {
test.reset();
});
e = expectThrows(IOException.class, () -> { test.reset(); });
assertThat(e.getMessage(), Matchers.is("Stream has been closed"));
int bytesReadAfter = bytesRead.get();
assertThat(bytesReadAfter - bytesReadBefore, Matchers.is(0));
Expand Down Expand Up @@ -216,9 +202,7 @@ public void testMarkInvalidation() throws Exception {
assertThat(test.storeToBuffer, Matchers.is(false));
assertThat(test.replayFromBuffer, Matchers.is(false));
// assert reset does not work any more
IOException e = expectThrows(IOException.class, () -> {
test.reset();
});
IOException e = expectThrows(IOException.class, () -> { test.reset(); });
assertThat(e.getMessage(), Matchers.is("Mark not called or has been invalidated"));
}

Expand Down Expand Up @@ -356,9 +340,7 @@ public void testInvalidateMarkAfterReset() throws Exception {
assertThat(test.storeToBuffer, Matchers.is(false));
assertThat(test.replayFromBuffer, Matchers.is(false));
// assert reset does not work anymore
IOException e = expectThrows(IOException.class, () -> {
test.reset();
});
IOException e = expectThrows(IOException.class, () -> { test.reset(); });
assertThat(e.getMessage(), Matchers.is("Mark not called or has been invalidated"));
}

Expand Down Expand Up @@ -572,8 +554,9 @@ public void testNoMockSimpleMarkResetEverywhere() throws Exception {
for (int length = 1; length <= 10; length++) {
for (int offset = 0; offset < length; offset++) {
for (int mark = 1; mark <= length - offset; mark++) {
try (BufferOnMarkInputStream in = new BufferOnMarkInputStream(new
NoMarkByteArrayInputStream(testArray, 0, length), mark)) {
try (
BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length), mark)
) {
// skip first offset bytes
in.readNBytes(offset);
in.mark(mark);
Expand All @@ -595,8 +578,9 @@ public void testNoMockSimpleMarkResetEverywhere() throws Exception {
public void testNoMockMarkResetEverywhere() throws Exception {
for (int length = 1; length <= 8; length++) {
for (int offset = 0; offset < length; offset++) {
try (BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length),
length)) {
try (
BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length), length)
) {
// skip first offset bytes
in.readNBytes(offset);
in.mark(length);
Expand All @@ -607,8 +591,9 @@ public void testNoMockMarkResetEverywhere() throws Exception {
in.reset();
}
}
try (BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length),
length)) {
try (
BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length), length)
) {
// skip first offset bytes
in.readNBytes(offset);
in.mark(length);
Expand All @@ -628,8 +613,12 @@ public void testNoMockDoubleMarkEverywhere() throws Exception {
for (int offset = 0; offset < length; offset++) {
for (int readLen = 1; readLen <= length - offset; readLen++) {
for (int markLen = 1; markLen <= length - offset; markLen++) {
try (BufferOnMarkInputStream in = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray, 0, length),
length)) {
try (
BufferOnMarkInputStream in = new BufferOnMarkInputStream(
new NoMarkByteArrayInputStream(testArray, 0, length),
length
)
) {
in.readNBytes(offset);
assertThat(in.ringBuffer.getAvailableToWriteByteCount(), Matchers.is(length));
assertThat(in.ringBuffer.getAvailableToReadByteCount(), Matchers.is(0));
Expand Down Expand Up @@ -719,7 +708,7 @@ private void testMarkResetMarkStep(BufferOnMarkInputStream stream, int offset, i
stream.mark(stepLen);
for (int readLen = 1; readLen <= Math.min(stepLen, length - offset); readLen++) {
for (int markLen = 1; markLen <= Math.min(stepLen, length - offset); markLen++) {
BufferOnMarkInputStream cloneStream = cloneBufferOnMarkStream(stream) ;
BufferOnMarkInputStream cloneStream = cloneBufferOnMarkStream(stream);
// read ahead
byte[] test = cloneStream.readNBytes(readLen);
assertArray(offset, test);
Expand All @@ -738,8 +727,10 @@ private void testMarkResetMarkStep(BufferOnMarkInputStream stream, int offset, i
private BufferOnMarkInputStream cloneBufferOnMarkStream(BufferOnMarkInputStream orig) {
int origOffset = ((NoMarkByteArrayInputStream) orig.source).getPos();
int origLen = ((NoMarkByteArrayInputStream) orig.source).getCount();
BufferOnMarkInputStream cloneStream = new BufferOnMarkInputStream(new NoMarkByteArrayInputStream(testArray,
origOffset, origLen - origOffset), orig.ringBuffer.getBufferSize());
BufferOnMarkInputStream cloneStream = new BufferOnMarkInputStream(
new NoMarkByteArrayInputStream(testArray, origOffset, origLen - origOffset),
orig.ringBuffer.getBufferSize()
);
if (orig.ringBuffer.buffer != null) {
cloneStream.ringBuffer.buffer = Arrays.copyOf(orig.ringBuffer.buffer, orig.ringBuffer.buffer.length);
} else {
Expand Down Expand Up @@ -777,17 +768,18 @@ private void assertArray(int offset, byte[] test) {
private Tuple<AtomicInteger, InputStream> getMockInfiniteInputStream() throws IOException {
InputStream mockSource = mock(InputStream.class);
AtomicInteger bytesRead = new AtomicInteger(0);
when(mockSource.read(org.mockito.Matchers.<byte[]>any(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyInt())).
thenAnswer(invocationOnMock -> {
final int len = (int) invocationOnMock.getArguments()[2];
if (len == 0) {
return 0;
} else {
int bytesCount = 1 + Randomness.get().nextInt(len);
bytesRead.addAndGet(bytesCount);
return bytesCount;
}
});
when(mockSource.read(org.mockito.Matchers.<byte[]>any(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyInt())).thenAnswer(
invocationOnMock -> {
final int len = (int) invocationOnMock.getArguments()[2];
if (len == 0) {
return 0;
} else {
int bytesCount = 1 + Randomness.get().nextInt(len);
bytesRead.addAndGet(bytesCount);
return bytesCount;
}
}
);
when(mockSource.read()).thenAnswer(invocationOnMock -> {
bytesRead.incrementAndGet();
return Randomness.get().nextInt(256);
Expand Down Expand Up @@ -845,8 +837,7 @@ int getCount() {
}

@Override
public void mark(int readlimit) {
}
public void mark(int readlimit) {}

@Override
public boolean markSupported() {
Expand Down
Loading

0 comments on commit cf8c7fd

Please sign in to comment.