Java Code Examples for java.nio.charset.Charset#availableCharsets()

The following examples show how to use java.nio.charset.Charset#availableCharsets() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MalformedSurrogates.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 
Example 2
Source File: MalformedSurrogates.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 
Example 3
Source File: InferFileTypeTest.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Test printout the JVM's supported charsets.
 */
@Test
public void testPrintoutSupportedCharsets() {
    SortedMap<String, Charset> charsetMap = Charset.availableCharsets();
    debugPrint("------------- availableCharsets ----------------");
    for (Map.Entry<String, Charset> entry : charsetMap.entrySet()) {
        String canonicalCharsetName = entry.getKey();
        debugPrint(canonicalCharsetName);
        Charset charset = entry.getValue();
        Set<String> aliases = charset.aliases();
        for (String alias : aliases) {
            debugPrint("\t" + alias);
        }
    }
    debugPrint("-----------------------------------------");
    String[] perforceCharsets = PerforceCharsets.getKnownCharsets();
    debugPrint("------------- perforceCharsets ----------------");
    for (String perforceCharset : perforceCharsets) {
        debugPrint(perforceCharset + " ... "
                + PerforceCharsets.getJavaCharsetName(perforceCharset));
    }
    debugPrint("-----------------------------------------");
    debugPrint("-----------------------------------------");
    debugPrint("Charset.defaultCharset().name(): " + Charset.defaultCharset().name());
    debugPrint("-----------------------------------------");
}
 
Example 4
Source File: MainFrame.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the code pages.
 */
public void createCodePages() {
  // Get supported encodings from JVM
  Map<String, Charset> charsetMap = Charset.availableCharsets();
  String sysCodePage = Charset.defaultCharset().name();
  
  this.codePages = new ArrayList<>();

  if (sysCodePage != null) {
    this.codePages.add(sysCodePage);
    if (this.codePage == null) {
      this.codePage = sysCodePage;
    }
  }
  for (String charsetName : charsetMap.keySet()) {
    if (!this.codePages.contains(charsetName)) {
      this.codePages.add(charsetName);
    }
  }
}
 
Example 5
Source File: CharsetsDialog.java    From 920-text-editor-v2 with Apache License 2.0 6 votes vote down vote up
private void initCharsets() {
        SortedMap m = Charset.availableCharsets();
        Set k = m.keySet();

        names = new String[m.size()];
        Iterator iterator = k.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            String n = (String) iterator.next();
//            Charset e = (Charset) m.get(n);
//            String d = e.displayName();
//            boolean c = e.canEncode();
//            System.out.print(n+", "+d+", "+c);
//            Set s = e.aliases();
//            Iterator j = s.iterator();
//            while (j.hasNext()) {
//                String a = (String) j.next();
//                System.out.print(", "+a);
//            }
//            System.out.println("");
            names[i++] = n;
        }
    }
 
Example 6
Source File: CheckEncodingPropertiesFile.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void printAllCharsets() {
    Map<String, Charset> all = Charset.availableCharsets();
    System.out.println("\n=========================================\n");
    for (String can : all.keySet()) {
        System.out.println(can + ": " + all.get(can).aliases());
    }
}
 
Example 7
Source File: SyncJapaneseFilesCharsetTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Test printout the JVM's supported charsets.
 */
@Test
public void testPrintoutSupportedCharsets() {
  SortedMap<String, Charset> charsetMap = Charset.availableCharsets();

  debugPrint("------------- availableCharsets ----------------");
  for (Map.Entry<String, Charset> entry : charsetMap.entrySet()) {
    String canonicalCharsetName = entry.getKey();
    debugPrint(canonicalCharsetName);
    Charset charset = entry.getValue();
    Set<String> aliases = charset.aliases();
    for (String alias : aliases) {
      debugPrint("\t" + alias);
    }
  }
  debugPrint("-----------------------------------------");

  String[] perforceCharsets = PerforceCharsets.getKnownCharsets();
  debugPrint("------------- perforceCharsets ----------------");
  for (String perforceCharset : perforceCharsets) {
    debugPrint(perforceCharset + " ... " + PerforceCharsets.getJavaCharsetName(perforceCharset));
  }
  debugPrint("-----------------------------------------");

  debugPrint("-----------------------------------------");
  debugPrint("Charset.defaultCharset().name(): " + Charset.defaultCharset().name());
  debugPrint("-----------------------------------------");
}
 
