Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.dataformat.smile;
  
  import java.io.*;
  import java.net.URL;
  
Factory used for constructing SmileParser and SmileGenerator instances; both of which handle Smile encoded data.

Extends com.fasterxml.jackson.core.JsonFactory mostly so that users can actually use it in place of regular non-Smile factory instances.

Note on using non-byte-based sources/targets (char based, like java.io.Reader and java.io.Writer): these can not be used for Smile-format documents, and thus will either downgrade to textual JSON (when parsing), or throw exception (when trying to create generator).

Author(s):
Tatu Saloranta
 
 public class SmileFactory extends JsonFactory
 {
     private static final long serialVersionUID = -1696783009312472365L;

    
Name used to identify Smile format. (and returned by getFormatName()
 
     public final static String FORMAT_NAME_SMILE = "Smile";
    
    
Bitfield (set of flags) of all parser features that are enabled by default.
 
     final static int DEFAULT_SMILE_PARSER_FEATURE_FLAGS = SmileParser.Feature.collectDefaults();

    
Bitfield (set of flags) of all generator features that are enabled by default.
 
     final static int DEFAULT_SMILE_GENERATOR_FEATURE_FLAGS = SmileGenerator.Feature.collectDefaults();
 
     /*
     /**********************************************************
     /* Configuration
     /**********************************************************
      */

    
Whether non-supported methods (ones trying to output using char-based targets like java.io.Writer, for example) should be delegated to regular Jackson JSON processing (if set to true); or throw java.lang.UnsupportedOperationException (if set to false)
 
     protected boolean _cfgDelegateToTextual;
 
 
 
     /*
     /**********************************************************
     /* Factory construction, configuration
     /**********************************************************
      */

    
Default constructor used to create factory instances. Creation of a factory instance is a light-weight operation, but it is still a good idea to reuse limited number of factory instances (and quite often just a single instance): factories are used as context for storing some reused processing objects (such as symbol tables parsers use) and this reuse only works within context of a single factory instance.
 
     public SmileFactory() { this(null); }
 
     public SmileFactory(ObjectCodec oc) { super(oc); }
 
     // @since 2.1
     @Override
     public SmileFactory copy()
     {
         _checkInvalidCopy(SmileFactory.class);
         // note: as with base class, must NOT copy mapper reference
         return new SmileFactory(null);
     }
     
     public void delegateToTextual(boolean state) {
          = state;
     }
    /*                                                                                       
    /**********************************************************                              
    /* Versioned                                                                             
    /**********************************************************                              
     */
    @Override
    public Version version() {
        return .;
    }
    
    /*
    /**********************************************************
    /* Format detection functionality (since 1.8)
    /**********************************************************
     */
    
    @Override
    public String getFormatName() {
        return ;
    }
    // Defaults work fine for this:
    // public boolean canUseSchema(FormatSchema schema) { }

    
Sub-classes need to override this method (as of 1.8)
    @Override
    public MatchStrength hasFormat(InputAccessor accthrows IOException {
        return SmileParserBootstrapper.hasSmileFormat(acc);
    }
    
    /*
    /**********************************************************
    /* Configuration, parser settings
    /**********************************************************
     */

    
Method for enabling or disabling specified parser feature (check SmileParser.Feature for list of features)
    public final SmileFactory configure(SmileParser.Feature fboolean state)
    {
        if (state) {
            enable(f);
        } else {
            disable(f);
        }
        return this;
    }

    
Method for enabling specified parser feature (check SmileParser.Feature for list of features)
    public SmileFactory enable(SmileParser.Feature f) {
         |= f.getMask();
        return this;
    }

    
Method for disabling specified parser features (check SmileParser.Feature for list of features)
    public SmileFactory disable(SmileParser.Feature f) {
         &= ~f.getMask();
        return this;
    }

    
Checked whether specified parser feature is enabled.
    public final boolean isEnabled(SmileParser.Feature f) {
        return ( & f.getMask()) != 0;
    }
    /*
    /**********************************************************
    /* Configuration, generator settings
    /**********************************************************
     */

    
Method for enabling or disabling specified generator feature (check SmileGenerator.Feature for list of features)

Since:
1.2
    public final SmileFactory configure(SmileGenerator.Feature fboolean state) {
        if (state) {
            enable(f);
        } else {
            disable(f);
        }
        return this;
    }


    
Method for enabling specified generator features (check SmileGenerator.Feature for list of features)
         |= f.getMask();
        return this;
    }

    
Method for disabling specified generator feature (check SmileGenerator.Feature for list of features)
         &= ~f.getMask();
        return this;
    }

    
Check whether specified generator feature is enabled.
    public final boolean isEnabled(SmileGenerator.Feature f) {
        return ( & f.getMask()) != 0;
    }
    
    /*
    /**********************************************************
    /* Overridden parser factory methods, new (2.1)
    /**********************************************************
     */
    @Override
    public SmileParser createParser(File f)
        throws IOExceptionJsonParseException
    {
        return _createParser(new FileInputStream(f), _createContext(ftrue));
    }
    @Override
    public SmileParser createParser(URL url)
        throws IOExceptionJsonParseException
    {
        return _createParser(_optimizedStreamFromURL(url), _createContext(urltrue));
    }
    @Override
        throws IOExceptionJsonParseException
    {
        return _createParser(in_createContext(infalse));
    }
    //public JsonParser createJsonParser(Reader r)
    
    @Override
    public SmileParser createParser(byte[] data)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        return _createParser(data, 0, data.lengthctxt);
    }
    
    @Override
    public SmileParser createParser(byte[] dataint offsetint len)
        throws IOExceptionJsonParseException
    {
        return _createParser(dataoffsetlen_createContext(datatrue));
    }
   
    /*
    /**********************************************************
    /* Overridden parser factory methods, old (pre-2.1)
    /**********************************************************
     */
    
    

