package decorator2;
/**
* @Created: 2024/09/24 11:31
* @Description: 定义了读取和写入操作的通用数据接口
*/
public interface DataSource {
void writeData(String data);
String readData();
}
package decorator2;
import java.io.*;
/**
* @Created: 2024/09/24 11:35
* @Description: 简单数据读写器
*/
public class FileDataSource implements DataSource {
private String name;
public FileDataSource(String name) {
this.name = name;
}
@Override
public void writeData(String data) {
File file = new File(name);
try (OutputStream fos = new FileOutputStream(file)) {
fos.write(data.getBytes(), 0, data.length());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
@Override
public String readData() {
char[] buffer = null;
File file = new File(name);
try (FileReader reader = new FileReader(file)) {
buffer = new char[(int) file.length()];
reader.read(buffer);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return new String(buffer);
}
}
package decorator2;
/**
* @Created: 2024/09/24 14:14
* @Description: 抽象基础装饰
*/
public abstract class DataSourceDecorator implements DataSource {
private DataSource wrapper;
public DataSourceDecorator(DataSource dataSource) {
this.wrapper = dataSource;
}
@Override
public void writeData(String name) {
wrapper.writeData(name);
}
@Override
public String readData() {
return wrapper.readData();
}
}
package decorator2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
/**
* @Created: 2024/09/24 14:27
* @Description: 压缩装饰
*/
public class CompressionDecorator extends DataSourceDecorator {
private int compLevel = 6;
public CompressionDecorator(DataSource dataSource) {
super(dataSource);
}
public int getCompressionLevel() {
return compLevel;
}
public void setCompressionLevel(int value) {
compLevel = value;
}
@Override
public void writeData(String data) {
super.writeData(compress(data));
}
@Override
public String readData() {
return decompress(super.readData());
}
private String compress(String stringData) {
byte[] data = stringData.getBytes();
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
DeflaterOutputStream dos = new DeflaterOutputStream(bout, new Deflater(compLevel));
dos.write(data);
dos.close();
bout.close();
return Base64.getEncoder().encodeToString(bout.toByteArray());
} catch (IOException ex) {
return null;
}
}
private String decompress(String stringData) {
byte[] data = Base64.getDecoder().decode(stringData);
try {
InputStream in = new ByteArrayInputStream(data);
InflaterInputStream iin = new InflaterInputStream(in);
ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
int b;
while ((b = iin.read()) != -1) {
bout.write(b);
}
in.close();
iin.close();
bout.close();
return new String(bout.toByteArray());
} catch (IOException ex) {
return null;
}
}
}
package decorator2;
import java.util.Base64;
/**
* @Created: 2024/09/24 14:26
* @Description: 加密装饰
*/
public class EncryptionDecorator extends DataSourceDecorator{
public EncryptionDecorator(DataSource dataSource) {
super(dataSource);
}
@Override
public void writeData(String data) {
super.writeData(encode(data));
}
@Override
public String readData() {
return decode(super.readData());
}
private String encode(String data) {
byte[] result = data.getBytes();
for (int i = 0; i < result.length; i++) {
result[i] += (byte) 1;
}
return Base64.getEncoder().encodeToString(result);
}
private String decode(String data) {
byte[] result = Base64.getDecoder().decode(data);
for (int i = 0; i < result.length; i++) {
result[i] -= (byte) 1;
}
return new String(result);
}
}
package decorator2;
/**
* @Created: 2024/09/24 14:29
* @Description:
*/
public class Demo {
public static void main(String[] args) {
String salaryRecords = "Name,Salary\nJohn Smith,100000\nSteven Jobs,912000";
FileDataSource fileDataSource = new FileDataSource("out/OutputDemo.txt");
EncryptionDecorator encryptionDecorator = new EncryptionDecorator(fileDataSource);
encryptionDecorator.writeData(salaryRecords);
DataSourceDecorator encoded0 = new CompressionDecorator(fileDataSource);
encoded0.writeData(salaryRecords);
DataSourceDecorator encoded = new CompressionDecorator(encryptionDecorator);
encoded.writeData(salaryRecords);
DataSource plain = new FileDataSource("out/OutputDemo.txt");
System.out.println("- Input ----------------");
System.out.println(salaryRecords);
System.out.println("- Encoded --------------");
System.out.println(plain.readData());
System.out.println("- Decoded --------------");
System.out.println(encoded.readData());
}
}