-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
6 changed files
with
210 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.xxl.tool.core; | ||
|
||
public class AssertTool { | ||
|
||
|
||
/** | ||
* Assert a boolean expression, throwing an {@code IllegalArgumentException} | ||
* if the expression evaluates to {@code false}. | ||
* <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre> | ||
* @param expression a boolean expression | ||
* @param message the exception message to use if the assertion fails | ||
* @throws IllegalArgumentException if {@code expression} is {@code false} | ||
*/ | ||
public static void isTrue(boolean expression, String message) { | ||
if (!expression) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
/** | ||
* Assert that an object is {@code null}. | ||
* | ||
* <pre> | ||
* Assert.isNull(value, "The value must be null"); | ||
* </pre> | ||
* | ||
* @param object the object to check | ||
* @param message the exception message to use if the assertion fails | ||
* @throws IllegalArgumentException if the object is not {@code null} | ||
*/ | ||
public static void isNull(Object object, String message) { | ||
if (object != null) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Assert that an object is not {@code null}. | ||
* | ||
* <pre> | ||
* Assert.notNull(clazz, "The class must not be null"); | ||
* </pre> | ||
* | ||
* @param object the object to check | ||
* @param message the exception message to use if the assertion fails | ||
* @throws IllegalArgumentException if the object is {@code null} | ||
*/ | ||
public static void notNull(Object object, String message) { | ||
if (object == null) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
|
||
} |
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,96 @@ | ||
package com.xxl.tool.io; | ||
|
||
import com.xxl.tool.core.AssertTool; | ||
|
||
import java.io.*; | ||
|
||
public class StreamTool { | ||
|
||
/** | ||
* The default buffer size used when copying bytes. | ||
*/ | ||
public static final int BUFFER_SIZE = 1024 * 4; | ||
|
||
|
||
// ---------------------- copy ---------------------- | ||
|
||
/** | ||
* Copy from InputStream to OutputStream, Closes both streams when done. | ||
* | ||
* @param in InputStream | ||
* @param out OutputStream | ||
* @return the number of bytes copied | ||
* @throws IOException in case of I/O errors | ||
*/ | ||
public static int copy(InputStream in, OutputStream out) throws IOException { | ||
AssertTool.notNull(in, "No InputStream specified"); | ||
AssertTool.notNull(out, "No OutputStream specified"); | ||
|
||
try { | ||
int byteCount = 0; | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int bytesRead; | ||
while ((bytesRead = in.read(buffer)) != -1) { | ||
out.write(buffer, 0, bytesRead); | ||
byteCount += bytesRead; | ||
} | ||
out.flush(); | ||
return byteCount; | ||
} finally { | ||
close(in); | ||
close(out); | ||
} | ||
} | ||
|
||
/** | ||
* Copy from byte array to OutputStream, Closes the stream when done. | ||
* | ||
* @param in the byte array | ||
* @param out OutputStream | ||
* @throws IOException in case of I/O errors | ||
*/ | ||
public static void copy(byte[] in, OutputStream out) throws IOException { | ||
AssertTool.notNull(in, "No input byte array specified"); | ||
AssertTool.notNull(out, "No OutputStream specified"); | ||
|
||
try { | ||
out.write(in); | ||
} | ||
finally { | ||
close(out); | ||
} | ||
} | ||
|
||
/** | ||
* Copy the contents of the given InputStream into a new byte array. | ||
* Closes the stream when done. | ||
* @param in the stream to copy from (may be {@code null} or empty) | ||
* @return the new byte array that has been copied to (possibly empty) | ||
* @throws IOException in case of I/O errors | ||
*/ | ||
public static byte[] copyToByteArray(InputStream in) throws IOException { | ||
if (in == null) { | ||
return new byte[0]; | ||
} | ||
|
||
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); | ||
copy(in, out); | ||
return out.toByteArray(); | ||
} | ||
|
||
/** | ||
* Attempt to close the supplied {@link Closeable}, ignore exceptions | ||
* | ||
* @param closeable the {@code Closeable} to close | ||
*/ | ||
private static void close(Closeable closeable) { | ||
try { | ||
closeable.close(); | ||
} | ||
catch (IOException ex) { | ||
// ignore | ||
} | ||
} | ||
|
||
|
||
} |
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,21 @@ | ||
package com.xxl.tool.test.core; | ||
|
||
import com.xxl.tool.core.AssertTool; | ||
import com.xxl.tool.core.StringTool; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class AssertToolTest { | ||
|
||
@Test | ||
public void isTrueTest() { | ||
AssertTool.isTrue(true, "not true"); | ||
} | ||
|
||
@Test | ||
public void isNullTest() { | ||
AssertTool.notNull(new Object(), "not null"); | ||
} | ||
|
||
} |
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,28 @@ | ||
package com.xxl.tool.test.io; | ||
|
||
import com.xxl.tool.io.StreamTool; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringBufferInputStream; | ||
|
||
public class StreamToolTest { | ||
private static final Logger logger = LoggerFactory.getLogger(StreamToolTest.class); | ||
|
||
@Test | ||
public void testJson() { | ||
try { | ||
InputStream inputStream = new ByteArrayInputStream("input".getBytes()); | ||
|
||
byte[] result = StreamTool.copyToByteArray(inputStream); | ||
logger.info(new String(result)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |