All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclarion.clarion.util.SharedWriter Maven / Gradle / Ivy

package org.jclarion.clarion.util;

import java.io.Writer;

import javax.swing.text.Segment;

public class SharedWriter extends Writer implements CharSequence
{
    private int pos;
    private char[] buffer;
    
    public SharedWriter()
    {
        buffer=new char[128];
    }
    
    public void reset()
    {
        pos=0;
    }
    
    public int getSize()
    {
        return pos;
    }
    
    public char[] getBuffer()
    {
        return buffer;
    }

    @Override
    public Writer append(char c)  
    {
        extendBuffer(1);
        buffer[pos++]=c;
        return this;
    }

    @Override
    public Writer append(CharSequence csq, int start, int end) {
        extendBuffer(end-start);
        for (int scan=start;scan0) pos--;
    }
    
    public boolean endsWith(char test[])
    {
        int t_scan = pos-test.length;
        if (t_scan<0) return false;
        for (char t : test ) {
            if (t!=buffer[t_scan++]) return false;
        }
        return true;
    }

    public void shrink(char test[])
    {
        int scan=test.length;
        while (pos>0 && scan>0) {
            scan--;
            if (buffer[pos-1]!=test[scan]) return;
            pos--;
        }
    }
    
    public boolean endsWith(byte test)
    {
        if (pos==0) return false;
        return buffer[pos-1]==test;
    }

    public void skip(long ofs)
    {
        pos+=ofs;
    }
        
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy