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

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » FormatScreenshots 
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  : dibyendu@mazumdar.demon.co.uk
 */

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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.