001 package com.croftsoft.core.io;
002
003 import java.io.*;
004
005 import com.croftsoft.core.lang.NullArgumentException;
006
007 /*********************************************************************
008 * A String Encoder and Parser.
009 *
010 * @version
011 * 2003-06-02
012 * @since
013 * 2003-06-01
014 * @author
015 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
016 *********************************************************************/
017
018 public final class StringCoder
019 implements Encoder, Parser
020 //////////////////////////////////////////////////////////////////////
021 //////////////////////////////////////////////////////////////////////
022 {
023
024 public static final String US_ASCII = "US-ASCII";
025
026 public static final String UTF_8 = "UTF-8";
027
028 //
029
030 private final String charSetName;
031
032 //////////////////////////////////////////////////////////////////////
033 //////////////////////////////////////////////////////////////////////
034
035 public StringCoder ( String charSetName )
036 //////////////////////////////////////////////////////////////////////
037 {
038 NullArgumentException.check ( this.charSetName = charSetName );
039 }
040
041 //////////////////////////////////////////////////////////////////////
042 //////////////////////////////////////////////////////////////////////
043
044 public byte [ ] encode ( Object object )
045 throws IOException
046 //////////////////////////////////////////////////////////////////////
047 {
048 return object.toString ( ).getBytes ( charSetName );
049 }
050
051 public Object parse (
052 InputStream inputStream,
053 int contentLength )
054 throws IOException
055 //////////////////////////////////////////////////////////////////////
056 {
057 return StreamLib.toString ( inputStream, charSetName );
058 }
059
060 //////////////////////////////////////////////////////////////////////
061 //////////////////////////////////////////////////////////////////////
062 }