Deprecated:
Since 2.1 Use createParser(java.io.File) instead
Since:
2.1
    @Deprecated
    @Override
    public SmileParser createJsonParser(File f)
        throws IOExceptionJsonParseException
    {
        return _createParser(new FileInputStream(f), _createContext(ftrue));
    }

    

Deprecated:
Since 2.1 Use createParser(java.net.URL) instead
Since:
2.1
    @Deprecated
    @Override
    public SmileParser createJsonParser(URL url)
        throws IOExceptionJsonParseException
    {
        return _createParser(_optimizedStreamFromURL(url), _createContext(urltrue));
    }

    

Deprecated:
Since 2.1 Use createParser(java.io.InputStream) instead
Since:
2.1
    @Deprecated
    @Override
        throws IOExceptionJsonParseException
    {
        return _createParser(in_createContext(infalse));
    }
    //public JsonParser createJsonParser(Reader r)
    
    

Deprecated:
Since 2.1 Use createParser(byte[]) instead
Since:
2.1
    @Deprecated
    @Override
    public SmileParser createJsonParser(byte[] data)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        return _createParser(data, 0, data.lengthctxt);
    }
    
    

Deprecated:
Since 2.1 Use createParser(byte[],int,int) instead
Since:
2.1
    @Deprecated
    @Override
    public SmileParser createJsonParser(byte[] dataint offsetint len)
        throws IOExceptionJsonParseException
    {
        return _createParser(dataoffsetlen_createContext(datatrue));
    }
    /*
    /**********************************************************
    /* Overridden generator factory methods, new (2.1)
    /**********************************************************
     */

    
Method for constructing com.fasterxml.jackson.core.JsonGenerator for generating Smile-encoded output.

Since Smile format always uses UTF-8 internally, enc argument is ignored.

    @Override
        throws IOException
    {
        // false -> we won't manage the stream unless explicitly directed to
        return _createGenerator(out_createContext(outfalse));
    }

    
Method for constructing com.fasterxml.jackson.core.JsonGenerator for generating Smile-encoded output.

