Skip to content

Commit 3def82d

Browse files
authored
Merge pull request #223 from BobHanson/hanson1
j2sClazz update, BufferedImage stolen data fix, CharSequence lambda fix
2 parents 9d814a9 + 3ad46d4 commit 3def82d

File tree

18 files changed

+261
-223
lines changed

18 files changed

+261
-223
lines changed
553 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728102503
1+
20210728170251
553 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728102503
1+
20210728170251
553 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/awt/image/BufferedImage.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@
112112
* @see WritableRaster
113113
*/
114114

115-
public class BufferedImage extends Image implements RenderedImage, Transparency // , WritableRenderedImage
115+
public class BufferedImage extends Image implements RenderedImage, Transparency, ImageObserver
116+
// , WritableRenderedImage
116117
{
117118

118119
private static final int STATE_UNINITIALIZED = 0;
@@ -1366,7 +1367,7 @@ public Graphics2D createGraphics() {
13661367
// allow creating a Graphics from MemoryImageSource
13671368
// so pixels would never be there.
13681369
if (秘pix != null)
1369-
秘g.drawImageFromPixelsOrRaster(this, 0, 0, null);
1370+
秘g.drawImageFromPixelsOrRaster(this, 0, 0, (ImageObserver) this);
13701371
}
13711372
Object pix = 秘pix;
13721373
/**
@@ -1389,8 +1390,11 @@ public Graphics2D createGraphics() {
13891390
}
13901391

13911392
public void 秘graphicsDisposed() {
1392-
gCount = Math.max(0, gCount - 1);
1393+
boolean doSync = (秘haveCanvas() && 秘isRasterDirty(false));
13931394
秘state = STATE_GRAPHICS;
1395+
if (doSync) {
1396+
秘syncRaster();
1397+
}
13941398
}
13951399

13961400
/**
@@ -1404,20 +1408,25 @@ public Graphics2D createGraphics() {
14041408
*/
14051409
@Override
14061410
public void flush() {
1407-
boolean isStolen = 秘isRasterDirty(false);
1408-
boolean haveRaster = 秘haveRaster();
1409-
if (isStolen && !haveRaster)
1410-
秘ensureRasterUpToDate(); // will set STATE_RASTER
1411+
boolean setRasterState = 秘syncRaster();
14111412
if (秘haveRaster() && 秘pix == null) {
14121413
秘getPixelsFromRaster(0);
14131414
}
14141415
秘getImageGraphic();
1415-
if (isStolen || haveRaster)
1416+
if (setRasterState)
14161417
秘state |= STATE_RASTER;
1417-
while (gCount > 0 && --gCount >= 0)
1418+
while (gCount-- > 0)
14181419
秘g.dispose();
14191420
}
14201421

1422+
private boolean 秘syncRaster() {
1423+
boolean isStolen = 秘isRasterDirty(false);
1424+
boolean haveRaster = 秘haveRaster();
1425+
if (isStolen && !haveRaster)
1426+
秘ensureRasterUpToDate(); // will set STATE_RASTER
1427+
return (isStolen || haveRaster);
1428+
}
1429+
14211430
/**
14221431
* Called only in JavaScript, because we are using this in HTML5Canvas, which is
14231432
* a distributed public interface, and the Mandarin char was causing problems.
@@ -2474,4 +2483,10 @@ public String toString() {
24742483
return new Color(pc.pixelToRgb(pc.rgbToPixel(c.getRGB(), null), null));
24752484
}
24762485

2486+
@Override
2487+
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
2488+
// ignored
2489+
return false;
2490+
}
2491+
24772492
}

sources/net.sf.j2s.java.core/src/javajs/util/Rdr.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static String bytesToUTF8String(byte[] bytes) {
114114
* XMLReaders
115115
*
116116
* @param bis
117-
* @return a UTF-8 string
117+
* @return a UTF-8 string or null if there is an error
118118
*/
119119
public static String streamToUTF8String(BufferedInputStream bis) {
120120
String[] data = new String[1];
@@ -144,7 +144,8 @@ public static BufferedReader getBufferedReader(BufferedInputStream bis, String c
144144
}
145145

146146
/**
147-
* This method is specifically for strings that are marked for UTF 8 or 16.
147+
* This method is specifically for strings that might be marked for UTF 8 or 16.
148+
* In this case, Java would return a (0xFEFF) code point as the first character.
148149
*
149150
* @param bytes
150151
* @return UTF-decoded bytes
@@ -344,6 +345,8 @@ public static byte[] getBytesFromSB(SB sb) {
344345
* Read a an entire BufferedInputStream for its bytes, and either return them or
345346
* leave them in the designated output channel.
346347
*
348+
* Closes the stream.
349+
*
347350
* @param bis
348351
* @param out a destination output channel, or null
349352
* @return byte[] (if out is null) or a message indicating length (if not)

sources/net.sf.j2s.java.core/src/swingjs/JSGraphics2D.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ private boolean drawImagePriv(Image img, int x, int y, int width, int height, Im
712712
int[] pixels = (int[]) (isTranslationOnly(m) && !isClipped(x,y,width,height) ? ((BufferedImage) img).get秘pixFromRaster() : null);
713713
DOMNode imgNode = null;
714714
if (pixels == null) {
715-
imgNode = ((BufferedImage) img).秘getImageNode(BufferedImage.GET_IMAGE_FROM_RASTER);
715+
imgNode = (img == observer ? canvas : ((BufferedImage) img).秘getImageNode(BufferedImage.GET_IMAGE_FROM_RASTER));
716716
if (imgNode != null)
717717
ctx.drawImage(imgNode, x, y, width, height);
718718
} else {

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes, JSFun
207207
try {
208208
URL url = new URL(uri);
209209
BufferedInputStream stream = (BufferedInputStream) url.getContent();
210-
return (asBytes ? Rdr.getStreamAsBytes(stream, null) : Rdr.streamToUTF8String(stream));
210+
return (asBytes ? streamToBytes(stream) : streamToString(stream));
211211
} catch (Exception e) {
212212
}
213213
}
@@ -248,11 +248,14 @@ static String ensureString(Object data) {
248248
if (data == null)
249249
return null;
250250
if (data instanceof byte[])
251-
return Rdr.bytesToUTF8String((byte[]) data);
251+
return new String((byte[]) data); // was Rdr.bytesToUTF8String
252252
if (data instanceof String || data instanceof SB)
253253
return data.toString();
254254
if (data instanceof InputStream)
255-
return Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
255+
try {
256+
return streamToString((InputStream) data);
257+
} catch (IOException e) {
258+
}
256259
return null;
257260
}
258261

@@ -1267,6 +1270,29 @@ public String getJ2SPath() {
12671270
return (String) getAppletAttribute("_j2sFullPath");
12681271
}
12691272

1273+
/**
1274+
* Read an InputStream in its entirety as a string, closing the stream.
1275+
*
1276+
* @param is
1277+
* @return a String
1278+
* @throws IOException
1279+
*/
1280+
public static String streamToString(InputStream is) throws IOException {
1281+
return new String(streamToBytes(is));
1282+
}
1283+
1284+
/**
1285+
* Read an InputStream in its entirety as a byte array. Closes the stream.
1286+
*
1287+
* @param is
1288+
* @return a byte array
1289+
*/
1290+
public static byte[] streamToBytes(InputStream is) throws IOException {
1291+
byte[] bytes = Rdr.getLimitedStreamBytes(is, -1);
1292+
is.close();
1293+
return bytes;
1294+
}
1295+
12701296

12711297

12721298

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBClass.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package swingjs.xml;
22

3-
import java.io.BufferedInputStream;
3+
import java.io.IOException;
44
import java.io.InputStream;
55
import java.util.ArrayList;
66
import java.util.HashMap;
@@ -12,7 +12,7 @@
1212
import javax.xml.namespace.QName;
1313

1414
import javajs.util.PT;
15-
import javajs.util.Rdr;
15+
import swingjs.JSUtil;
1616
import swingjs.api.Interface;
1717

1818
class JSJAXBClass implements Cloneable {
@@ -111,11 +111,15 @@ private static void getPackageInfo(Class<?> javaClass) {
111111
// Keeping this simple for now.
112112
InputStream is = javaClass.getResourceAsStream("_$.js");
113113
if (is != null) {
114-
String data = Rdr.streamToUTF8String(new BufferedInputStream(is));
115-
packageAccessorType = parseAccessorType(data);
116-
data = PT.getQuotedAttribute(data, "namespace");
117-
if (data != null)
118-
packageNamespace = data;
114+
String data;
115+
try {
116+
data = JSUtil.streamToString(is);
117+
packageAccessorType = parseAccessorType(data);
118+
data = PT.getQuotedAttribute(data, "namespace");
119+
if (data != null)
120+
packageNamespace = data;
121+
} catch (IOException e) {
122+
}
119123
}
120124
}
121125
}

0 commit comments

Comments
 (0)