Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 BOM issue: #3

Open
wants to merge 1 commit into
base: ini4j-0.5.2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion src/main/java/org/ini4j/Ini.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@
import java.io.Writer;

import java.net.URL;
import java.nio.charset.Charset;

public class Ini extends BasicProfile implements Persistable, Configurable
{
// Byte-order mark for UTF8 and UTF16 files
private static final int FIRST_UTF8_BYTE = 0xEF;
private static final int SECOND_UTF8_BYTE = 0xBB;
private static final int THIRD_UTF8_BYTE = 0xBF;

private static final int FIRST_UTF16_BIGENDIAN = 0xFE;
private static final int SECOND_UTF16_BIGENDIAN = 0xFF;

private static final int FIRST_UTF16_LITTLEENDIAN = 0xFF;
private static final int SECOND_UTF16_LITTLEENDIAN = 0xFE;

private static final long serialVersionUID = -6029486578113700585L;

private Config _config;
private File _file;

Expand All @@ -53,7 +66,75 @@ public Ini(Reader input) throws IOException, InvalidFileFormatException
public Ini(InputStream input) throws IOException, InvalidFileFormatException
{
this();
load(input);

boolean resetMustBeDone = false;
final boolean resetIsSupported = input.markSupported();
final Reader originalInputStream = new InputStreamReader( input );

if (resetIsSupported)
{
input.mark( 1024 );
}
final int firstReadByte = input.read();
if ( (firstReadByte == FIRST_UTF16_BIGENDIAN)
|| (firstReadByte == FIRST_UTF16_LITTLEENDIAN)
|| (firstReadByte == FIRST_UTF8_BYTE) )
{
final int secondReadByte = input.read();
if ( (firstReadByte == FIRST_UTF16_BIGENDIAN)
&& (secondReadByte == SECOND_UTF16_BIGENDIAN) )
{
//we have detected UTF16 BE
_config.setFileEncoding( Charset.forName( "UTF-16BE" ) );
}
else if ( (firstReadByte == FIRST_UTF16_LITTLEENDIAN)
&& (secondReadByte == SECOND_UTF16_LITTLEENDIAN) )
{
//we have detected UTF16 LE
_config.setFileEncoding( Charset.forName( "UTF-16LE" ) );
}
else if ( (firstReadByte == FIRST_UTF8_BYTE)
&& (secondReadByte == SECOND_UTF8_BYTE) )
{
final int thirdReadByte = input.read();
if ( (firstReadByte == FIRST_UTF8_BYTE)
&& (secondReadByte == SECOND_UTF8_BYTE)
&& (thirdReadByte == THIRD_UTF8_BYTE) )
{
//we have detected UTF8
_config.setFileEncoding( Charset.forName( "UTF-8" ) );
}
else
{
resetMustBeDone = true;
}
}
else
{
resetMustBeDone = true;
}
}
else
{
resetMustBeDone = true;
}

if (resetMustBeDone)
{
if (resetIsSupported)
{
input.reset();
load(input);
}
else
{
load(originalInputStream);
}
}
else
{
load(input);
}
}

public Ini(URL input) throws IOException, InvalidFileFormatException
Expand Down