Example 8
Source File: TestString.java    From Jpom with MIT License 5 votes vote down vote up
public static void main(String[] args) {
//        System.out.println(CheckPassword.checkPassword("123aA!"));
//        DateTime dateTime = DateUtil.parseUTC("2019-04-04T10:11:21Z");
//        System.out.println(dateTime);
//        dateTime.setTimeZone(TimeZone.getDefault());
//        System.out.println(dateTime);
        Pattern pattern = Pattern.compile("(https://|http://)?([\\w-]+\\.)+[\\w-]+(:\\d+|/)+([\\w- ./?%&=]*)?");
        String url = "http://192.168.1.111:2122/node/index.html?nodeId=dyc";
        System.out.println(ReUtil.isMatch(pattern, url));
        System.out.println(ReUtil.isMatch(PatternPool.URL_HTTP, url));


//        System.out.println(FileUtil.file("/a", null, "", "ss"));

        System.out.println(Math.pow(1024, 2));

        System.out.println(Integer.MAX_VALUE);

        while (true) {
            SortedMap<String, Charset> x = Charset.availableCharsets();
            Collection<Charset> values = x.values();
            boolean find = false;
            for (Charset charset : values) {
                String name = charset.name();
                if ("utf-8".equalsIgnoreCase(name)) {
                    find = true;
                    break;
                }
            }
            if (!find) {
                System.out.println("没有找utf-8");
            }
        }
//        System.out.println(x);
    }
 
Example 9
Source File: CEncoding.java    From FxDock with Apache License 2.0 5 votes vote down vote up
public static CEncoding[] list()
{
	if(encodings == null)
	{
		SortedMap<String,Charset> m = Charset.availableCharsets();
		CEncoding[] a = new CEncoding[m.size()];
		int ix = 0;
		for(Charset cs: m.values())
		{
			a[ix++] = new CEncoding(cs);
		}
		encodings = a;
	}
	return encodings;
}
 
Example 10
Source File: CharsetsDataServlet.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
		IOException {
	try {
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");

		String locale = request.getParameter("locale");

		// Avoid resource caching
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);

		PrintWriter writer = response.getWriter();
		writer.print("<list>");
		writer.print("<charset>");
		writer.print("<code>auto</code>");
		writer.print("<name>auto</name>");
		writer.print("</charset>");
		Map<String, Charset> charsets = Charset.availableCharsets();
		for (String name : charsets.keySet()) {
			writer.print("<charset>");
			writer.print("<code><![CDATA[" + name + "]]></code>");
			writer.print("<name><![CDATA[" + charsets.get(name).displayName(LocaleUtil.toLocale(locale))
					+ "]]></name>");
			writer.print("</charset>");
		}

		writer.print("</list>");
	} catch (Throwable e) {
		log.error(e.getMessage(), e);
		if (e instanceof ServletException)
			throw (ServletException) e;
		else if (e instanceof IOException)
			throw (IOException) e;
		else
			throw new ServletException(e.getMessage(), e);
	}
}
 
