/** * Returns a Charset for the named charset. If the name is null, return the default Charset. * * @param charset * The name of the requested charset, may be null. * @return a Charset for the named charset * @throws java.nio.charset.UnsupportedCharsetException * If the named charset is unavailable */ public static Charset toCharset(final String charset) { return charset == null ? Charset.defaultCharset() : Charset.forName(charset); }
public String call() throws IOException { return Charset.defaultCharset().name(); } }
@Override public void write(D record) throws IOException { if (null != record) { String s = record.toString(); System.out.println(s); ++ _numRecordsWritten; _numBytesWritten += s.getBytes(Charset.defaultCharset()).length; } }
import java.io.File; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; // ... Path filePath = new File("fileName").toPath(); Charset charset = Charset.defaultCharset(); List<String> stringList = Files.readAllLines(filePath, charset); String[] stringArray = stringList.toArray(new String[]{});
@Test public void testMultiByteBreak() throws Exception { System.out.println("testMultiByteBreak() Default charset: "+Charset.defaultCharset().displayName()); final long delay = 50; final File origin = new File(this.getClass().getResource("/test-file-utf8.bin").toURI()); final File file = new File(getTestDirectory(), "testMultiByteBreak.txt"); createFile(file, 0); final TestTailerListener listener = new TestTailerListener();
@Before public void setUp() throws IOException { inputFile = new File(TEST_INPUT_DIR); expectedMessages = new ArrayList<>(); outContent = new ByteArrayOutputStream(); errContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent, false, Charset.defaultCharset().name())); System.setErr(new PrintStream(errContent, false, Charset.defaultCharset().name())); }
@Test public void sameEncoding_string_CharsetEncoder_constructor() throws Exception { final CharsetEncoder enc = Charset.defaultCharset().newEncoder(); succesfulRun(new FileWriterWithEncoding(file2.getPath(), enc)); }
public static void main(String[] args) throws Exception { System.out.println(Arrays.asList(args)); File thisFolder = new File(SelfrunTest.class.getResource("/org/embulk/cli/DummyMain.class").toURI()).getParentFile(); try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(thisFolder, "args.txt")), Charset.defaultCharset()))) { for (String arg : args) { writer.write(arg); writer.newLine(); } } } }
@Test public void testTwoSourceInputFiles() throws IOException { File inputFile1 = new File(TEST_DIR + "/UpperCamelCaseTest.swift"); File inputFile2 = new File(TEST_DIR + "/LowerCamelCaseTest.swift"); String[] command = {"--no-color", inputFile1.getPath(), inputFile2.getPath() }; Tailor.main(command); String[] msgs = outContent.toString(Charset.defaultCharset().name()).split(NEWLINE_REGEX); String actualSummary = msgs[msgs.length - 1]; assertTrue(actualSummary.startsWith("Analyzed 2 files, skipped 0 files")); }
@Test public void testScp() throws Exception { if (isRemote == false) return; SSHClient ssh = new SSHClient(this.hostname, this.port, this.username, this.password); File tmpFile = File.createTempFile("test_scp", "", new File("/tmp")); tmpFile.deleteOnExit(); FileUtils.write(tmpFile, "test_scp", Charset.defaultCharset()); ssh.scpFileToRemote(tmpFile.getAbsolutePath(), "/tmp"); } }
@Test public void testDisplaySummary() throws IOException { final long files = 5; final long skipped = 1; final long errors = 7; final long warnings = 4; formatter.displaySummary(files, skipped, errors, warnings); assertThat(outContent.toString(Charset.defaultCharset().name()), isEmptyString()); }
@Test public void testToCharset() { Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset())); Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8"))); }
private ImmutableSortedMap<String, ImmutableSet<String>> readMap(final String mapPath) { String fileContent; String actualPath = mapPath; try { if (Strings.isNullOrEmpty(mapPath)) { URL resource = this.getClass().getClassLoader().getResource("defaultWhiteListMap.json"); actualPath = resource.getFile(); LOGGER.info("using default whiteList map located at [%s]", actualPath); fileContent = Resources.toString(resource, Charset.defaultCharset()); } else { fileContent = Files.asCharSource(new File(mapPath), Charset.forName("UTF-8")).read(); } return mapper.readerFor(new TypeReference<ImmutableSortedMap<String, ImmutableSet<String>>>() { }).readValue(fileContent); } catch (IOException e) { throw new ISE(e, "Got an exception while parsing file [%s]", actualPath); } } }
/** * Main method being triggered in step 3 of the Workaround. * <p> * Method prints out TRANSFORMATION_FAILED_FLAG if something went wrong. * * @param args Command line arguments passed by a user in step 1 of the Workaround. */ public static void main(String[] args) { PrintStream ps = null; try { String encoding = System.getProperty("file.encoding", Charset.defaultCharset().name()); ps = new PrintStream(System.out, true, encoding); ps.println(transform(args)); } catch (Throwable t) { t.printStackTrace(); if (ps != null) ps.println(TRANSFORMATION_FAILED_FLAG); if (t instanceof Error) throw (Error)t; } }
public static StreamEncoder forOutputStreamWriter(OutputStream out, Object lock, String charsetName) throws UnsupportedEncodingException { String csn = charsetName; if (csn == null) csn = Charset.defaultCharset().name(); try { if (Charset.isSupported(csn)) return new StreamEncoder(out, lock, Charset.forName(csn)); } catch (IllegalCharsetNameException x) { } throw new UnsupportedEncodingException (csn); }
public static List<String> readFileByLine(String filePath) throws IOException { return Files.readLines(new File(filePath), Charset.defaultCharset()); }
@Before public void setUp() throws IOException { inputFile = new File(TEST_INPUT_DIR + "/UpperCamelCaseTest.swift"); expectedMessages = new ArrayList<>(); outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent, false, Charset.defaultCharset().name())); }
@Test public void sameEncoding_CharsetEncoder_constructor() throws Exception { final CharsetEncoder enc = Charset.defaultCharset().newEncoder(); succesfulRun(new FileWriterWithEncoding(file2, enc)); }
@Test public void testGetLine() throws Exception { final AbstractCheck check = new AbstractCheck() { @Override public int[] getDefaultTokens() { return CommonUtil.EMPTY_INT_ARRAY; } @Override public int[] getAcceptableTokens() { return getDefaultTokens(); } @Override public int[] getRequiredTokens() { return getDefaultTokens(); } }; check.setFileContents(new FileContents(new FileText( new File(getPath("InputAbstractCheckTestFileContents.java")), Charset.defaultCharset().name()))); Assert.assertEquals("Invalid line content", " * I'm a javadoc", check.getLine(3)); }
@Test public void extract() throws IOException { File base = StoreLocation.LOCATION, outDir = folder.newFolder(); int count = 0; try (Store store = new Store(base)) { store.load(); AreaManager areaManager = new AreaManager(store); areaManager.load(); for (AreaDefinition area : areaManager.getAreas()) { Files.write(gson.toJson(area), new File(outDir, area.id + ".json"), Charset.defaultCharset()); ++count; } } logger.info("Dumped {} areas to {}", count, outDir); } }