package CoreFS.SD03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import CoreFS.SA01.CoreException;
import CoreFS.SA01.CoreGlobalConstants;
import CoreFS.SA06.CoreReturnObject;

public class CoreLogFileReader {
	
	public static StringBuffer pageContents = new StringBuffer();

	public static CoreReturnObject readLog(String byDateTime,String LogName) {
		CoreReturnObject cro = new CoreReturnObject();
		try {
			String systempath=System.getProperty("catalina.home");
			String path="";
			if(byDateTime.equals(""))
			{
				path=systempath+"\\logs\\"+LogName+ ".log";
				//path = CoreGlobalConstants.logpath +LogName+ ".log";
			}else
			{
				path=systempath+"\\logs\\"+LogName+ ".log"+byDateTime;
			    //path = CoreGlobalConstants.logpath + LogName+".log."+byDateTime;
			}
			InputStream is = new FileInputStream(path);			
			
			byte[] buffer = new byte[is.available()];
			pageContents.setLength(0);
			pageContents.append(new String(buffer, 0, is.read(buffer)));			
			is.close();
			cro.setResult(pageContents.toString());
		} catch (Exception e) {
			cro = cro.SetErrInfo(e.getMessage());
			e.printStackTrace();
		}
		return cro;
	}
	public static CoreReturnObject readLog(String byDateTime, String LogName,
			Integer beg) {
		CoreReturnObject cro = new CoreReturnObject();
		try {
			String systempath = System.getProperty("catalina.home");
			String path = "";
			if (byDateTime.equals("")) {
				path = systempath + "\\logs\\" + LogName + ".log";
				// path = CoreGlobalConstants.logpath +LogName+ ".log";
			} else {
				path = systempath + "\\logs\\" + LogName + ".log" + byDateTime;
				// path = CoreGlobalConstants.logpath +
				// LogName+".log."+byDateTime;
			}

			final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M

			File f = new File(path);	
			
			int num = (int) f.length() / 20480 + 1;			
			int begin = (int) f.length() * beg / num;
			// if(be==num-1)
			// {
			// begin=begin+1;
			// }

			MappedByteBuffer inputBuffer = new RandomAccessFile(f, "rw")
					.getChannel().map(FileChannel.MapMode.READ_ONLY, begin,
							f.length() / num);

			byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容

			long start = System.currentTimeMillis();

			for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {

				if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {

					for (int i = 0; i < BUFFER_SIZE; i++)

						dst[i] = inputBuffer.get(offset + i);

				} else {

					for (int i = 0; i < inputBuffer.capacity() - offset; i++)

						dst[i] = inputBuffer.get(offset + i);

				}

				int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
						: inputBuffer.capacity() % BUFFER_SIZE;

				pageContents.setLength(0);				
				pageContents.append(new String(dst, 0, length));
				// System.out.println(new String(dst, 0, length));// new
				// String(dst,0,length)这样可以取出缓存保存的字符串，可以对其进行操作

			}
			
			cro.setResult(pageContents.toString());

		} catch (Exception ex) {
			cro = cro.SetErrInfo(ex.getMessage());
			ex.printStackTrace();
		}
		return cro;
	}
}
