Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  import java.util.Arrays;
  
Value node that contains Base64 encoded binary value, which will be output and stored as Json String value.
 
 public final class BinaryNode
     extends ValueNode
 {
     final static BinaryNode EMPTY_BINARY_NODE = new BinaryNode(new byte[0]);
 
     final byte[] _data;
 
     public BinaryNode(byte[] data)
     {
          = data;
     }
 
     public BinaryNode(byte[] dataint offsetint length)
     {
         if (offset == 0 && length == data.length) {
              = data;
         } else {
              = new byte[length];
             System.arraycopy(dataoffset, 0, length);
         }
     }
 
     public static BinaryNode valueOf(byte[] data)
     {
         if (data == null) {
             return null;
         }
         if (data.length == 0) {
             return ;
         }
         return new BinaryNode(data);
     }
 
     public static BinaryNode valueOf(byte[] dataint offsetint length)
     {
         if (data == null) {
             return null;
         }
         if (length == 0) {
             return ;
         }
         return new BinaryNode(dataoffsetlength);
     }
 
     @Override
     public JsonNodeType getNodeType()
     {
         return .;
     }
 
     @Override
     public JsonToken asToken() {
         /* No distinct type; could use one for textual values,
          * but given that it's not in text form at this point,
          * embedded-object is closest
          */
         return .;
     }

    

Note: caller is not to modify returned array in any way, since it is not a copy but reference to the underlying byte array.

 
     @Override
     public byte[] binaryValue() { return ; }

    
Hmmh. This is not quite as efficient as using serialize(com.fasterxml.jackson.core.JsonGenerator,com.fasterxml.jackson.databind.SerializerProvider), but will work correctly.
 
     @Override
     public String asText() {
         return Base64Variants.getDefaultVariant().encode(false);
     }
 
     @Override
     public final void serialize(JsonGenerator jgSerializerProvider provider)
         throws IOExceptionJsonProcessingException
     {
         jg.writeBinary(provider.getConfig().getBase64Variant(),
                 , 0, .);
     }
 
     @Override
     public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) { // final class, can do this
            return false;
        }
        return Arrays.equals(((BinaryNodeo).);
    }
    @Override
    public int hashCode() {
        return ( == null) ? -1 : .;
    }

    
Different from other values, since contents need to be surrounded by (double) quotes.
    @Override
    public String toString()
    {
        return Base64Variants.getDefaultVariant().encode(true);
    }
New to GrepCode? Check out our FAQ X