package com.eactive.eai.util;

import com.eactive.eai.adapter.socket2.protocol.ByteMessage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ByteCharSequence implements CharSequence{
	private final byte[] data;
	private final int length;
	private final int offset;
	
	public ByteCharSequence(byte[] data){
		this(data, 0, data.length);
	}
	public ByteCharSequence(byte[] data, int offset, int length){
		this.data = data;
		this.offset = offset;
		this.length = length;
	}
	
	@Override
	public int length() {
		return this.length;
	}
	
	@Override
	public char charAt(int index) {
		return (char)(data[offset + index] & 0xff);
	}

	@Override
	public CharSequence subSequence(int start, int end) {
		return new ByteCharSequence(data, offset + start, end - start);
	}
	
	public static void main(String[] args) throws Exception{
		ByteMessage b = new ByteMessage();
		b.append((byte)0xD4);
		b.append((byte)0xD6);		
		b.append((byte)0xC3);		
		b.append((byte)0xF5);		
		b.append((byte)0xF0);		
		b.append((byte)0xF9);		
		b.append((byte)0xF4);		
		b.append((byte)0x40);		
		b.append((byte)0x40);		
		b.append("0000HDRISO12345678901234567890123456789012345678901234567".getBytes());
		
		Matcher m = Pattern.compile("[0-9]{4}HDR").matcher(new ByteCharSequence(b.getBytes()));
		int index =0;
		if (m.find()){
			index = m.start();
		}
		// System.out.println("index = " + index);
	}
}
