Package com.zaranux.client.java.nio

Source Code of com.zaranux.client.java.nio.ByteBuffer

package com.zaranux.client.java.nio;

//import java.util.Vector;


//import com.zaranux.client.util.Log;

import com.zaranux.client.sun.io.ByteToCharUTF8;
import com.zaranux.client.sun.io.CharToByteUTF8;

//import com.zaranux.client.programs.Terminal;

import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.crypto.ByteArrayOutputStream;

public class ByteBuffer {

  //Vector<Byte> buffer = new Vector<Byte>();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
//  Terminal zterminal;
  CharToByteUTF8 ctbu = new CharToByteUTF8();
  ByteToCharUTF8 btcu = new ByteToCharUTF8();
  AsyncCallback<String> outputCallback;
 
  public int getLength()
  {
    return baos.size();
  }
 
  public ByteBuffer(AsyncCallback<String> outputCallback)
  {
    this.outputCallback = outputCallback;
  }
 
  public char removeLast(){
    byte[] ba = baos.toByteArray();
    baos = new ByteArrayOutputStream(); // clear the buffer
    for (int i = 0; i < ba.length-1; i++) {
      put(ba[i]);
    }
    return (char) ba[ba.length-1];
  }
 
  public char getLast() {
    byte[] ba = baos.toByteArray();
    return (char) ba[ba.length-1];
  }
 
  public void flush()
  {
    byte[] ba = baos.toByteArray();
    baos = new ByteArrayOutputStream(); // clear the buffer
   
    if( ba.length <= 0) return;
   

    try
    {
      String r = new String(btcu.convertAll(ba));
      outputCallback.onSuccess(r);
    }catch(com.zaranux.client.java.io.CharConversionException e)
    {
    }
   
   
//    zterminal.output(ne String()
//    StringBuffer sb = new StringBuffer();
//    while(buffer.size() > 1)
//    {
//      sb.append(getChar());
//    }
//    zterminal.output(sb.toString());
  }
 
 
  public void put(byte b)
  {
    baos.write(b);
    //buffer.add(b);
  }
 
/*  public char getChar()
  {
    byte[] ba = new byte[2];
    ba[0] = buffer.remove(0);
    ba[1] = buffer.remove(0);
    return Util.ByteArray2CharUnicode(ba);
  }
  */
 
  public void put(char c)
  {
    char[] ca = new char[1];
    ca[0] = c;
    try{
      byte[] ba =  ctbu.convertAll(ca);
      // byte[] b = Util.Char2ByteArrayUnichode(c);
      for(byte b : ba)
      {
        baos.write(b);
      }
      }catch(com.zaranux.client.java.io.CharConversionException e)
      {
      }
  }
 
  public void putCharAt(int index, char c) {
    //  byte[] ba =  ctbu.convertAll(ca);
      // byte[] b = Util.Char2ByteArrayUnichode(c);
    byte[] ba = baos.toByteArray();
    //Log.debug("put char  "+c+" to "+ toString()+ " with lenght = " + ba.length+" at "+ index);
      if(index < ba.length){
        baos = new ByteArrayOutputStream(); // clear the buffer
        for (int i = 0; i < ba.length; i++) {
          if(i == index)
          {
            put(c);
          }
          put(ba[i]);
          //Log.debug("baos = " + toString());
        }
      }
      else
        put(c);
         
  }
 
  public void put(byte[] b)
  {
    put(b,0,b.length);
  }
  public void put(byte[] b, int off, int len)
  {
    checkArraySize(b.length, off, len);
    for(int i = 0 ; i<len; i++)
    {
      baos.write(b[off + i]);
    }
  }
 
  public int  get(byte[] b, int off, int len)
  {
    checkArraySize(b.length, off, len);
   
    int n = Math.min(len, baos.size() );
   
    if( n == 0return -1;
   
    byte[] ba = baos.toByteArray();
    baos = new ByteArrayOutputStream(); // new stream
   
    for(int i = 0; i<n; i++)
    {
      b[off+i] = ba[i];
    }

    return n;
  }
 
  public int remaining()
  {
    return baos.size();
  }
 
    static void checkArraySize(int size,int off,int len)
    {
        if ((off | len | (off + len) | (size - (off + len))) < 0)
            throw new IndexOutOfBoundsException();     
    }

    /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    return baos.toString();
  }

  public char removeCharAt(int index) {
    //Log.debug("remove char of "+toString()+ " at "+ index);
   
    byte[] ba = baos.toByteArray();
    baos = new ByteArrayOutputStream(); // clear the buffer
    for (int i = 0; i < ba.length; i++) {
      if(i!= index)
      {
        put(ba[i]);
        //Log.debug("baos = " + toString());
      }
    }
    return (char) ba[index];
   
  }
 
  public void clear()
  {
    baos = new ByteArrayOutputStream();
  }
 
  public void put(String s)
  {
    try{
      byte[] ba =  ctbu.convertAll(s.toCharArray());
        baos.write(ba);
    }catch(com.zaranux.client.java.io.CharConversionException e)
      {
      }catch(Exception e)
      {
      }
  }


  public boolean equals(String buf) {
    //Log.debug("buf = "+buf);
    byte[] ba = baos.toByteArray();
    if(ba.length != buf.length())
      return false;
    for (int i = 0; i < ba.length; i++) {
      if((char)ba[i]!= buf.charAt(i))
      {
        return false;
      }
    }
    return true;
  }

 
 
}
TOP

Related Classes of com.zaranux.client.java.nio.ByteBuffer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.