Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.core.io;
  
  import java.io.*;
  
String token that can lazily serialize String contained and then reuse that serialization later on. This is similar to JDBC prepared statements, for example, in that instances should only be created when they are used more than use; prime candidates are various serializers.

Class is final for performance reasons and since this is not designed to be extensible or customizable (customizations would occur in calling code)

 
 public class SerializedString
     implements SerializableStringjava.io.Serializable
 {
     protected final String _value;
 
     /* 13-Dec-2010, tatu: Whether use volatile or not is actually an important
      *   decision for multi-core use cases. Cost of volatility can be non-trivial
      *   for heavy use cases, and serialized-string instances are accessed often.
      *   Given that all code paths with common Jackson usage patterns go through
      *   a few memory barriers (mostly with cache/reuse pool access) it seems safe
      *   enough to omit volatiles here, given how simple lazy initialization is.
      *   This can be compared to how {@link String#intern} works; lazily and
      *   without synchronization or use of volatile keyword.
      */
     
     protected /*volatile*/ byte[] _quotedUTF8Ref;
 
     protected /*volatile*/ byte[] _unquotedUTF8Ref;
 
     protected /*volatile*/ char[] _quotedChars;
 
     public SerializedString(String v) {
         if (v == null) {
             throw new IllegalStateException("Null String illegal for SerializedString");
         }
          = v;
     }
     
     /*
     /**********************************************************
     /* Serializable overrides
     /**********************************************************
      */

    
Ugly hack, to work through the requirement that _value is indeed final, and that JDK serialization won't call ctor(s).

Since:
2.1
 
     protected transient String _jdkSerializeValue;
 
     private void readObject(ObjectInputStream inthrows IOException {
          = in.readUTF();
     }
 
     private void writeObject(ObjectOutputStream outthrows IOException {
         out.writeUTF();
     }
 
     protected Object readResolve() {
         return new SerializedString();
     }
 
     /*
     /**********************************************************
     /* API
     /**********************************************************
      */
 
 //  @Override
     public final String getValue() { return ; }
    
    
Returns length of the String as characters
 
 //  @Override
     public final int charLength() { return .length(); }
     
 //  @Override
     public final char[] asQuotedChars()
     {
         char[] result = ;
         if (result == null) {
             result = JsonStringEncoder.getInstance().quoteAsString();
              = result;
         }
         return result;
     }

    
Accessor for accessing value that has been quoted using JSON quoting rules, and encoded using UTF-8 encoding.
//  @Override
    public final byte[] asUnquotedUTF8()
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().encodeAsUTF8();
              = result;
        }
        return result;
    }

    
Accessor for accessing value as is (without JSON quoting) encoded using UTF-8 encoding.
//  @Override
    public final byte[] asQuotedUTF8()
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().quoteAsUTF8();
             = result;
        }
        return result;
    }
    /*
    /**********************************************************
    /* Additional 2.0 methods for appending/writing contents
    /**********************************************************
     */
//  @Override
    public int appendQuotedUTF8(byte[] bufferint offset)
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().quoteAsUTF8();
             = result;
        }
        final int length = result.length;
        if ((offset + length) > buffer.length) {
            return -1;
        }
        System.arraycopy(result, 0, bufferoffsetlength);
        return length;
    }
//  @Override
    public int appendQuoted(char[] bufferint offset)
    {
        char[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().quoteAsString();
             = result;
        }
        final int length = result.length;
        if ((offset + length) > buffer.length) {
            return -1;
        }
        System.arraycopy(result, 0, bufferoffsetlength);
        return length;
    }
//  @Override
    public int appendUnquotedUTF8(byte[] bufferint offset)
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().encodeAsUTF8();
              = result;
        }
        final int length = result.length;
        if ((offset + length) > buffer.length) {
            return -1;
        }
        System.arraycopy(result, 0, bufferoffsetlength);
        return length;
    }
//  @Override
    public int appendUnquoted(char[] bufferint offset)
    {
        String str = ;
        final int length = str.length();
        if ((offset + length) > buffer.length) {
            return -1;
        }
        str.getChars(0,  lengthbufferoffset);
        return length;
    }
//  @Override
    public int writeQuotedUTF8(OutputStream outthrows IOException
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().quoteAsUTF8();
             = result;
        }
        final int length = result.length;
        out.write(result, 0, length);
        return length;
    }
//  @Override
    public int writeUnquotedUTF8(OutputStream outthrows IOException
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().encodeAsUTF8();
              = result;
        }
        final int length = result.length;
        out.write(result, 0, length);
        return length;
    }
//  @Override
    public int putQuotedUTF8(ByteBuffer buffer)
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().quoteAsUTF8();
             = result;
        }
        final int length = result.length;
        if (length > buffer.remaining()) {
            return -1;
        }
        buffer.put(result, 0, length);
        return length;
    }
//  @Override
    public int putUnquotedUTF8(ByteBuffer buffer)
    {
        byte[] result = ;
        if (result == null) {
            result = JsonStringEncoder.getInstance().encodeAsUTF8();
              = result;
        }
        final int length = result.length;
        if (length > buffer.remaining()) {
            return -1;
        }
        buffer.put(result, 0, length);
        return length;
    }
    
    /*
    /**********************************************************
    /* Standard method overrides
    /**********************************************************
     */
    @Override
    public final String toString() { return ; }
    
    @Override
    public final int hashCode() { return .hashCode(); }
    @Override
    public final boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == null || o.getClass() != getClass()) return false;
        SerializedString other = (SerializedStringo;
        return .equals(other._value);
    }
New to GrepCode? Check out our FAQ X