Skip to content

Commit

Permalink
Fixed bug that corrupted UTF-8 encoded dialog.tlks
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Feb 19, 2014
1 parent 1fa0790 commit d4067f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
20 changes: 11 additions & 9 deletions src/infinity/gui/StringEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ else if (version.equals("V3.0")) {
0, 2 * entries_count.getValue());
progress.setMillisToDecideToPopup(100);
for (int i = 0; i < entries_count.getValue(); i++) {
entries[i] = new StringEntry(Filereader.readBytes(bis, entry_size));
entries[i] = new StringEntry(Filereader.readBytes(bis, entry_size), charset);
progress.setProgress(i + 1);
if (progress.isCanceled()) {
entries = null;
Expand All @@ -451,7 +451,7 @@ else if (version.equals("V3.0")) {

RandomAccessFile ranfile = new RandomAccessFile(stringfile, "r");
for (int i = 0; i < entries.length; i++) {
entries[i].readString(ranfile, entries_offset.getValue(), charset);
entries[i].readString(ranfile, entries_offset.getValue());
progress.setProgress(i + 1 + entries_count.getValue());
if (progress.isCanceled()) {
entries = null;
Expand Down Expand Up @@ -562,7 +562,6 @@ private StrRefWriter()
@Override
public void run()
{
Charset charset = StringResource.getCharset();
bexport.setEnabled(false);
bsave.setEnabled(false);
breread.setEnabled(false);
Expand Down Expand Up @@ -613,11 +612,11 @@ public void run()

for (int i = 0; i < entries.length; i++) {
if (entries[i] != null)
entries[i].writeString(bos, charset);
entries[i].writeString(bos);
progress.setProgress(i + 1 + entries_count.getValue());
}
for (int i = 0; i < added_entries.size(); i++) {
added_entries.get(i).writeString(bos, charset);
added_entries.get(i).writeString(bos);
progress.setProgress(entries_count.getValue() + entries.length + i + 1);
}
bos.close();
Expand All @@ -642,15 +641,18 @@ private static final class StringEntry extends AbstractStruct
private int doffset, dlength;
private String string = "";
private byte data[];
private Charset charset;

private StringEntry() throws Exception
{
super(null, null, new byte[entry_size], 0);
this.charset = StringResource.getCharset();
}

StringEntry(byte buffer[]) throws Exception
StringEntry(byte buffer[], Charset charset) throws Exception
{
super(null, null, buffer, 0);
this.charset = (charset != null) ? charset : StringResource.getCharset();
}

@Override
Expand Down Expand Up @@ -693,7 +695,7 @@ else if (version.equals("V3.0")) { // Remember to updateValue writeField() as th
}
}

public void readString(RandomAccessFile ranfile, int baseoffset, Charset charset) throws IOException
public void readString(RandomAccessFile ranfile, int baseoffset) throws IOException
{
ranfile.seek((long)(baseoffset + doffset));
string = Filereader.readString(ranfile, dlength, charset);
Expand All @@ -702,7 +704,7 @@ public void readString(RandomAccessFile ranfile, int baseoffset, Charset charset
public int update(int newoffset)
{
doffset = newoffset;
dlength = string.length();
dlength = string.getBytes(charset).length;
return dlength;
}

Expand Down Expand Up @@ -736,7 +738,7 @@ else if (version.equals("V3.0")) {
}
}

public void writeString(OutputStream os, Charset charset) throws IOException
public void writeString(OutputStream os) throws IOException
{
Filewriter.writeString(os, string, dlength, charset);
}
Expand Down
10 changes: 6 additions & 4 deletions src/infinity/util/Filewriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ public static void writeString(OutputStream os, String s, int length) throws IOE
writeString(os, s, length, Charset.forName("ISO-8859-1")); // For NWN, no other conflicts?
}

public static void writeString(OutputStream os, String s, int length, Charset charset) throws IOException
public static void writeString(OutputStream os, String s, int minLength, Charset charset) throws IOException
{
writeBytes(os, s.getBytes(charset));
byte buffer[] = new byte[length - s.length()];
if (buffer.length != 0)
byte[] stringBytes = s.getBytes(charset);
writeBytes(os, stringBytes);
if (minLength > stringBytes.length) {
byte buffer[] = new byte[minLength - stringBytes.length];
writeBytes(os, buffer);
}
}

private Filewriter(){}
Expand Down

0 comments on commit d4067f0

Please sign in to comment.