Skip to content

Instantly share code, notes, and snippets.

@koduki
Created March 13, 2016 06:35
Show Gist options
  • Save koduki/3bef78601fa0ba5af07b to your computer and use it in GitHub Desktop.
Save koduki/3bef78601fa0ba5af07b to your computer and use it in GitHub Desktop.
Generate CSV data include binary.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cn.orz.pascal.example.csv;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.function.BiConsumer;
/**
* Generate CSV data include binary.
*
* @author koduki
*/
public class TestdataGenerator {
Header header = new Header();
public static void main(String[] args) throws Exception {
TestdataGenerator parser = new TestdataGenerator();
List<LinkedHashMap<String, Colmun>> records = parser.read("input.csv");
parser.write("output.csv", records, 1_000, (LinkedHashMap<String, Colmun> record, Integer index) -> {
record.get("id").set("\"" + index + "\"");
});
}
class Header {
List<String> headers = new ArrayList<>();
int index = 0;
public void add(Colmun col) {
headers.add(col.getAsString());
}
public int size() {
return headers.size();
}
public String next() {
String name = headers.get(index);
index += 1;
return name;
}
public void reset() {
index = 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (String name : headers) {
sb.append('"');
sb.append(name);
sb.append('"');
sb.append(',');
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
}
class Colmun {
private ByteBuffer col;
public Colmun() {
col = ByteBuffer.allocate(256);
}
public void put(byte b) {
col.put(b);
}
public String getAsString() {
String value = new String(this.col.array()).trim();
return value.substring(1, value.length() - 1);
}
public Object get() {
String value = new String(this.col.array());
if (value.charAt(0) == '"') {
return getAsString();
} else {
return Integer.parseInt(value.trim());
}
}
public void set(String value) {
col = ByteBuffer.allocate(256);
col.put(value.getBytes());
}
public byte[] array() {
return this.col.array();
}
}
public void write(String outputFile, List<LinkedHashMap<String, Colmun>> records, int recordLength, BiConsumer<LinkedHashMap<String, Colmun>, Integer> apply) throws FileNotFoundException, IOException {
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
// write header
out.writeBytes(header.toString());
out.write('\r');
out.write('\n');
// write header
int index = 0;
for (int n = 0; n < recordLength; n++) {
LinkedHashMap<String, Colmun> r = records.get(index);
index += 1;
if (index == records.size()) {
index = 0;
}
apply.accept(r, n);
String[] keys = r.keySet().toArray(new String[0]);
for (int i = 0; i < keys.length; i++) {
String name = keys[i];
out.write(r.get(name).array());
if (keys.length != i + 1) {
out.write(',');
}
}
if (recordLength != n + 1) {
out.write('\r');
out.write('\n');
}
}
out.flush();
}
public List<LinkedHashMap<String, Colmun>> read(String fileName) throws FileNotFoundException, IOException {
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
byte[] bs = new byte[4096];
Colmun col = new Colmun();
int readByte;
boolean isHeader = true;
List<LinkedHashMap<String, Colmun>> records = new ArrayList<>();
LinkedHashMap<String, Colmun> record = new LinkedHashMap<>();
while (-1 != (readByte = in.read(bs))) {
for (int i = 0; i < bs.length; i++) {
byte b = bs[i];
char c = (char) b;
if (c == '\r') {
// CRは読み飛ばす.
} else if (c == '\n') {
if (isHeader) {
header.add(col);
isHeader = false;
System.out.println("headear:" + header.size());
} else {
record.put(header.next(), col);
records.add(record);
record = new LinkedHashMap<>();
header.reset();
};
col = new Colmun();
} else if (c == ',') {
if (isHeader) {
header.add(col);
} else {
record.put(header.next(), col);
}
col = new Colmun();
} else if (b == 0) {
record.put(header.next(), col);
records.add(record);
break;
} else {
col.put(b);
}
}
}
return records;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment