Skip to content

Commit 53cb8e4

Browse files
author
jossonsmith
committed
Fixed packing system for the bug:
"I've installed a windows to test my works on j2s. But, setting the encoding of eclipse to UTF-8, I've got wrong string generation from j2s on windows. " Now always output JavaScript in UTF-8 with extra EF BB BF header.
1 parent d6ab948 commit 53cb8e4

7 files changed

Lines changed: 522 additions & 279 deletions

File tree

sources/net.sf.j2s.lib/build/build.xml

Lines changed: 392 additions & 276 deletions
Large diffs are not rendered by default.

sources/net.sf.j2s.lib/j2slib.zip

-51.4 KB
Binary file not shown.

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/J2SConcat.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ public static void main(String[] args) {
9090
}
9191
try {
9292
FileOutputStream fos = new FileOutputStream(dest);
93+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
9394
if (completelyCompressing) {
94-
fos.write(RegExCompress.regexCompress(buf.toString()).getBytes());
95+
fos.write(RegExCompress.regexCompress(buf.toString()).getBytes("UTF-8"));
9596
} else {
96-
fos.write(RegExCompress.regexCompress2(buf.toString()).getBytes());
97+
fos.write(RegExCompress.regexCompress2(buf.toString()).getBytes("UTF-8"));
9798
}
9899
fos.close();
99100
} catch (IOException e) {

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/PackCSSIntoJS.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,19 @@ public boolean accept(File pathname) {
106106
String jsContent2 = jsContent.substring(0, idx3) + ", \"" + cssContent + "\");\r\n" + jsContent.substring(idx2);
107107
if (!jsContent.equals(jsContent2)) {
108108
System.out.println("Updating " + jsFile.getName() + " ...");
109+
/*
109110
FileOutputStream fos = new FileOutputStream(jsFile);
110111
fos.write(jsContent2.getBytes());
111112
fos.close();
113+
*/
114+
try {
115+
FileOutputStream fos = new FileOutputStream(jsFile);
116+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
117+
fos.write(jsContent2.getBytes("UTF-8"));
118+
fos.close();
119+
} catch (IOException e) {
120+
e.printStackTrace();
121+
}
112122
}
113123
}
114124
}

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/RegExCompress.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,25 @@ public static String regexCompress2(String str) {
133133
public static String readFileAll(InputStream res) {
134134
try {
135135
ByteArrayOutputStream baos = new ByteArrayOutputStream();
136+
byte[] utf8Header = new byte[3];
136137
byte[] buf = new byte[1024];
137138
int read = 0;
139+
// Try to ignore the UTF-8 header! And return string are considered as
140+
// UTF-8 encoded.
141+
int readLen = 0;
142+
while ((read = res.read(utf8Header, readLen, 3 - readLen)) != -1) {
143+
readLen += read;
144+
if (readLen == 3) {
145+
if (utf8Header[0] == (byte) 0xef
146+
&& utf8Header[1] == (byte) 0xbb
147+
&& utf8Header[2] == (byte) 0xbf) {
148+
// skip
149+
} else {
150+
baos.write(utf8Header);
151+
}
152+
break;
153+
}
154+
}
138155
while ((read = res.read(buf)) != -1) {
139156
baos.write(buf, 0, read);
140157
}

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/SmartJSCompressor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ public static void main(String[] args) throws IOException {
190190
source = RegExCompress.regexCompress2(source);
191191
}
192192

193-
new FileOutputStream(dest).write(source.getBytes());
193+
FileOutputStream fos = new FileOutputStream(dest);
194+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
195+
fos.write(source.getBytes("utf-8"));
196+
fos.close();
194197
System.out.println(1.0 * source.length() / sourceLength);
195198
}
196199

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.lib.build;
15+
16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.FileNotFoundException;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
22+
/**
23+
* @author josson smith
24+
*
25+
* 2006-7-29
26+
*/
27+
public class UTF8Concat {
28+
/**
29+
* @param args
30+
*/
31+
public static void main(String[] args) {
32+
boolean completelyCompressing = true;
33+
int indexDelta = 1;
34+
if (args.length > 0 && ("true".equals(args[0]) || "false".equals(args[0]))) {
35+
completelyCompressing = "true".equals(args[0]);
36+
indexDelta++;
37+
}
38+
File dest = new File(args[indexDelta - 1]);
39+
StringBuffer buf = new StringBuffer();
40+
String j2sKeySig = "/* http://j2s.sf.net/ */";
41+
buf.append(j2sKeySig);
42+
System.out.println("To " + dest.getAbsolutePath());
43+
boolean isAllFileEarlier = dest.exists();
44+
if (isAllFileEarlier) {
45+
for (int i = 0; i < args.length - 1 - indexDelta; i++) {
46+
File src = new File(args[indexDelta], args[i + 1 + indexDelta]);
47+
if (!src.exists()) {
48+
System.err.println("Source file " + src.getAbsolutePath() + " does not exist!");
49+
return ;
50+
}
51+
// System.out.println("Checking " + args[i + 1 + indexDelta] + " ...");
52+
if (src.lastModified() > dest.lastModified()) {
53+
isAllFileEarlier = false;
54+
break;
55+
}
56+
}
57+
if (isAllFileEarlier) {
58+
System.out.println("Already updated.");
59+
return;
60+
}
61+
}
62+
for (int i = 0; i < args.length - 1 - indexDelta; i++) {
63+
File src = new File(args[indexDelta], args[i + 1 + indexDelta]);
64+
try {
65+
String s = RegExCompress.readFileAll(new FileInputStream(src));
66+
if (s.startsWith(j2sKeySig)) {
67+
s = s.substring(j2sKeySig.length());
68+
}
69+
/*
70+
if (completelyCompressing) {
71+
buf.append(RegExCompress.regexCompress(s));
72+
} else {
73+
buf.append(RegExCompress.regexCompress2(s));
74+
}
75+
*/
76+
buf.append(s);
77+
} catch (FileNotFoundException e) {
78+
e.printStackTrace();
79+
return;
80+
}
81+
}
82+
try {
83+
FileOutputStream fos = new FileOutputStream(dest);
84+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
85+
if (completelyCompressing) {
86+
fos.write(RegExCompress.regexCompress(buf.toString()).getBytes("UTF-8"));
87+
} else {
88+
fos.write(RegExCompress.regexCompress2(buf.toString()).getBytes("UTF-8"));
89+
}
90+
fos.close();
91+
} catch (IOException e) {
92+
e.printStackTrace();
93+
}
94+
95+
}
96+
}

0 commit comments

Comments
 (0)