下面列出了org.apache.commons.lang3.ArrayUtils#EMPTY_BYTE_ARRAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public byte[] serialize(Object entry) {
if (entry == null) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
Kryo kryo = kryoPool.borrow();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
Output output = new Output(outputStream);
if (type == null) {
kryo.writeClassAndObject(output, entry);
} else {
kryo.writeObject(output, entry);
}
kryoPool.release(kryo);
output.flush();
byte[] result = outputStream.toByteArray();
output.close();
return result;
}
public byte[] produce( byte data[] ) throws PGPException
{
if ( ArrayUtils.isEmpty( data ) )
{
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
try
{
byte signedData[] = PGPSign.sign( data, senderPrivateKey );
return PGPEncrypt.encrypt( signedData, recipientPublicKey );
}
catch ( Exception e )
{
throw new PGPException( "Cannot sign and encrypt a message.", e );
}
}
public byte[] consume( byte encData[] ) throws PGPException
{
if ( ArrayUtils.isEmpty( encData ) )
{
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
try
{
byte signedData[] = PGPDecrypt.decrypt( encData, senderPrivateKey );
return PGPVerify.verify( signedData, recipientPublicKey );
}
catch ( Exception e )
{
throw new PGPException( "Cannot decrypt and verify a signature.", e );
}
}
/**
* Converts any given object to a xml-fragment-string, which is further
* converted to a binary representation.
*
* @param o any object
*
* @return a binary representation of the xml-fragment
*/
@Override
public byte[] serialize( Object o )
{
try
{
JAXBContext context = JAXBContext.newInstance( o.getClass() );
Marshaller m = context.createMarshaller();
m.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE );
// comment this to save space and reduce readability
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
ByteArrayOutputStream stream = new ByteArrayOutputStream();
m.marshal( o, stream );
return stream.toByteArray();
}
catch ( JAXBException e )
{
LOG.warn( e.getMessage() );
}
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
/**
* 将byteBuf中剩余的字节读取到一个字节数组中。
*
* @param byteBuf 方法返回之后 readableBytes == 0
* @return new instance
*/
@Nonnull
private static byte[] readRemainBytes(ByteBuf byteBuf) {
if (byteBuf.readableBytes() == 0) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
byte[] result = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(result);
return result;
}
@Override
public Object construct(Object[] args) {
if (args.length == 0) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
} else {
return super.construct(args);
}
}
InMemory(final byte[] byteArray) {
if (byteArray == null) {
bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY;
}
else {
bytes_ = byteArray;
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void constructorWithWebResponse() throws Exception {
final List<NameValuePair> emptyList = Collections.emptyList();
final WebResponseData webResponseData = new WebResponseData(
ArrayUtils.EMPTY_BYTE_ARRAY, HttpStatus.SC_NOT_FOUND, "not found",
emptyList);
final WebResponse webResponse = new WebResponse(webResponseData, URL_FIRST, HttpMethod.GET, 10);
final FailingHttpStatusCodeException e = new FailingHttpStatusCodeException(webResponse);
assertEquals(webResponse, e.getResponse());
assertEquals(webResponse.getStatusMessage(), e.getStatusMessage());
assertEquals(webResponse.getStatusCode(), e.getStatusCode());
assertTrue("message doesn't contain failing url", e.getMessage().indexOf(URL_FIRST.toExternalForm()) > -1);
}
InMemory(final byte[] byteArray) {
if (byteArray == null) {
bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY;
}
else {
bytes_ = byteArray;
}
}
private byte[] readContent( Response response ) throws IOException
{
if ( response.getEntity() == null )
{
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = ( InputStream ) response.getEntity();
IOUtils.copy( is, bos );
return bos.toByteArray();
}
/**
* Converts from Date-object to byte-array.
*
* @param o the Date-object
*
* @return a binary representation
*/
@Override
public byte[] serialize( Object o )
{
try
{
return this.marshal( ( Date ) o ).getBytes();
}
catch ( Exception e )
{
LOG.warn( e.getMessage() );
}
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
/** Serialize a BaggageMessage to a byte string, returning an empty bytestring if the provided message is null or
* invalid */
public static byte[] toByteArray(BaggageMessage message) {
if (message != null) {
try {
return message.toByteArray();
} catch (Throwable t) {}
}
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
public static byte[] generateKey( byte[] data )
{
try
{
MessageDigest sha = MessageDigest.getInstance( "SHA-1" );
byte[] key = sha.digest( data );
return Arrays.copyOf( key, DEFAULT_KEY_SIZE / 8 );
}
catch ( NoSuchAlgorithmException e )
{
LOG.warn( e.getMessage() );
}
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
Frame read(BitStream bitStream, Direction d) throws IOException {
variable0 = (int) bitStream.readUnsigned(BITS_WIDTH_TABLE[d.variable0Bits]);
width = (int) bitStream.readUnsigned(BITS_WIDTH_TABLE[d.widthBits]);
height = (int) bitStream.readUnsigned(BITS_WIDTH_TABLE[d.heightBits]);
xOffset = bitStream.readSigned (BITS_WIDTH_TABLE[d.xOffsetBits]);
yOffset = bitStream.readSigned (BITS_WIDTH_TABLE[d.yOffsetBits]);
optionalBytes = (int) bitStream.readUnsigned(BITS_WIDTH_TABLE[d.optionalBytesBits]);
codedBytes = (int) bitStream.readUnsigned(BITS_WIDTH_TABLE[d.codedBytesBits]);
flip = bitStream.readBit();
optionalBytesData = ArrayUtils.EMPTY_BYTE_ARRAY;
box = new BBox();
box.xMin = xOffset;
box.xMax = box.xMin + width - 1;
if (flip != 0) { // bottom-up
box.yMin = yOffset;
box.yMax = box.yMin + height - 1;
} else { // top-down
box.yMax = yOffset;
box.yMin = box.yMax - height + 1;
}
box.width = box.xMax - box.xMin + 1;
box.height = box.yMax - box.yMin + 1;
return this;
}
private static byte[] encryptData( SecurityManager securityManager, String hostIdTarget, byte[] data )
throws PGPException
{
try
{
if ( ArrayUtils.isEmpty( data ) )
{
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
else
{
EncryptionTool encTool = securityManager.getEncryptionTool();
KeyManager keyMan = securityManager.getKeyManager();
PGPPublicKey pubKey = keyMan.getRemoteHostPublicKey( hostIdTarget );
if ( pubKey != null )
{
LOG.debug( String.format( " ****** Encrypting with %s ****** ", hostIdTarget ) );
return encTool.encrypt( data, pubKey, true );
}
else
{
LOG.debug( String.format( " ****** Encryption error. Could not find Public key : %s ****** ",
hostIdTarget ) );
throw new PGPException( "Cannot find Public Key" );
}
}
}
catch ( Exception ex )
{
throw new PGPException( "Error in encryptData", ex );
}
}
@Override
public Object construct(Object[] args) {
if (args.length == 0) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
} else {
return super.construct(args);
}
}
/** Construct a BaggageMessage protobuf message and serialize it to a byte array. If this baggage is empty, an empty
* byte array will be returned */
public byte[] toByteArray() {
BaggageMessage message = buildMessage();
return message == null ? ArrayUtils.EMPTY_BYTE_ARRAY : message.toByteArray();
}
WrappingData(final Cipher cipher, final byte[] extraInfo) {
this.cipher = cipher;
this.extraInfo = extraInfo != null ? extraInfo : ArrayUtils.EMPTY_BYTE_ARRAY;
}
/**
* Unit tests for the UniformSplit algorithm. Makes sure it divides up the space of
* keys in the way that we expect.
*/
@Test
public void unitTestUniformSplit() {
UniformSplit splitter = new UniformSplit();
// Check splitting while starting from scratch
try {
splitter.split(1);
throw new AssertionError("Splitting into <2 regions should have thrown exception");
} catch (IllegalArgumentException e) { }
byte[][] twoRegionsSplits = splitter.split(2);
assertEquals(1, twoRegionsSplits.length);
assertArrayEquals(twoRegionsSplits[0], new byte[] { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 });
byte[][] threeRegionsSplits = splitter.split(3);
assertEquals(2, threeRegionsSplits.length);
byte[] expectedSplit0 = new byte[] {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
assertArrayEquals(expectedSplit0, threeRegionsSplits[0]);
byte[] expectedSplit1 = new byte[] {(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA};
assertArrayEquals(expectedSplit1, threeRegionsSplits[1]);
// Check splitting existing regions that have start and end points
byte[] splitPoint = splitter.split(new byte[] {0x10}, new byte[] {0x30});
assertArrayEquals(new byte[] { 0x20 }, splitPoint);
byte[] lastRow = new byte[] {xFF, xFF, xFF, xFF, xFF, xFF, xFF, xFF};
assertArrayEquals(lastRow, splitter.lastRow());
byte[] firstRow = ArrayUtils.EMPTY_BYTE_ARRAY;
assertArrayEquals(firstRow, splitter.firstRow());
splitPoint = splitter.split(firstRow, new byte[] {0x20});
assertArrayEquals(splitPoint, new byte[] { 0x10 });
splitPoint = splitter.split(new byte[] {(byte)0xdf, xFF, xFF, xFF, xFF,
xFF, xFF, xFF}, lastRow);
assertArrayEquals(splitPoint, new byte[] { (byte) 0xef, xFF, xFF, xFF, xFF, xFF, xFF, xFF});
splitPoint = splitter.split(new byte[] {'a', 'a', 'a'}, new byte[] {'a', 'a', 'b'});
assertArrayEquals(splitPoint, new byte[] { 'a', 'a', 'a', (byte) 0x80 });
// Check splitting region with multiple mappers per region
byte[][] splits = splitter.split(new byte[] {'a', 'a', 'a'}, new byte[] {'a', 'a', 'd'},
3, false);
assertEquals(2, splits.length);
assertArrayEquals(splits[0], new byte[]{'a', 'a', 'b'});
assertArrayEquals(splits[1], new byte[]{'a', 'a', 'c'});
splits = splitter.split(new byte[] {'a', 'a', 'a'}, new byte[] {'a', 'a', 'e'}, 2, true);
assertEquals(3, splits.length);
assertArrayEquals(splits[1], new byte[] { 'a', 'a', 'c'});
}
/**
* Constructs without data stream for subclasses that override getBody().
*
* @param statusCode Status code from the server
* @param statusMessage Status message from the server
* @param responseHeaders Headers in this response
*/
protected WebResponseData(final int statusCode,
final String statusMessage, final List<NameValuePair> responseHeaders) {
this(ArrayUtils.EMPTY_BYTE_ARRAY, statusCode, statusMessage, responseHeaders);
}