package com.mindprod.example;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import static java.lang.System.*;
/**
* Experiments with Byte Order Marks.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2016-07-18 initial version
* @since 2016-07-18
*/
public class BOMExperiments
{
/**
* a byte order mark
*/
static final char BOM = ( char ) 0xfeff;
/**
* dump chars just read in hex
*
* @param charsRead count of chars read
* @param ca array of chars read
*/
private static void dumpCa( int charsRead, char[] ca )
{
out.println( charsRead
+ " : "
+ Integer.toHexString( ca[ 0 ] )
+ ", "
+ Integer.toHexString( ca[ 1 ] )
+ ", "
+ Integer.toHexString( ca[ 2 ] )
+ ", "
+ Integer.toHexString( ca[ 3 ] ) );
}
/**
* write and read with various combination of encoding and BOM
*
* @param args not used
*/
public static void main( final String[] args )
{
try
{
{
final FileOutputStream fos = new FileOutputStream( "C:/temp/utf8.txt", false );
final OutputStreamWriter eosw = new OutputStreamWriter( fos, "UTF-8" );
final PrintWriter prw = new PrintWriter( eosw, false );
prw.write( BOM );
prw.write( "platypus" );
prw.close();
}
{
final FileOutputStream fos = new FileOutputStream( "C:/temp/utf16be.txt", false );
final OutputStreamWriter eosw = new OutputStreamWriter( fos, "UTF-16BE" );
final PrintWriter prw = new PrintWriter( eosw, false );
prw.write( BOM );
prw.write( "platypus" );
prw.close();
}
{
final FileOutputStream fos = new FileOutputStream( "C:/temp/utf16le.txt", false );
final OutputStreamWriter eosw = new OutputStreamWriter( fos, "UTF-16LE" );
final PrintWriter prw = new PrintWriter( eosw, false );
prw.write( BOM );
prw.write( "platypus" );
prw.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf8.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-8" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16be.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16BE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16le.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16LE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16be.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-8" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16le.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-8" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf8.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16BE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16le.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16BE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf8.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16LE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
{
final FileInputStream fis = new FileInputStream( "C:/temp/utf16be.txt" );
final InputStreamReader eisr = new InputStreamReader( fis, "UTF-16LE" );
final char[] ca = new char[ 9 ];
final int charsRead = eisr.read( ca );
dumpCa( charsRead, ca );
eisr.close();
}
}
catch ( IOException e )
{
err.println( e.getMessage() );
}
}
}