-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1416: limit output fetched by npm process
usually only the last few lines are of interest anyway, so be kind to memory usage.
- Loading branch information
Showing
4 changed files
with
322 additions
and
4 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
lib/src/main/java/com/diffplug/spotless/LimitedOverwritingByteArrayOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public class LimitedOverwritingByteArrayOutputStream extends ByteArrayOutputStream { | ||
|
||
private final int limit; | ||
|
||
private int zeroIndexPointer = 0; | ||
|
||
private boolean isOverLimit = false; | ||
|
||
public LimitedOverwritingByteArrayOutputStream(int limit) { | ||
this(limit, 32); | ||
} | ||
|
||
public LimitedOverwritingByteArrayOutputStream(int limit, int initialCapacity) { | ||
super(initialCapacity); | ||
if (limit < initialCapacity) { | ||
throw new IllegalArgumentException("Limit must be greater than initial capacity"); | ||
} | ||
if (limit < 0) { | ||
throw new IllegalArgumentException("Limit must be greater than 0"); | ||
} | ||
if (limit % 2 != 0) { | ||
throw new IllegalArgumentException("Limit must be even"); // to fit 16 bit unicode chars | ||
} | ||
this.limit = limit; | ||
} | ||
|
||
// ---- writing | ||
@Override | ||
public synchronized void write(int b) { | ||
if (count < limit) { | ||
super.write(b); | ||
return; | ||
} | ||
isOverLimit = true; | ||
buf[zeroIndexPointer] = (byte) b; | ||
zeroIndexPointer = (zeroIndexPointer + 1) % limit; | ||
} | ||
|
||
@Override | ||
public synchronized void write(byte[] b, int off, int len) { | ||
int remaining = limit - count; | ||
if (remaining >= len) { | ||
super.write(b, off, len); | ||
return; | ||
} | ||
if (remaining > 0) { | ||
// write what we can "normally" | ||
super.write(b, off, remaining); | ||
// rest delegated | ||
write(b, off + remaining, len - remaining); | ||
return; | ||
} | ||
// we are over the limit | ||
isOverLimit = true; | ||
// write till limit is reached | ||
int writeTillLimit = Math.min(len, limit - zeroIndexPointer); | ||
System.arraycopy(b, off, buf, zeroIndexPointer, writeTillLimit); | ||
zeroIndexPointer = (zeroIndexPointer + writeTillLimit) % limit; | ||
if (writeTillLimit < len) { | ||
// write rest | ||
write(b, off + writeTillLimit, len - writeTillLimit); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void reset() { | ||
super.reset(); | ||
zeroIndexPointer = 0; | ||
isOverLimit = false; | ||
} | ||
|
||
// ---- output | ||
@Override | ||
public synchronized void writeTo(OutputStream out) throws IOException { | ||
if (!isOverLimit) { | ||
super.writeTo(out); | ||
return; | ||
} | ||
out.write(buf, zeroIndexPointer, limit - zeroIndexPointer); | ||
out.write(buf, 0, zeroIndexPointer); | ||
} | ||
|
||
@Override | ||
public synchronized byte[] toByteArray() { | ||
if (!isOverLimit) { | ||
return super.toByteArray(); | ||
} | ||
byte[] result = new byte[limit]; | ||
System.arraycopy(buf, zeroIndexPointer, result, 0, limit - zeroIndexPointer); | ||
System.arraycopy(buf, 0, result, limit - zeroIndexPointer, zeroIndexPointer); | ||
return result; | ||
} | ||
|
||
@Override | ||
public synchronized String toString() { | ||
if (!isOverLimit) { | ||
return super.toString(); | ||
} | ||
return new String(buf, zeroIndexPointer, limit - zeroIndexPointer) + new String(buf, 0, zeroIndexPointer); | ||
} | ||
|
||
@Override | ||
public synchronized String toString(String charsetName) throws UnsupportedEncodingException { | ||
if (!isOverLimit) { | ||
return super.toString(charsetName); | ||
} | ||
return new String(buf, zeroIndexPointer, limit - zeroIndexPointer, charsetName) + new String(buf, 0, zeroIndexPointer, charsetName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
lib/src/test/java/com/diffplug/spotless/LimitedOverwritingByteArrayOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.stream.Stream; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class LimitedOverwritingByteArrayOutputStreamTest { | ||
|
||
private final byte[] bytes = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toStringBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(13, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toString()).isEqualTo("0123456789"); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toStringBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(4, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toString()).hasSize(4); | ||
Assertions.assertThat(stream.toString()).isEqualTo("6789"); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toStringBehavesNormallyAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(bytes.length, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toString()).isEqualTo("0123456789"); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toByteArrayBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(13, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toByteArray()).isEqualTo(bytes); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toByteArrayBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(4, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toByteArray()).hasSize(4); | ||
Assertions.assertThat(stream.toByteArray()).isEqualTo(new byte[]{'6', '7', '8', '9'}); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void toByteArrayBehavesOverwritingAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(bytes.length, 1); | ||
writeStrategy.write(stream, bytes); | ||
Assertions.assertThat(stream.toByteArray()).isEqualTo(bytes); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void writeToBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(13, 1); | ||
writeStrategy.write(stream, bytes); | ||
ByteArrayOutputStream target = new ByteArrayOutputStream(); | ||
stream.writeTo(target); | ||
Assertions.assertThat(target.toByteArray()).isEqualTo(bytes); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void writeToBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(4, 1); | ||
writeStrategy.write(stream, bytes); | ||
ByteArrayOutputStream target = new ByteArrayOutputStream(); | ||
stream.writeTo(target); | ||
Assertions.assertThat(target.toByteArray()).hasSize(4); | ||
Assertions.assertThat(target.toByteArray()).isEqualTo(new byte[]{'6', '7', '8', '9'}); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} writeStrategy: {0}") | ||
@MethodSource("writeStrategies") | ||
void writeToBehavesNormallyAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(bytes.length, 1); | ||
writeStrategy.write(stream, bytes); | ||
ByteArrayOutputStream target = new ByteArrayOutputStream(); | ||
stream.writeTo(target); | ||
Assertions.assertThat(target.toByteArray()).isEqualTo(bytes); | ||
} | ||
|
||
@Test | ||
void writeToBehavesCorrectlyWhenOverLimitMultipleCalls() { | ||
// this test explicitly captures a border case where the buffer is not empty but can exactly fit what we are writing | ||
LimitedOverwritingByteArrayOutputStream stream = new LimitedOverwritingByteArrayOutputStream(2, 1); | ||
stream.write('0'); | ||
stream.write(new byte[]{'1', '2'}, 0, 2); | ||
Assertions.assertThat(stream.toString()).hasSize(2); | ||
Assertions.assertThat(stream.toString()).isEqualTo("12"); | ||
} | ||
|
||
private static Stream<Arguments> writeStrategies() { | ||
return Stream.of( | ||
Arguments.of("writeAllAtOnce", allAtOnce()), | ||
Arguments.of("writeOneByteAtATime", oneByteAtATime()), | ||
Arguments.of("writeTwoBytesAtATime", twoBytesAtATime()), | ||
Arguments.of("writeOneAndThenTwoBytesAtATime", oneAndThenTwoBytesAtATime()), | ||
Arguments.of("firstFourBytesAndThenTheRest", firstFourBytesAndThenTheRest())); | ||
} | ||
|
||
private static ByteWriteStrategy allAtOnce() { | ||
return (stream, bytes) -> stream.write(bytes, 0, bytes.length); | ||
} | ||
|
||
private static ByteWriteStrategy oneByteAtATime() { | ||
return (stream, bytes) -> { | ||
for (byte b : bytes) { | ||
stream.write(b); | ||
} | ||
}; | ||
} | ||
|
||
private static ByteWriteStrategy twoBytesAtATime() { | ||
return (stream, bytes) -> { | ||
for (int i = 0; i < bytes.length; i += 2) { | ||
stream.write(bytes, i, 2); | ||
} | ||
}; | ||
} | ||
|
||
private static ByteWriteStrategy oneAndThenTwoBytesAtATime() { | ||
return (stream, bytes) -> { | ||
int written = 0; | ||
for (int i = 0; i + 3 < bytes.length; i += 3) { | ||
stream.write(bytes, i, 1); | ||
stream.write(bytes, i + 1, 2); | ||
written += 3; | ||
} | ||
if (written < bytes.length) { | ||
stream.write(bytes, written, bytes.length - written); | ||
} | ||
|
||
}; | ||
} | ||
|
||
private static ByteWriteStrategy firstFourBytesAndThenTheRest() { | ||
return (stream, bytes) -> { | ||
stream.write(bytes, 0, 4); | ||
stream.write(bytes, 4, bytes.length - 4); | ||
}; | ||
} | ||
|
||
@FunctionalInterface | ||
private interface ByteWriteStrategy { | ||
void write(LimitedOverwritingByteArrayOutputStream stream, byte[] bytes); | ||
} | ||
|
||
} |