Example 11
Source File: CPEditController.java    From olat with Apache License 2.0 5 votes vote down vote up
CompMenuForm(final UserRequest ureq, final WindowControl wControl, final Boolean compMenuConfig, final String contentEncoding, final String jsEncoding) {
    super(ureq, wControl);
    this.compMenuConfig = compMenuConfig == null ? true : compMenuConfig.booleanValue();
    this.contentEncoding = contentEncoding;
    this.jsEncoding = jsEncoding;

    final Map<String, Charset> charsets = Charset.availableCharsets();
    final int numOfCharsets = charsets.size() + 1;

    encodingContentKeys = new String[numOfCharsets];
    encodingContentKeys[0] = NodeEditController.CONFIG_CONTENT_ENCODING_AUTO;

    encodingContentValues = new String[numOfCharsets];
    encodingContentValues[0] = translate("encoding.auto");

    encodingJSKeys = new String[numOfCharsets];
    encodingJSKeys[0] = NodeEditController.CONFIG_JS_ENCODING_AUTO;

    encodingJSValues = new String[numOfCharsets];
    encodingJSValues[0] = translate("encoding.same");

    int count = 1;
    final Locale locale = ureq.getLocale();
    for (final Map.Entry<String, Charset> charset : charsets.entrySet()) {
        encodingContentKeys[count] = charset.getKey();
        encodingContentValues[count] = charset.getValue().displayName(locale);
        encodingJSKeys[count] = charset.getKey();
        encodingJSValues[count] = charset.getValue().displayName(locale);
        count++;
    }

    initForm(ureq);
}
 
Example 12
Source File: CheckEncodingPropertiesFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void printAllCharsets() {
    Map<String, Charset> all = Charset.availableCharsets();
    System.out.println("\n=========================================\n");
    for (String can : all.keySet()) {
        System.out.println(can + ": " + all.get(can).aliases());
    }
}
 
Example 13
Source File: CheckEncodingPropertiesFile.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void printAllCharsets() {
    Map<String, Charset> all = Charset.availableCharsets();
    System.out.println("\n=========================================\n");
    for (String can : all.keySet()) {
        System.out.println(can + ": " + all.get(can).aliases());
    }
}
 
Example 14
Source File: CheckEncodingPropertiesFile.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void printAllCharsets() {
    Map<String, Charset> all = Charset.availableCharsets();
    System.out.println("\n=========================================\n");
    for (String can : all.keySet()) {
        System.out.println(can + ": " + all.get(can).aliases());
    }
}
 
Example 15
Source File: GuiUtils.java    From common_gui_tools with Apache License 2.0 4 votes vote down vote up
/**
 * 当前Java虚拟机支持的charset.
 */
public static SortedMap<String, Charset> availableCharsets() {
    return Charset.availableCharsets();
}
 
Example 16
Source File: SyncUtf16beFilesWinLocalTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test sync utf16-le encoded files: UTF-16LE BOM: ff fe
 */
@Test
public void testSyncUtf16leFiles() {
	String depotFile = null;

	try {
		IClient client = server.getClient("p4TestUserWS20112Windows");
		server.setCurrentClient(client);
		SortedMap<String, Charset> charsetMap = Charset.availableCharsets();

		debugPrint("------------- availableCharsets ----------------");
		for (Map.Entry<String, Charset> entry : charsetMap.entrySet()) {
			String canonicalCharsetName = entry.getKey();
			debugPrint(canonicalCharsetName);
			Charset charset = entry.getValue();
			Set<String> aliases = charset.aliases();
			for (String alias : aliases) {
				debugPrint("\t" + alias);
			}
		}
		debugPrint("-----------------------------------------");

		String[] perforceCharsets = PerforceCharsets.getKnownCharsets();
		debugPrint("------------- perforceCharsets ----------------");
		for (String perforceCharset : perforceCharsets) {
			debugPrint(perforceCharset + " ... "
					+ PerforceCharsets.getJavaCharsetName(perforceCharset));
		}
		debugPrint("-----------------------------------------");

		debugPrint("-----------------------------------------");
		debugPrint("Charset.defaultCharset().name(): "
				+ Charset.defaultCharset().name());
		debugPrint("-----------------------------------------");
		
		
		depotFile = "//depot/152Bugs/utf16-be/utf16-be.xbit";

		List<IFileSpec> files = client.sync(
				FileSpecBuilder.makeFileSpecList(depotFile),
				new SyncOptions().setForceUpdate(true));
		assertNotNull(files);

	} catch (P4JavaException e) {
		fail("Unexpected exception: " + e.getLocalizedMessage());
	}
}
 
