Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2011 David Yu dyuproject@gmail.com
  //------------------------------------------------------------------------
  //Licensed under the Apache License, Version 2.0 (the "License");
  //you may not use this file except in compliance with the License.
  //You may obtain a copy of the License at 
  //http://www.apache.org/licenses/LICENSE-2.0
  //Unless required by applicable law or agreed to in writing, software
  //distributed under the License is distributed on an "AS IS" BASIS,
 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 //See the License for the specific language governing permissions and
 //limitations under the License.
 //========================================================================
 
 package io.protostuff;
 
 import java.util.List;
 
Smile IO utilities for messages.

Author(s):
David Yu
Created:
Feb 10, 2011
 
 public final class SmileIOUtil
 {
 
     private SmileIOUtil()
     {
     }

    
A custom factory simply to expose certain fields.
 
     public static final class Factory extends SmileFactory
     {

        
Needed by jackson's internal utf8 stream parser.
 
         public BytesToNameCanonicalizer getRootByteSymbols()
         {
             return ;
         }

        
Returns the parser feature flags.
 
         public int getParserFeatures()
         {
             return ;
         }

        
Returns the smile-specific parser feature flags.
 
         public int getSmileParserFeatures()
         {
             return ;
         }

        
Returns the generator feature flags.
 
         public int getGeneratorFeatures()
         {
             return ;
         }

        
Returns the smile-specific generator feature flags.
 
         public int getSmileGeneratorFeatures()
         {
             return ;
         }
 
     }

    
The default smile factory for creating smile parsers and generators.
 
     public static final Factory DEFAULT_SMILE_FACTORY = new Factory();
 
     static
     {
    }

    
Creates a smile pipe from a byte array.
    public static Pipe newPipe(byte[] databoolean numericthrows IOException
    {
        return newPipe(data, 0, data.lengthnumeric);
    }

    
Creates a smile pipe from a byte array.
    public static Pipe newPipe(byte[] dataint offsetint lengthboolean numeric)
            throws IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                datafalse);
        final SmileParser parser = newSmileParser(nulldataoffsetoffset + lengthfalse,
                context);
        return JsonIOUtil.newPipe(parsernumeric);
        // return JsonIOUtil.newPipe(DEFAULT_SMILE_FACTORY.createJsonParser(data, offset, length), numeric);
    }

    
Creates a smile pipe from an java.io.InputStream.
    public static Pipe newPipe(InputStream inboolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                infalse);
        final SmileParser parser = newSmileParser(incontext.allocReadIOBuffer(), 0, 0,
                truecontext);
        return JsonIOUtil.newPipe(parsernumeric);
        // return JsonIOUtil.newPipe(DEFAULT_SMILE_FACTORY.createJsonParser(in), numeric);
    }

    
Creates a com.fasterxml.jackson.dataformat.smile.SmileParser from the inputstream with the supplied buf inBuffer to use.
    public static SmileParser newSmileParser(InputStream inbyte[] buf,
            int offsetint limitthrows IOException
    {
        return newSmileParser(inbufoffsetlimitfalse,
                new IOContext(._getBufferRecycler(), in,
                        false));
    }

    
Creates a com.fasterxml.jackson.dataformat.smile.SmileParser from the inputstream with the supplied buf inBuffer to use.
    static SmileParser newSmileParser(InputStream inbyte[] buf,
            int offsetint limitboolean bufferRecyclableIOContext context)
            throws IOException
    {
        return new SmileParser(context,
                .getParserFeatures(),
                .getSmileParserFeatures(),
                .getCodec(),
                .getRootByteSymbols().makeChild(truetrue),
                in,
                bufoffsetlimitbufferRecyclable);
    }

    
Creates a com.fasterxml.jackson.dataformat.smile.SmileGenerator for the outputstream with the supplied buf outBuffer to use.
    public static SmileGenerator newSmileGenerator(OutputStream outbyte[] buf)
    {
        return newSmileGenerator(outbuf, 0, falsenew IOContext(
                ._getBufferRecycler(), outfalse));
    }

    
Creates a com.fasterxml.jackson.dataformat.smile.SmileGenerator for the outputstream with the supplied buf outBuffer to use.
    static SmileGenerator newSmileGenerator(OutputStream outbyte[] bufint offset,
            boolean bufferRecyclableIOContext context)
    {
        return new SmileGenerator(context,
                .getGeneratorFeatures(),
                .getSmileGeneratorFeatures(),
                .getCodec(),
                out,
                buf,
                offset,
                bufferRecyclable);
    }

    
Merges the message with the byte array using the given schema.
    public static <T> void mergeFrom(byte[] data, T messageSchema<T> schema,
            boolean numericthrows IOException
    {
        mergeFrom(data, 0, data.lengthmessageschemanumeric);
    }

    
Merges the message with the byte array using the given schema.
    public static <T> void mergeFrom(byte[] dataint offsetint length, T message,
            Schema<T> schemaboolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                datafalse);
        final SmileParser parser = newSmileParser(nulldataoffsetoffset + lengthfalse,
                context);
        // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(data, offset, length);
        try
        {
            JsonIOUtil.mergeFrom(parsermessageschemanumeric);
        }
        finally
        {
            parser.close();
        }
    }

    
Merges the message from the java.io.InputStream using the given schema.
    public static <T> void mergeFrom(InputStream in, T messageSchema<T> schema,
            boolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                infalse);
        final SmileParser parser = newSmileParser(incontext.allocReadIOBuffer(), 0, 0,
                truecontext);
        // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
        try
        {
            JsonIOUtil.mergeFrom(parsermessageschemanumeric);
        }
        finally
        {
            parser.close();
        }
    }

    
Merges the message from the java.io.InputStream using the given schema.

The io.protostuff.LinkedBuffer's internal byte array will be used when reading the message.

    public static <T> void mergeFrom(InputStream in, T messageSchema<T> schema,
            boolean numericLinkedBuffer bufferthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                infalse);
        final SmileParser parser = newSmileParser(inbuffer.buffer, 0, 0, falsecontext);
        // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
        try
        {
            JsonIOUtil.mergeFrom(parsermessageschemanumeric);
        }
        finally
        {
            parser.close();
        }
    }

    
Serializes the message into a byte array using the given schema.
    public static <T> byte[] toByteArray(T messageSchema<T> schemaboolean numeric)
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try
        {
            writeTo(baosmessageschemanumeric);
        }
        catch (IOException e)
        {
            throw new RuntimeException("Serializing to a byte array threw an IOException " +
                    "(should never happen)."e);
        }
        return baos.toByteArray();
    }

    
Serializes the message into a byte array using the given schema.

The io.protostuff.LinkedBuffer's internal byte array will be used as the primary buffer when writing the message.

    public static <T> byte[] toByteArray(T messageSchema<T> schemaboolean numeric,
            LinkedBuffer buffer)
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try
        {
            writeTo(baosmessageschemanumericbuffer);
        }
        catch (IOException e)
        {
            throw new RuntimeException("Serializing to a byte array threw an IOException " +
                    "(should never happen)."e);
        }
        return baos.toByteArray();
    }

    
Serializes the message into an java.io.OutputStream using the given schema.
    public static <T> void writeTo(OutputStream out, T messageSchema<T> schema,
            boolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                outfalse);
        final SmileGenerator generator = newSmileGenerator(out,
                context.allocWriteEncodingBuffer(), 0, truecontext);
        // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
        try
        {
            JsonIOUtil.writeTo(generatormessageschemanumeric);
        }
        finally
        {
            generator.close();
        }
    }

    
Serializes the message into an java.io.OutputStream using the given schema.

The io.protostuff.LinkedBuffer's internal byte array will be used as the primary buffer when writing the message.

    public static <T> void writeTo(OutputStream out, T messageSchema<T> schema,
            boolean numericLinkedBuffer bufferthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                outfalse);
        final SmileGenerator generator = newSmileGenerator(outbuffer.buffer, 0, false,
                context);
        // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
        try
        {
            JsonIOUtil.writeTo(generatormessageschemanumeric);
        }
        finally
        {
            generator.close();
        }
    }

    
Serializes the messages into the stream using the given schema.
    public static <T> void writeListTo(OutputStream outList<T> messages,
            Schema<T> schemaboolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                outfalse);
        final SmileGenerator generator = newSmileGenerator(out,
                context.allocWriteEncodingBuffer(), 0, truecontext);
        // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
        try
        {
            JsonIOUtil.writeListTo(generatormessagesschemanumeric);
        }
        finally
        {
            generator.close();
        }
    }

    
Serializes the messages into the stream using the given schema.

The io.protostuff.LinkedBuffer's internal byte array will be used as the primary buffer when writing the message.

    public static <T> void writeListTo(OutputStream outList<T> messages,
            Schema<T> schemaboolean numericLinkedBuffer bufferthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                outfalse);
        final SmileGenerator generator = newSmileGenerator(outbuffer.buffer, 0, false,
                context);
        // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
        try
        {
            JsonIOUtil.writeListTo(generatormessagesschemanumeric);
        }
        finally
        {
            generator.close();
        }
    }

    
Parses the messages from the stream using the given schema.
    public static <T> List<T> parseListFrom(InputStream inSchema<T> schema,
            boolean numericthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                infalse);
        final SmileParser parser = newSmileParser(incontext.allocReadIOBuffer(), 0, 0,
                truecontext);
        // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
        try
        {
            return JsonIOUtil.parseListFrom(parserschemanumeric);
        }
        finally
        {
            parser.close();
        }
    }

    
Parses the messages from the stream using the given schema.

The io.protostuff.LinkedBuffer's internal byte array will be used when reading the message.

    public static <T> List<T> parseListFrom(InputStream inSchema<T> schema,
            boolean numericLinkedBuffer bufferthrows IOException
    {
        final IOContext context = new IOContext(._getBufferRecycler(),
                infalse);
        final SmileParser parser = newSmileParser(inbuffer.buffer, 0, 0, falsecontext);
        // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
        try
        {
            return JsonIOUtil.parseListFrom(parserschemanumeric);
        }
        finally
        {
            parser.close();
        }
    }
New to GrepCode? Check out our FAQ X