Char Buffer : Buffering « File Input Output « 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 » File Input Output » Buffering 




Char Buffer
   

/*
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

/**
 * Fast StringBuffer replacement that allows direct access to the underlying
 * char[]. This is based com.sosnoski.util.array.CharArray from
 * Sosnoski Software Solutions, Inc.
 */
public final class CharBuf {

    private char[] buf;
    private int size;

    public CharBuf() {
        this(64);
    }

    public CharBuf(int capacity) {
        buf = new char[capacity];
    }

    public CharBuf(String s) {
        this(s.length());
        append(s);
    }

    public CharBuf(CharBuf s) {
        this(s.size);
        size = s.size;
        System.arraycopy(s.buf, 0, buf, 0, size);
    }

    public int size() {
        return size;
    }

    public void clear() {
        size = 0;
    }

    private void ensureCapacity(int len) {
        if (size + len > buf.length) {
            int n = buf.length * 1;
            if (size + len > n) {
                n = size + len;
            }
            char[] a = new char[n];
            System.arraycopy(buf, 0, a, 0, size);
            buf = a;
        }
    }

    /**
     * Append ch and return its index.
     */
    public int append(char ch) {
    ensureCapacity(size + 1);
    int start = size;
        buf[size++= ch;
    return start;
  }

    /**
     * Append i and return its index.
     */
    public int append(int i) {
        return append(Integer.toString(i));
    }

    /**
     * Append s and return its index.
     */
    public int append(String s) {
        return append(s.toCharArray());
  }

    /**
     * Append a and return its index.
     */
    public int append(char[] a) {
        int n = a.length;
        ensureCapacity(size + n);
    int start = size;
        System.arraycopy(a, 0, buf, size, n);
        size += n;
    return start;
  }

    /**
     * Replace characters from i onwards with supplied characters. This does
     * not do any error checking or make any attempt to expand the buffer
     * for performance reasons.
     */
    public void replace(int i, char[] text) {
        System.arraycopy(text, 0, buf, i, text.length);
    }

    /**
     * Replace characters from first to last - 1 with c. This does
     * not do any error checking for performance reasons.
     */
    public void replace(int first, int last, char c) {
        for (int i = first; i < last; i++) {
            buf[i= c;
        }
    }

    public String toString() {
        return new String(buf, 0, size);
    }

    /**
     * Constructs and returns a simple array containing the same data as held
     * in a portion of this growable array.
     */
    public char[] toArray(int offset, int length) {
        char[] a = new char[length];
        System.arraycopy(buf, offset, a, 0, length);
        return a;
    }

    public void setSize(int sz) {
        size = sz;
    }

    /**
     * Construct a <code>String</code> from a portion of the character sequence
     * present.
     */
    public String toString(int offset, int length) {
        return new String(buf, offset, length);
    }

    /**
     * Insert the characters from a <code>char[]</code> into the array.
     */
    public void insert(int offset, char[] text) {
        adjust(offset, offset, text.length);
        System.arraycopy(text, 0, buf, offset, text.length);
    }

    /**
     * Insert the characters from a <code>String</code> into the array.
     */
    public void insert(int offset, String text) {
        adjust(offset, offset, text.length());
        text.getChars(0, text.length(), buf, offset);
    }

    /**
     * Replace a character range in the array with the characters from a
     * <code>String</code>.
     */
    public void replace(int from, int to, String text) {
        adjust(from, to, text.length());
        text.getChars(0, text.length(), buf, from);
    }

    /**
     * Replace a character range in the array with the characters from a
     * <code>char[]</code>.
     */
    public void replace(int from, int to, char[] text) {
        adjust(from, to, text.length);
        System.arraycopy(text, 0, buf, from, text.length);
    }

    /**
     * Adjust the characters in the array to make room for an insertion or
     * replacement. Depending on the relative sizes of the range being
     * replaced and the range being inserted, this may move characters past
     * the start of the replacement range up or down in the array.
     *
     @param from index number of first character to be replaced
     @param to index number past last character to be replaced
     @param length length of character range being inserted
     */
    protected void adjust(int from, int to, int length) {
        if (from >= && to < size && from <= to) {
            int change = from + length - to;
            if (change > 0) {
                ensureCapacity(size + change);
            }
            if (to < size){
                System.arraycopy(buf, to, buf, to + change, size - to);
                size += change;
            }
        else {
            throw new ArrayIndexOutOfBoundsException("Invalid remove range");
        }
    }

    /**
     * Set the value at an index position in the array.
     */
    public void set(int index, char value) {
        buf[index= value;
    }

    /**
     * Remove a range of value from the array. The index positions for values
     * above the range removed are decreased by the number of values removed.
     *
     @param from index number of first value to be removed
     @param to index number past last value to be removed
     */
    public void remove(int from, int to) {
        if (from >= && to <= size && from <= to) {
            if (to < size){
                int change = from - to;
                System.arraycopy(buf, to, buf, from, size - to);
                size += change;
            }
        else {
            throw new ArrayIndexOutOfBoundsException("Invalid remove range");
        }
    }

}

   
    
    
  














Related examples in the same category
1.Endian differences and data storageEndian differences and data storage
2.View BuffersView Buffers
3.Using BuffersUsing Buffers
4.Output BufferingOutput Buffering
5.Getting different representations from a ByteBufferGetting different representations from a ByteBuffer
6.Manipulating ints in a ByteBuffer with an IntBufferManipulating ints in a ByteBuffer with an IntBuffer
7.Circular Byte Buffer
8.Circular Char Buffer from http://ostermiller.org
9.Buffered copying
10.Byte Buffer
11.Circular Char Buffer
12.Caching InputStream
13.Caching OutputStream
14.ByteBuffer FloatBuffer demo
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.