Example 17
Source File: SyncUtf16beFilesWinLocalTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test sync utf16-le encoded files: UTF-16LE BOM: ff fe
 */
@Test
public void testSyncUtf16leFiles() {
	String depotFile = null;

	try {
		IClient client = server.getClient("p4TestUserWS20112Windows");
		server.setCurrentClient(client);
		SortedMap<String, Charset> charsetMap = Charset.availableCharsets();

		debugPrint("------------- availableCharsets ----------------");
		for (Map.Entry<String, Charset> entry : charsetMap.entrySet()) {
			String canonicalCharsetName = entry.getKey();
			debugPrint(canonicalCharsetName);
			Charset charset = entry.getValue();
			Set<String> aliases = charset.aliases();
			for (String alias : aliases) {
				debugPrint("\t" + alias);
			}
		}
		debugPrint("-----------------------------------------");

		String[] perforceCharsets = PerforceCharsets.getKnownCharsets();
		debugPrint("------------- perforceCharsets ----------------");
		for (String perforceCharset : perforceCharsets) {
			debugPrint(perforceCharset + " ... "
					+ PerforceCharsets.getJavaCharsetName(perforceCharset));
		}
		debugPrint("-----------------------------------------");

		debugPrint("-----------------------------------------");
		debugPrint("Charset.defaultCharset().name(): "
				+ Charset.defaultCharset().name());
		debugPrint("-----------------------------------------");
		
		
		depotFile = "//depot/152Bugs/utf16-be/utf16-be.xbit";

		List<IFileSpec> files = client.sync(
				FileSpecBuilder.makeFileSpecList(depotFile),
				new SyncOptions().setForceUpdate(true));
		assertNotNull(files);

	} catch (P4JavaException e) {
		fail("Unexpected exception: " + e.getLocalizedMessage());
	}
}
 
Example 18
Source File: SyncUtf16beFilesWinLocalTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test sync utf16-le encoded files: UTF-16LE BOM: ff fe
 */
@Test
public void testSyncUtf16leFiles() {
	String depotFile = null;

	try {
		IClient client = server.getClient("p4TestUserWS20112Windows");
		server.setCurrentClient(client);
		SortedMap<String, Charset> charsetMap = Charset.availableCharsets();

		debugPrint("------------- availableCharsets ----------------");
		for (Map.Entry<String, Charset> entry : charsetMap.entrySet()) {
			String canonicalCharsetName = entry.getKey();
			debugPrint(canonicalCharsetName);
			Charset charset = entry.getValue();
			Set<String> aliases = charset.aliases();
			for (String alias : aliases) {
				debugPrint("\t" + alias);
			}
		}
		debugPrint("-----------------------------------------");

		String[] perforceCharsets = PerforceCharsets.getKnownCharsets();
		debugPrint("------------- perforceCharsets ----------------");
		for (String perforceCharset : perforceCharsets) {
			debugPrint(perforceCharset + " ... "
					+ PerforceCharsets.getJavaCharsetName(perforceCharset));
		}
		debugPrint("-----------------------------------------");

		debugPrint("-----------------------------------------");
		debugPrint("Charset.defaultCharset().name(): "
				+ Charset.defaultCharset().name());
		debugPrint("-----------------------------------------");
		
		
		depotFile = "//depot/152Bugs/utf16-be/utf16-be.xbit";

		List<IFileSpec> files = client.sync(
				FileSpecBuilder.makeFileSpecList(depotFile),
				new SyncOptions().setForceUpdate(true));
		assertNotNull(files);

	} catch (P4JavaException e) {
		fail("Unexpected exception: " + e.getLocalizedMessage());
	}
}
 
Example 19
Source File: PreferencesFormController.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * org.olat.presentation.framework.control.Controller, org.olat.presentation.framework.UserRequest)
 */