Since Smile format always uses UTF-8 internally, no encoding need to be passed to this method.

    @Override
    {
        // false -> we won't manage the stream unless explicitly directed to
        return _createGenerator(out_createContext(outfalse));
    }
    
    /*
    /**********************************************************
    /* Overridden generator factory methods, old (pre-2.1)
    /**********************************************************
     */
    
    

Deprecated:
Since 2.1 Use createGenerator(java.io.OutputStream) instead
Since:
2.1
    @Deprecated
    @Override
        throws IOException
    {
        // false -> we won't manage the stream unless explicitly directed to
        return _createGenerator(out_createContext(outfalse));
    }

    

Deprecated:
Since 2.1 Use createGenerator(java.io.OutputStream) instead
Since:
2.1
    @Deprecated
    @Override
    {
        // false -> we won't manage the stream unless explicitly directed to
        IOContext ctxt = _createContext(outfalse);
        return _createGenerator(outctxt);
    }
    @Deprecated
    @Override
        throws IOException
    {
        return _createGenerator(outctxt);
    }
    
    /*
    /******************************************************
    /* Overridden internal factory methods
    /******************************************************
     */
    //protected IOContext _createContext(Object srcRef, boolean resourceManaged)

    
Overridable factory method that actually instantiates desired parser.
    @Override
    protected SmileParser _createParser(InputStream inIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return new SmileParserBootstrapper(ctxtin).constructParser(,
        		);
    }

    
Overridable factory method that actually instantiates desired parser.
    @Override
    protected JsonParser _createParser(Reader rIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        if () {
            return super._createParser(rctxt);
        }
        throw new UnsupportedOperationException("Can not create generator for non-byte-based target");
    }

    
Overridable factory method that actually instantiates desired parser.
    @Override
    protected SmileParser _createParser(byte[] dataint offsetint lenIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return new SmileParserBootstrapper(ctxtdataoffsetlen).constructParser(
                ,
                isEnabled(..),
                );
    }

    
Overridable factory method that actually instantiates desired generator.
    @Override
    protected JsonGenerator _createGenerator(Writer outIOContext ctxt)
        throws IOException
    {
        if () {
            return super._createGenerator(outctxt);
        }
        throw new UnsupportedOperationException("Can not create generator for non-byte-based target");
    }
    @Override
    protected JsonGenerator _createUTF8Generator(OutputStream outIOContext ctxtthrows IOException {
        return _createGenerator(outctxt);
    }
    
    //public BufferRecycler _getBufferRecycler()
    @Override
    protected Writer _createWriter(OutputStream outJsonEncoding encIOContext ctxtthrows IOException
    {
        if () {
            return super._createWriter(outencctxt);
        }
        throw new UnsupportedOperationException("Can not create generator for non-byte-based target");
    }
    
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    
    protected SmileGenerator _createGenerator(OutputStream outIOContext ctxt)
        throws IOException
    {
        int feats = ;
        /* One sanity check: MUST write header if shared string values setting is enabled,
         * or quoting of binary data disabled.
         * But should we force writing, or throw exception, if settings are in conflict?
         * For now, let's error out...
         */
        SmileGenerator gen = new SmileGenerator(ctxtfeatsout);
        if ((feats & ...getMask()) != 0) {
            gen.writeHeader();
        } else {
            if ((feats & ...getMask()) != 0) {
                throw new JsonGenerationException(
                        "Inconsistent settings: WRITE_HEADER disabled, but CHECK_SHARED_STRING_VALUES enabled; can not construct generator"
                        +" due to possible data loss (either enable WRITE_HEADER, or disable CHECK_SHARED_STRING_VALUES to resolve)");
            }
            if ((feats & ...getMask()) == 0) {
        	throw new JsonGenerationException(
        			"Inconsistent settings: WRITE_HEADER disabled, but ENCODE_BINARY_AS_7BIT disabled; can not construct generator"
        			+" due to possible data loss (either enable WRITE_HEADER, or ENCODE_BINARY_AS_7BIT to resolve)");
            }
        }
        return gen;
    }
New to GrepCode? Check out our FAQ X