package com.eactive.eai.adapter.socket2.common;

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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class FileUtil {
	private FileUtil() {
		
	}
	public static byte[] readFile(String filename) throws IOException {
		
		try (
			BufferedReader br = new BufferedReader(new FileReader(filename));
		) {
			String line = "";
			ByteMessage m = new ByteMessage();
			while ((line = br.readLine()) != null) {	
				while(line.indexOf("0x") != -1) {
					m.append(line.substring(0, line.indexOf("0x")));
					String hex = line.substring(line.indexOf("0x"), line.indexOf("0x")+4);	
					m.append((byte) Integer.parseInt(hex.substring(2), 16));
					line = line.substring(line.indexOf("0x")+4);				
				}
				m.append(line);
			}
			return m.getBytes();
		}
	}
}
