How do I convert a byte[] array to a CharSequence?

You need to write a class AsciiCharSequence that implements storing a sequence of ASCII characters in an array of bytes. Compared to the String class storing each character as a char, AsciiCharSequence will take up less memory.

The AsciiCharSequence class must:

  • Implement the interface java.lang.CharSequence;

  • Have a constructor that accepts an array of bytes;

  • Define methods length(), charAt(), subSequence() and toString()

Well I'm almost there wrote:

public class AsciiCharSequence implements CharSequence {

     byte[] a1;

  public AsciiCharSequence(byte[] a){  //Конструктор класса
        a = this.a1;
    }

    @Override
    public String toString() {    //Переопределение toString()
        return "AsciiCharSequence{" +
                "a1=" + Arrays.toString(a1) +
                '}';
    }

    @Override
    public int length(){
        return a1.length;
    }; //Переопределение length()

    @Override
    public char charAt(int index){      //Переопределение charAt()
        char t000 = (char)a1[index];
        return t000;
    }

    @Override
    public CharSequence subSequence(int start, int end){  //Переопределение subSequence()
        CharSequence buf[] = new CharSequence[end - start];

       for(int i=start; i<=end;i++)
       {
           byte t = a1[i-start];
          buf[i]=(CharSequence)t;
       }
         return buf[end -start];
    }
}

Most problematic area = Redefinition subSequence(). These are the last 12 lines from the end. The problem is this: I can't write the code so that the method returns the correct type (CharSequence?). If the algorithm for forming an array is more or less clear, then the transformation of the array byte[] to CharSequence is not at all clear. The method itself should be applied to an array of characters and return an array of characters with indexes ranging from start to end. For example,

String s0 = "Вова и Дима козлы";
   s0.subSequence(14,16) 

Must return "evil".

Author: Tagir Valeev, 2015-10-09

3 answers

Just create a new instance of your type:

@Override
public CharSequence subSequence(int start, int end){  //Переопределение subSequence()
    return new AsciiCharSequence(Arrays.copyOfRange(a1, start, end));
}

Note that you redefined toString() incorrectly. The documentation clearly says:

String toString()

Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence.

That is, if you redefine the CharSequence interface, you must return to toString() a string that matches the content of your CharSequence. This is easy to do:

@Override
public String toString() {    //Переопределение toString()
    return new String(a1, java.nio.charset.StandardCharsets.ISO_8859_1);
}
 8
Author: Tagir Valeev, 2020-06-12 12:52:24

You can rewrite the class in this form:

public class AsciiCharSequence implements CharSequence {
    private byte[] data;

    public AsciiCharSequence(byte[] data) {
        this.data = data;
    }

    @Override
    public int length() {
        return data.length;
    }

    @Override
    public char charAt(int index) {
        return (char) (data[index] & 0xff);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        int length = end - start;
        byte[] bytes = new byte[length];
        for (int i = 0, j = start; i < length; i++, j++) {
            bytes[i] = data[j];
        }
        return new AsciiCharSequence(bytes);
    }

    @Override
    public String toString() {
        return new String(data);
    }
}

Pulled from the github. Author - Mikhail Valeyko.

 3
Author: Andrew Kachalin, 2015-10-09 13:05:29
class AsciiCharSequence implements CharSequence {
    private byte[] byteSequence;

    public AsciiCharSequence(byte[] bytesSequence) {
        this.byteSequence = bytesSequence;
    }

    @Override
    public int length() {
        return byteSequence.length;
    }

    @Override
    public char charAt(int index) {
        return (char) byteSequence[index];
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return new AsciiCharSequence(java.util.Arrays.copyOfRange(byteSequence, start, end));
    }

    @Override
    public String toString() {
        return new String(byteSequence);
    }
}
 1
Author: Dmitry Novikov, 2020-03-24 05:09:15