package com.wizzardo.http.utils;

import com.wizzardo.tools.reflection.StringReflection;

/**
 * Created by wizzardo on 29.12.14.
 */
public class AsciiReader {

    public static String read(byte[] bytes) {
        return read(bytes, 0, bytes.length);
    }

    public static String read(byte[] bytes, int offset, int length) {
        if (length <= 0)
            return new String();

        int h = 0;
        int k;
        char[] data = new char[length];
        for (int i = 0; i < length; i++) {
            data[i] = (char) (k = (bytes[offset + i] & 0xff));
            h = 31 * h + k;
        }

        return StringReflection.createString(data, h);
    }

    public static String read(byte[] bytes, int offset, int length, int hash) {
//            return read(bytes, offset, length);
        if (length == 0)
            return new String();

        char[] data = new char[length];
        for (int i = 0; i < length; i++) {
            data[i] = (char) (bytes[offset + i] & 0xff);
        }

        return StringReflection.createString(data, hash);
    }

    public static String read(byte[] buffer, int bufferLength, byte[] bytes, int offset, int length) {
        char[] data = new char[bufferLength + length];

        int h = 0;
        int k;
        for (int i = 0; i < bufferLength; i++) {
            data[i] = (char) (k = (buffer[i] & 0xff));
            h = 31 * h + k;
        }
        for (int i = 0; i < length; i++) {
            data[i + bufferLength] = (char) (k = (bytes[offset + i] & 0xff));
            h = 31 * h + k;
        }
        return StringReflection.createString(data, h);
    }

    public static byte[] write(String s) {
        byte[] bytes = new byte[s.length()];
        char[] chars = StringReflection.chars(s);
        int l = chars.length;
        for (int i = 0; i < l; i++) {
            bytes[i] = (byte) chars[i];
        }
        return bytes;
    }
}

@@ -1712,16 +1712,139 @@
 gth()%5D;%0A
+        write(s, bytes, 0);%0A        return bytes;%0A    %7D%0A%0A    public static int write(String s, byte%5B%5D bytes, int offset) %7B%0A
         
@@ -1913,16 +1913,25 @@
 s.length
+ + offset
 ;%0A      
@@ -1941,25 +1941,30 @@
 or (int i = 
-0
+offset
 ; i %3C l; i++
@@ -2004,16 +2004,25 @@
  chars%5Bi
+ - offset
 %5D;%0A     
@@ -2033,31 +2033,27 @@
 %0A        return 
-bytes
+l
 ;%0A    %7D%0A%7D%0A

add write(String, bytes, offset)