package com.download;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
/**
* function1: 生成测试数据 10个5M文件
* function2: 启动线程,读取文件,如果缓存块超过10M,则生成落地文件
*
*/
public class Naming {
static ByteBuffer cache = ByteBuffer.allocate(10 * 1024 * 1024);
private static final Object object = new Object();
public static void main(String[] args) {
// 生成测试数据
// String test = "E:\\tmp\\offset\\test";
//
// byte[] b = new byte[5 * 1024 * 1024];
// BufferedOutputStream bos = null;
// for (int i = 0; i < 10; i++) {
// try {
// String name = System.currentTimeMillis() + "-" + i;
// bos = new BufferedOutputStream(new FileOutputStream(new File(test + "/" + name + ".txt")));
// Arrays.fill(b, (byte)i);
// bos.write(b);
// bos.flush();
// bos.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
String input = "E:\\tmp\\offset\\input";
String output = "E:\\tmp\\offset";
Thread inputThread = new Thread(new parseInput(input), "inputThread");
Thread outputThread = new Thread(new output(output), "outputThread");
inputThread.start();
outputThread.start();
}
public static class parseInput implements Runnable {
private String input;
public parseInput(String input) {
this.input = input;
}
@Override
public void run() {
System.out.println("input Thread is running...");
while (true) {
if (new File(input).list().length != 0) {
for (File file : new File(input).listFiles()) {
if (file.getName().endsWith("txt")) {
System.out.println("开始解析文件" + file.getName());
try {
RandomAccessFile accessFile = new RandomAccessFile(file, "r");
FileChannel channel = accessFile.getChannel();
long currentPosition = 0L;
int readLength = channel.read(cache, currentPosition);
while (readLength != -1) {
currentPosition += readLength;
if (!cache.hasRemaining()) {
synchronized(object) {
object.wait();
}
}
readLength = channel.read(cache, currentPosition);
}
channel.close();
accessFile.close();
file.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static class output implements Runnable {
private String output;
public output(String output) {
this.output = output;
}
@Override
public void run() {
System.out.println("output Thread is running...");
while (true) {
if (!cache.hasRemaining()) { //缓存没有剩余空间时,开始读取数据
System.out.println("缓存块已满,开始序列化到硬盘");
cache.flip();
long blockid = System.currentTimeMillis();
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(output + "/" + blockid)));
bos.write(cache.array());
cache.clear();
synchronized (object) {
object.notifyAll();
}
System.out.println("序列化结束, 文件名为" + blockid);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}