Skip to content

Commit 0b71303

Browse files
hansonrhansonr
authored andcommitted
Charset support
1 parent f0190a2 commit 0b71303

18 files changed

+19433
-176
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,7 @@ private void initConsumer(ImageConsumer ic) {
583583
private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) {
584584
int off = pixeloffset + pixelscan * y + x;
585585
if (isConsumer(ic)) {
586-
boolean isbytes = AU.isAB(pixels);
587-
if (isbytes) {
586+
if (pixels instanceof byte[]) {
588587
ic.setPixels(x, y, w, h, model, (byte[]) pixels, off, pixelscan);
589588
} else {
590589
ic.setPixels(x, y, w, h, model, (int[]) pixels, off, pixelscan);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package java.nio;
2+
3+
public class InvalidMarkException
4+
extends IllegalStateException
5+
{
6+
private static final long serialVersionUID = 1698329710438510774L;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package java.nio.charset;
2+
3+
import java.io.IOException;
4+
5+
@SuppressWarnings("serial")
6+
public class CharacterCodingException extends IOException{}

sources/net.sf.j2s.java.core/src/java/nio/charset/Charset.java

Lines changed: 119 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.TreeMap;
4444

4545
import swingjs.JSCharSet;
46+
import swingjs.JSUtil;
4647
import sun.misc.ASCIICaseInsensitiveComparator;
4748
import sun.nio.cs.StandardCharsets;
4849
import sun.nio.cs.ThreadLocalCoders;
@@ -319,7 +320,7 @@ private static void checkName(String s) {
319320
}
320321

321322
/* The standard set of charsets */
322-
private static CharsetProvider standardProvider = new StandardCharsets();
323+
// private static CharsetProvider standardProvider = new StandardCharsets();
323324

324325
// Cache of the most-recently-returned charsets,
325326
// along with the names that were used to find them
@@ -383,77 +384,76 @@ public void remove() {
383384
}
384385

385386
// Thread-local gate to prevent recursive provider lookups
386-
private static ThreadLocal<ThreadLocal<?>> gate =
387-
new ThreadLocal<ThreadLocal<?>>();
388-
389-
private static Charset lookupViaProviders(final String charsetName) {
390-
391-
// The runtime startup sequence looks up standard charsets as a
392-
// consequence of the VM's invocation of System.initializeSystemClass
393-
// in order to, e.g., set system properties and encode filenames. At
394-
// that point the application class loader has not been initialized,
395-
// however, so we can't look for providers because doing so will cause
396-
// that loader to be prematurely initialized with incomplete
397-
// information.
398-
//
399-
if (!sun.misc.VM.isBooted())
400-
return null;
401-
402-
if (gate.get() != null)
403-
// Avoid recursive provider lookups
404-
return null;
405-
try {
406-
gate.set(gate);
407-
408-
return AccessController.doPrivileged(
409-
new PrivilegedAction<Charset>() {
410-
public Charset run() {
411-
for (Iterator<CharsetProvider> i = providers();
412-
i.hasNext();) {
413-
CharsetProvider cp = i.next();
414-
Charset cs = cp.charsetForName(charsetName);
415-
if (cs != null)
416-
return cs;
417-
}
418-
return null;
419-
}
420-
});
387+
// private static ThreadLocal<ThreadLocal<?>> gate = new ThreadLocal<ThreadLocal<?>>();
421388

422-
} finally {
423-
gate.set(null);
424-
}
425-
}
426-
427-
/* The extended set of charsets */
428-
private static class ExtendedProviderHolder {
429-
static final CharsetProvider extendedProvider = extendedProvider();
430-
// returns ExtendedProvider, if installed
431-
private static CharsetProvider extendedProvider() {
432-
return AccessController.doPrivileged(
433-
new PrivilegedAction<CharsetProvider>() {
434-
public CharsetProvider run() {
435-
try {
436-
Class<?> epc
437-
= Class.forName("sun.nio.cs.ext.ExtendedCharsets");
438-
return (CharsetProvider)epc.newInstance();
439-
} catch (ClassNotFoundException x) {
440-
// Extended charsets not available
441-
// (charsets.jar not present)
442-
} catch (InstantiationException |
443-
IllegalAccessException x) {
444-
throw new Error(x);
445-
}
446-
return null;
447-
}
448-
});
449-
}
450-
}
451-
452-
private static Charset lookupExtendedCharset(String charsetName) {
453-
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
454-
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
455-
}
389+
// private static Charset lookupViaProviders(final String charsetName) {
390+
//
391+
// // The runtime startup sequence looks up standard charsets as a
392+
// // consequence of the VM's invocation of System.initializeSystemClass
393+
// // in order to, e.g., set system properties and encode filenames. At
394+
// // that point the application class loader has not been initialized,
395+
// // however, so we can't look for providers because doing so will cause
396+
// // that loader to be prematurely initialized with incomplete
397+
// // information.
398+
// //
399+
// if (!sun.misc.VM.isBooted())
400+
// return null;
401+
//
402+
// if (gate.get() != null)
403+
// // Avoid recursive provider lookups
404+
// return null;
405+
// try {
406+
// gate.set(gate);
407+
//
408+
// return AccessController.doPrivileged(
409+
// new PrivilegedAction<Charset>() {
410+
// public Charset run() {
411+
// for (Iterator<CharsetProvider> i = providers();
412+
// i.hasNext();) {
413+
// CharsetProvider cp = i.next();
414+
// Charset cs = cp.charsetForName(charsetName);
415+
// if (cs != null)
416+
// return cs;
417+
// }
418+
// return null;
419+
// }
420+
// });
421+
//
422+
// } finally {
423+
// gate.set(null);
424+
// }
425+
// }
426+
//
427+
// /* The extended set of charsets */
428+
// private static class ExtendedProviderHolder {
429+
// static final CharsetProvider extendedProvider = extendedProvider();
430+
// // returns ExtendedProvider, if installed
431+
// private static CharsetProvider extendedProvider() {
432+
// return AccessController.doPrivileged(
433+
// new PrivilegedAction<CharsetProvider>() {
434+
// public CharsetProvider run() {
435+
// try {
436+
// Class<?> epc
437+
// = Class.forName("sun.nio.cs.ext.ExtendedCharsets");
438+
// return (CharsetProvider)epc.newInstance();
439+
// } catch (ClassNotFoundException x) {
440+
// // Extended charsets not available
441+
// // (charsets.jar not present)
442+
// } catch (InstantiationException |
443+
// IllegalAccessException x) {
444+
// throw new Error(x);
445+
// }
446+
// return null;
447+
// }
448+
// });
449+
// }
450+
// }
456451

452+
// private static Charset lookupExtendedCharset(String charsetName) {
453+
// CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
454+
// return (ecp != null) ? ecp.charsetForName(charsetName) : null;
455+
// }
456+
//
457457
private static Charset lookup(String charsetName) {
458458
if (charsetName == null)
459459
throw new IllegalArgumentException("Null charset name");
@@ -473,18 +473,26 @@ private static Charset lookup2(String charsetName) {
473473
cache1 = a;
474474
return (Charset)a[1];
475475
}
476-
Charset cs = new JSCharSet(charsetName, null);
477-
478-
if ((cs = standardProvider.charsetForName(charsetName)) != null ||
479-
(cs = lookupExtendedCharset(charsetName)) != null ||
480-
(cs = lookupViaProviders(charsetName)) != null)
481-
{
476+
String name = JSCharSet.lookupName(charsetName);
477+
if (name == null)
478+
return null;
479+
Charset cs;
480+
try {
481+
cs = (Charset) Class.forName("sun.nio.cs." + name).newInstance();
482+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
483+
return null;
484+
}
485+
if (cs != null) {
486+
// if ((cs = standardProvider.charsetForName(charsetName)) != null ||
487+
// (cs = lookupExtendedCharset(charsetName)) != null ||
488+
// (cs = lookupViaProviders(charsetName)) != null)
489+
// {
482490
cache(charsetName, cs);
483491
return cs;
484492
}
485493

486494
/* Only need to check the name if we didn't find a charset for it */
487-
checkName(charsetName);
495+
// checkName(charsetName);
488496
return null;
489497
}
490498

@@ -572,23 +580,25 @@ private static void put(Iterator<Charset> i, Map<String,Charset> m) {
572580
* to charset objects
573581
*/
574582
public static SortedMap<String,Charset> availableCharsets() {
575-
return AccessController.doPrivileged(
576-
new PrivilegedAction<SortedMap<String,Charset>>() {
577-
public SortedMap<String,Charset> run() {
578-
TreeMap<String,Charset> m =
579-
new TreeMap<String,Charset>(
580-
ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
581-
put(standardProvider.charsets(), m);
582-
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
583-
if (ecp != null)
584-
put(ecp.charsets(), m);
585-
for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
586-
CharsetProvider cp = i.next();
587-
put(cp.charsets(), m);
588-
}
589-
return Collections.unmodifiableSortedMap(m);
590-
}
591-
});
583+
JSUtil.notImplemented("Charset.availableCharsets");
584+
return null;
585+
//// return AccessController.doPrivileged(
586+
//// new PrivilegedAction<SortedMap<String,Charset>>() {
587+
//// public SortedMap<String,Charset> run() {
588+
// TreeMap<String,Charset> m =
589+
// new TreeMap<String,Charset>(
590+
// ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
591+
// put(standardProvider.charsets(), m);
592+
// CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
593+
// if (ecp != null)
594+
// put(ecp.charsets(), m);
595+
// for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
596+
// CharsetProvider cp = i.next();
597+
// put(cp.charsets(), m);
598+
// }
599+
// return Collections.unmodifiableSortedMap(m);
600+
//// }
601+
//// });
592602
}
593603

594604
private static volatile Charset defaultCharset;
@@ -640,10 +650,10 @@ public static Charset defaultCharset() {
640650
* If the canonical name or any of the aliases are illegal
641651
*/
642652
protected Charset(String canonicalName, String[] aliases) {
643-
checkName(canonicalName);
653+
// checkName(canonicalName);
644654
String[] as = (aliases == null) ? new String[0] : aliases;
645-
for (int i = 0; i < as.length; i++)
646-
checkName(as[i]);
655+
// for (int i = 0; i < as.length; i++)
656+
// checkName(as[i]);
647657
this.name = canonicalName;
648658
this.aliases = as;
649659
}
@@ -777,6 +787,18 @@ public boolean canEncode() {
777787
return true;
778788
}
779789

790+
/**
791+
* Added for convenience - SwingJS
792+
*
793+
* @param b
794+
* @return
795+
* @throws CharacterCodingException
796+
*/
797+
public final CharBuffer decode(byte[] b, int offset, int length) throws CharacterCodingException {
798+
return decode(ByteBuffer.wrap(b, offset, length));
799+
}
800+
801+
780802
/**
781803
* Convenience method that decodes bytes in this charset into Unicode
782804
* characters.

0 commit comments

Comments
 (0)