Java Code Examples for org.apache.commons.lang3.ArrayUtils#EMPTY_BYTE_ARRAY

The following examples show how to use org.apache.commons.lang3.ArrayUtils#EMPTY_BYTE_ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoSerializer.java    From journalkeeper with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ObjectSerializer.java    From peer-os with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: PGPMessenger.java    From peer-os with Apache License 2.0 6 votes vote down vote up
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 );
    }
}
 
Example 4
Source File: PGPMessenger.java    From peer-os with Apache License 2.0 6 votes vote down vote up
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 );
    }
}
 
Example 5
Source File: ByteArrayConstructor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object construct(Object[] args) {
	if (args.length == 0) {
		return ArrayUtils.EMPTY_BYTE_ARRAY;
	} else {
		return super.construct(args);
	}
}
 
Example 6
Source File: DownloadedContent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
InMemory(final byte[] byteArray) {
    if (byteArray == null) {
        bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY;
    }
    else {
        bytes_ = byteArray;
    }
}
 
Example 7
Source File: FailingHttpStatusCodeExceptionTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example 8
Source File: DownloadedContent.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
InMemory(final byte[] byteArray) {
    if (byteArray == null) {
        bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY;
    }
    else {
        bytes_ = byteArray;
    }
}
 
Example 9
Source File: AbstractSocketCodec.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
/**
 * 将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;
}
 
Example 10
Source File: BazaarRestClient.java    From peer-os with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 11
Source File: DateSerializer.java    From peer-os with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: ProtobufUtils.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** 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;
}
 
Example 13
Source File: SecurityUtilities.java    From peer-os with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: DCC.java    From riiablo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: MessageContentUtil.java    From peer-os with Apache License 2.0 5 votes vote down vote up
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 );
    }
}
 
Example 16
Source File: ByteArrayConstructor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object construct(Object[] args) {
	if (args.length == 0) {
		return ArrayUtils.EMPTY_BYTE_ARRAY;
	} else {
		return super.construct(args);
	}
}
 
Example 17
Source File: TestRegionSplitter.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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'});
}
 
Example 18
Source File: JceKeyCipher.java    From aws-encryption-sdk-java with Apache License 2.0 4 votes vote down vote up
WrappingData(final Cipher cipher, final byte[] extraInfo) {
    this.cipher = cipher;
    this.extraInfo = extraInfo != null ? extraInfo : ArrayUtils.EMPTY_BYTE_ARRAY;
}
 
Example 19
Source File: BaggageImpl.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** 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();
}
 
Example 20
Source File: WebResponseData.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}