-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
178 additions
and
172 deletions.
There are no files selected for viewing
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,48 @@ | ||
package au.com.southsky.jfreesane; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
final class ByteStreams { | ||
private static int COPY_BUF_SIZE = 8192; | ||
|
||
private ByteStreams() {} | ||
|
||
/** | ||
* A stand-in for {@code InputStream.readAllBytes} available in Java 9+. We currently support Java | ||
* 8, so this is what we have to do. | ||
*/ | ||
static int readAllBytes(InputStream in, byte[] dst) throws IOException { | ||
int pos = 0; | ||
while (pos < dst.length) { | ||
int r = in.read(dst, pos, dst.length - pos); | ||
if (r == -1) { | ||
return pos; | ||
} | ||
|
||
pos += r; | ||
} | ||
|
||
return pos; | ||
} | ||
|
||
/** | ||
* Copies up to maxBytes from src to dst, returning how many were copied. | ||
*/ | ||
static int copy(InputStream src, OutputStream dst, int maxBytes) throws IOException { | ||
byte[] buf = new byte[Math.min(maxBytes, COPY_BUF_SIZE)]; | ||
int total = 0; | ||
|
||
while (total < maxBytes) { | ||
int r = src.read(buf, 0, Math.min(maxBytes - total, COPY_BUF_SIZE)); | ||
if (r == -1) { | ||
break; | ||
} | ||
dst.write(buf, 0, r); | ||
total += r; | ||
} | ||
|
||
return total; | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/main/java/au/com/southsky/jfreesane/Preconditions.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,43 @@ | ||
package au.com.southsky.jfreesane; | ||
|
||
final class Preconditions { | ||
private Preconditions() {} | ||
|
||
static void checkState(boolean state, String message, Object... args) { | ||
if (!state) { | ||
throw new IllegalStateException(String.format(message, args)); | ||
} | ||
} | ||
|
||
static void checkState(boolean state, String message) { | ||
if (!state) { | ||
throw new IllegalStateException(message); | ||
} | ||
} | ||
|
||
static void checkState(boolean state) { | ||
if (!state) { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
static void checkArgument(boolean arg) { | ||
if (!arg) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
static void checkArgument(boolean arg, String message, Object... args) { | ||
if (!arg) { | ||
throw new IllegalArgumentException(String.format(message, args)); | ||
} | ||
} | ||
|
||
static <T> T checkNotNull(T obj) { | ||
if (obj == null) { | ||
throw new NullPointerException(); | ||
} | ||
|
||
return obj; | ||
} | ||
} |
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
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
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
Oops, something went wrong.