BOMInputStream impl #1

Closed
NiclasThobaben wants to merge 2 commits from feature/io/BOMInputStream into master
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package de.nclazz.commons.io;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
public class ByteOrderInputStream extends InputStream {
/*
1. find BOM
2. store BOM bytes
*/
private List<Byte> bomBytes = new ArrayList<>();
private final InputStream stream;
private ByteOrderMark bom;
public ByteOrderInputStream(InputStream stream) {
this.stream = stream;
}
@Override
public int read(byte b[], int off, int len) throws IOException {
return 0;
}
public ByteOrderMark getBom() {
return this.bom;
}
@Override
public int read() throws IOException {
return this.stream.read();
}
private void findBOM() {
List<ByteOrderMark> availableBOMs = new ArrayList<>(ByteOrderMark.available());
}
}

View File

@ -0,0 +1,44 @@
package de.nclazz.commons.io;
import sun.nio.cs.UTF_16BE;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ByteOrderMark {
public static final ByteOrderMark UTF_8 = new ByteOrderMark(StandardCharsets.UTF_8, 0xEF, 0xBB, 0xBF);
public static final ByteOrderMark UTF_16BE = new ByteOrderMark(StandardCharsets.UTF_16BE,0xFE, 0xFF);
public static final ByteOrderMark UTF_16LE = new ByteOrderMark(StandardCharsets.UTF_16LE,0xFF, 0xFE);
public static final ByteOrderMark UTF_32BE = new ByteOrderMark(Charset.forName("UTF-32BE"),0x00, 0x00, 0xFE, 0xFF);
public static final ByteOrderMark UTF_32LE = new ByteOrderMark(Charset.forName("UTF-32LE"),0xFF, 0xFE, 0x00, 0x00);
private Charset charset;
private final int[] bytes;
public ByteOrderMark(Charset charset, int...bytes) {
this.charset = charset;
this.bytes = bytes;
}
public int length() {
return this.bytes.length;
}
public int[] getBytes() {
return Arrays.copyOf(this.bytes, this.bytes.length);
}
public Charset getCharset() {
return this.charset;
}
public static List<ByteOrderMark> available() {
return Arrays.asList(UTF_8, UTF_16BE, UTF_16LE, UTF_32BE, UTF_32LE);
}
}