package com.dyuproject.protostuff;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * String serialization - deferred binding. 
 *
 * @author David Yu
 * @created Feb 4, 2010
 */
public abstract class StringSerializer
{
    
    /**
     * Serializes the string to bytes.
     */
    public abstract byte[] ser(String nonNullValue);
    
    /**
     * Deserializes the bytes to a string.
     */
    public abstract String deser(byte[] nonNullValue);
    
    private static final StringSerializer __utf8string = new StringSerializer()
    {
        public String deser(byte[] nonNullValue)
        {
            try
            {
                return new String(nonNullValue, "UTF-8");
            }
            catch (UnsupportedEncodingException e)
            {
                throw new RuntimeException(e);
            }
        }

        public byte[] ser(String nonNullValue)
        {
            try
            {
                return nonNullValue.getBytes("UTF-8");
            }
            catch (UnsupportedEncodingException e)
            {
                throw new RuntimeException(e);
            }
        }
    };
    
    private static final Charset utf8 = Charset.forName("UTF-8");
    
    private static final StringSerializer __utf8stringFromCharset = new StringSerializer()
    {
        public String deser(byte[] nonNullValue)
        {
            return new String(nonNullValue, utf8);
        }

        public byte[] ser(String nonNullValue)
        {
            return nonNullValue.getBytes(utf8);
        }
    };

    private static final StringSerializer __string = new StringSerializer()
    {
        public String deser(byte[] nonNullValue)
        {
            return new String(nonNullValue);
        }

        public byte[] ser(String nonNullValue)
        {
            return nonNullValue.getBytes();
        }
    };
    
    /**
     * UTF-8 string serializer.  Binding deferred.
     */
    public static final StringSerializer STRING = 
        "UTF-8".equals(Charset.defaultCharset().name()) ? __string : 
            (System.getProperty("charset.utf8")!=null ? __utf8stringFromCharset : __utf8string);


}

@@ -502,32 +502,183 @@
 lValue);%0D%0A    %0D%0A
+    /**%0D%0A     * Deserializes the bytes to a string.%0D%0A     */%0D%0A    public abstract String deser(byte%5B%5D nonNullValue, int offset, int len);%0D%0A    %0D%0A    %0D%0A
     private stat
@@ -734,32 +734,32 @@
 ngSerializer()%0D%0A
-
     %7B%0D%0A        p
@@ -1048,32 +1048,384 @@
    %7D%0D%0A        %7D%0D
+%0A        %0D%0A        public String deser(byte%5B%5D nonNullValue, int offset, int len)%0D%0A        %7B%0D%0A            try%0D%0A            %7B%0D%0A                return new String(nonNullValue, offset, len, %22UTF-8%22);%0D%0A            %7D%0D%0A            catch (UnsupportedEncodingException e)%0D%0A            %7B%0D%0A                throw new RuntimeException(e);%0D%0A            %7D%0D%0A        %7D%0D
 %0A%0D%0A        publi
@@ -2015,32 +2015,200 @@
 f8);%0D%0A        %7D%0D
+%0A        %0D%0A        public String deser(byte%5B%5D nonNullValue, int offset, int len)%0D%0A        %7B%0D%0A            return new String(nonNullValue, offset, len, utf8);%0D%0A        %7D%0D
 %0A%0D%0A        publi
@@ -2460,32 +2460,32 @@
 ue)%0D%0A        %7B%0D%0A
-
             retu
@@ -2516,32 +2516,194 @@
 ue);%0D%0A        %7D%0D
+%0A        %0D%0A        public String deser(byte%5B%5D nonNullValue, int offset, int len)%0D%0A        %7B%0D%0A            return new String(nonNullValue, offset, len);%0D%0A        %7D%0D
 %0A%0D%0A        publi

add deser for bytes provided with offset and length