package org.loon.simple.slg.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
*
* Copyright 2008
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* @project loonframework
* @author chenpeng
* @email:
[email protected]
* @version 0.1
*/
public class Keep {
private static final long serialVersionUID = -1982090153295778606L;
final static public String LS = System.getProperty( "line.separator", "\n");
final static public String FS = System.getProperty( "file.separator", "\\");
/**
* 增位变更指定字符串(混淆记录用)
*
* @param s
* @param x
* @return
*/
final static public String addChange( final String s, final int x) {
String result = null;
StringBuilder sbr = new StringBuilder();
for ( int i = 0; i < s.length(); i++) {
char p = ( char) (s.charAt(i) + x);
sbr.append(p);
}
try {
result = URLEncoder.encode(sbr.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return result;
}
/**
* 减位变更指定字符串(混淆记录用)
*
* @param s
* @param x
* @return
*/
final static public String backChange( final String s, final int x) {
String result = null;
try {
result = URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
StringBuilder sbr = new StringBuilder();
for ( int i = 0; i < result.length(); i++) {
char p = ( char) (result.charAt(i) - x);
sbr.append(p);
}
return sbr.toString();
}
/**
* 压缩byte[]
*
* @param buffer
* @return
*/
public static byte[] compress( final byte[] buffer) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipos = new GZIPOutputStream(baos);
gzipos.write(buffer);
gzipos.flush();
gzipos.close();
return baos.toByteArray();
} catch (IOException e) {
return null;
}
}
/**
* 压缩byte[]
*
* @param buffer
* @return
*/
public static byte[] uncompress( final byte[] buffer) {
try {
GZIPInputStream gzipis = new GZIPInputStream(
new ByteArrayInputStream(buffer));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] tmp = new byte[8192];
int len;
while ((len = gzipis.read(tmp)) > 0) {
baos.write(tmp, 0, len);
}
baos.flush();
return baos.toByteArray();
} catch (IOException e) {
return null;
}
}
/**
* 保存游戏记录
*
* @param file
* @param bytes
* @throws IOException
*/
public static void save( final String fileName, final String message) throws IOException {
save( new File(fileName), new ByteArrayInputStream(Keep.compress(message.getBytes())));
}
/**
* 保存记录到文件
*
* @param file
* @param input
* @throws IOException
*/
private static void save( final File file, final InputStream input)
throws IOException {
mkdirs(file);
BufferedOutputStream output = null;
try {
int contentLength = input.available();
output = new BufferedOutputStream(
new FileOutputStream(file, false));
while (contentLength-- > 0) {
output.write(input.read());
}
} finally {
close(input, file);
close(output, file);
}
}
final private static byte[] read( final InputStream inputStream) {
byte[] arrayByte = null;
BufferedInputStream buffer = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
try {
int read;
while ((read = buffer.read(bytes)) >= 0) {
byteArrayOutputStream.write(bytes, 0, read);
}
arrayByte = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (buffer != null) {
buffer.close();
buffer = null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return Keep.uncompress(arrayByte);
}
/**
* 读取记录到list
*
* @param fileName
* @return
* @throws IOException
*/
public static List load( final String fileName) throws IOException {
File file = new File(fileName);
BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(Keep.read( new FileInputStream(file)))));
List records = new ArrayList();
String record = null;
try {
while ((record = reader.readLine()) != null) {
records.add(record);
}
} finally {
close(reader, file);
}
return records;
}
/**
* 创建文件夹
*
* @param file
* @throws IOException
*/
private static void mkdirs( final File file) throws IOException {
checkFile(file);
File parentFile = file.getParentFile();
if (parentFile != null) {
if (!parentFile.exists() && !parentFile.mkdirs()) {
throw new IOException( "Creating directories "
+ parentFile.getPath() + " failed.");
}
}
}
private static void checkFile( final File file) throws IOException {
if (file.exists() && !file.isFile()) {
throw new IOException( "File " + file.getPath()
+ " is actually not a file.");
}
}
private static void close( final InputStream input, final File file) {
if (input != null) {
try {
input.close();
} catch (IOException e) {
closingFailed(file, e);
}
}
}
private static void close( final OutputStream output, final File file) {
if (output != null) {
try {
output.close();
} catch (IOException e) {
closingFailed(file, e);
}
}
}
private static void close( final Reader reader, final File file) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
closingFailed(file, e);
}
}
}
private static void closingFailed( final File file, final IOException e) {
String message = "Closing file " + file.getPath() + " failed.";
throw new RuntimeException(message + ":" + e.getMessage());
}
}