A format for String objects that is easier to persist : Format « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Format 




A format for String objects that is easier to persist
 
/***
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *    Project: www.simpledbm.org
 *    Author : Dibyendu Majumdar
 *    Email  : [email protected]
 */

import java.nio.ByteBuffer;

/**
 * A format for String objects that is easier to persist. 
 
 @author Dibyendu Majumdar
 @since 26-Jun-2005
 */
public final class ByteString implements Comparable<ByteString> {
    
    private byte[] bytes;
    
    public ByteString() {
    }

    public ByteString(String s) {
        bytes = s.getBytes();
    }
    
    public ByteString(byte[] bytes) {
        this.bytes = bytes.clone();
    }
    
    @Override
  public String toString() {
        if (bytes == null)
            return "";
        return new String(bytes);
    }
    
    public int getStoredLength() {
        return bytes.length + (Short.SIZE / Byte.SIZE);
    }
    
    public void store(ByteBuffer bb) {
        short n = 0;
        if (bytes != null) {
            n = (shortbytes.length;
        }
        bb.putShort(n);
        if (n > 0) {
            bb.put(bytes, 0, n);
        }
    }
    
    public void retrieve(ByteBuffer bb) {
        short n = bb.getShort();
        if (n > 0) {
            bytes = new byte[n];
            bb.get(bytes);
        }
        else {
            bytes = null;
        }
    }

  public int compareTo(ByteString o) {
    /*
     * FIXME: This is inefficient 
     */
    String s1 = new String(bytes);
    String s2 = new String(o.bytes);
    return s1.compareTo(s2);
  }
  
  public int length() {
    return bytes.length;
  }
  
  public byte get(int offset) {
    return bytes[offset];
  }
}

   
  














Related examples in the same category
1.String Align Demo: extends FormatString Align Demo: extends Format
2.Format SizeFormat Size
3.Get Percent Value
4.Int format and conversion
5.The # symbol shows a digit or nothing if no digit present
6.Make custom Input Text Formatter in Java
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.