@Override
protected void initForm(final FormItemContainer formLayout, final Controller listener, final UserRequest ureq) {
    setFormTitle("title.prefs");
    setFormContextHelp(this.getClass().getPackage().getName(), "home-prefs.html", "help.hover.prefs");

    // load preferences
    final Preferences prefs = tobeChangedIdentity.getUser().getPreferences();

    // Username
    final StaticTextElement username = uifactory.addStaticTextElement("form.username", tobeChangedIdentity.getName(), formLayout);
    username.setEnabled(false);

    // Language
    final Map<String, String> languages = I18nManager.getInstance().getEnabledLanguagesTranslated();
    final String[] langKeys = StringHelper.getMapKeysAsStringArray(languages);
    final String[] langValues = StringHelper.getMapValuesAsStringArray(languages);
    ArrayHelper.sort(langKeys, langValues, false, true, false);
    language = uifactory.addDropdownSingleselect("form.language", formLayout, langKeys, langValues, null);
    final String langKey = prefs.getLanguage();
    // Preselect the users language if available. Maye not anymore enabled on
    // this server
    if (prefs.getLanguage() != null && I18nModule.getEnabledLanguageKeys().contains(langKey)) {
        language.select(prefs.getLanguage(), true);
    } else {
        language.select(I18nModule.getDefaultLocale().toString(), true);
    }

    // Font size
    final String[] cssFontsizeValues = new String[] { translate("form.fontsize.xsmall"), translate("form.fontsize.small"), translate("form.fontsize.normal"),
            translate("form.fontsize.large"), translate("form.fontsize.xlarge"), translate("form.fontsize.presentation") };
    fontsize = uifactory.addDropdownSingleselect("form.fontsize", formLayout, cssFontsizeKeys, cssFontsizeValues, null);
    fontsize.select(prefs.getFontsize(), true);
    fontsize.addActionListener(this, FormEvent.ONCHANGE);

    // Text encoding
    final Map<String, Charset> charsets = Charset.availableCharsets();
    final String currentCharset = getUserService().getUserCharset(tobeChangedIdentity);
    final String[] csKeys = StringHelper.getMapKeysAsStringArray(charsets);
    charset = uifactory.addDropdownSingleselect("form.charset", formLayout, csKeys, csKeys, null);
    charset.select(currentCharset, true);

    // Submit and cancel buttons
    final FormLayoutContainer buttonLayout = FormLayoutContainer.createButtonLayout("button_layout", getTranslator());
    formLayout.add(buttonLayout);
    uifactory.addFormSubmitButton("submit", buttonLayout);
    uifactory.addFormCancelButton("cancel", buttonLayout, ureq, getWindowControl());
}
 
Example 20
Source File: ShapeFileChooser.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Constructor
 */
public ShapeFileChooser() {
    super();
    encodingCB = new JComboBox();
    SortedMap m = Charset.availableCharsets();
    DefaultComboBoxModel cbm = new DefaultComboBoxModel();
    cbm.addElement("System");
    Iterator ir = m.keySet().iterator();
    while (ir.hasNext()) {
        String n = (String) ir.next();
        Charset e = (Charset) m.get(n);
        cbm.addElement(e.displayName());
    }
    encodingCB.setModel(cbm);
    panel = new JPanel(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(5, 10, 0, 0);
    constraints.gridx = 0;
    constraints.gridy = 0;
    panel.add(new JLabel("Encoding:"), constraints);
    constraints.gridy = 1;
    panel.add(encodingCB, constraints);    
    setAccessory(panel);
    //panel.setVisible(false);

    this.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent pce) {
            String prop = pce.getPropertyName();

            //If a file became selected, find out which one.
            if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
                File file = (File) pce.getNewValue();
                if (file != null && file.isFile()){
                    String fn = file.getAbsolutePath();
                    if (fn.toLowerCase().endsWith(".shp")) {
                        String encoding = IOUtil.encodingDetectShp(fn);
                        if (encoding.equals("ISO8859_1"))
                            encoding = "UTF-8";
                        encodingCB.setSelectedItem(encoding);
                        panel.setVisible(true);
                    } else {
                        panel.setVisible(false);
                    }
                    repaint();
                }
            }
        }
    });
}