/** * The encoding as returned by the underlying OutputStreamWriter. Can be the historical name. * * @return the encoding * @see java.io.OutputStreamWriter#getEncoding() */ public String getEncoding() { return out.getEncoding(); }
public StreamingMarkupWriter(final Writer writer, final String encoding, boolean useDoubleQuotes) { this.useDoubleQuotes = useDoubleQuotes; this.writer = writer; if (encoding != null) { this.encoding = encoding; this.encodingKnown = true; } else if (writer instanceof OutputStreamWriter) { this.encoding = getNormalizedEncoding(((OutputStreamWriter) writer).getEncoding()); this.encodingKnown = true; } else if (writer instanceof EncodingAwareBufferedWriter) { this.encoding = getNormalizedEncoding(((EncodingAwareBufferedWriter) writer).getEncoding()); this.encodingKnown = true; } else { this.encoding = "US-ASCII"; this.encodingKnown = false; } this.encoder = Charset.forName(this.encoding).newEncoder(); }
public int read() throws IOException { if (pis.available() > 0) { return pis.read(); } int c = reader.read(); if (c == -1) { return c; } osw.write(c); osw.flush(); pos.flush(); if (pis.available() > 0) { return pis.read(); } else { throw new IOException("Cannot encode input data using " + osw.getEncoding() + " encoding."); // NOI18N } }
@Before public void setUp() throws Exception { final File encodingFinder = new File(getTestDirectory(), "finder.txt"); try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(encodingFinder))) { defaultEncoding = out.getEncoding(); } file1 = new File(getTestDirectory(), "testfile1.txt"); file2 = new File(getTestDirectory(), "testfile2.txt"); final char[] arr = new char[1024]; final char[] chars = "ABCDEFGHIJKLMNOPQabcdefgihklmnopq".toCharArray(); for (int i = 0; i < arr.length; i++) { arr[i] = chars[i % chars.length]; } textContent = new String(arr); }
private void writeNode (Node node) throws IOException { switch (node.getNodeType ()) { case Node.DOCUMENT_NODE: String javaEncoding = writer.getEncoding ();
/** * Returns the encoding being used to write this output stream (i.e., the * encoding of the file). * * @return The encoding of the stream. */ public String getEncoding() { return internalOut.getEncoding(); }
public final static String getDefaultCharSet() throws UnsupportedEncodingException { OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream(), CharsetUtil.UTF_8); String enc = writer.getEncoding(); return enc; }
/** * Because of legacy encodings used by earlier JDK versions, we * need to be careful when accessing encoding names via JDK * classes. */ public static String findEncodingFor(Writer w) { if (w instanceof OutputStreamWriter) { String enc = ((OutputStreamWriter) w).getEncoding(); /* [WSTX-146]: It is important that we normalize this, since * older JDKs return legacy encoding names ("UTF8" instead of * canonical "UTF-8") */ return normalize(enc); } return null; } }
/** * Print an XML declaration before serializing the element. * The specified Map associates prefixes with namespace URLs. */ public static void serializeElementAsDocument(Element el, Map namespaces, Writer writer) { PrintWriter pw = new PrintWriter(writer); String javaEncoding = (writer instanceof OutputStreamWriter) ? ((OutputStreamWriter) writer).getEncoding() : null; String xmlEncoding = java2XMLEncoding(javaEncoding); if (xmlEncoding != null) { pw.println(Constants.XML_DECL_START + xmlEncoding + Constants.XML_DECL_END); } else { pw.println("<?xml version=\"1.0\"?>"); } serializeAsXML(el, namespaces, writer); }
/** * Write the specified WSDL definition to the specified Writer. * * @param wsdlDef the WSDL definition to be written. * @param sink the Writer to write the xml to. */ public void writeWSDL(Definition wsdlDef, Writer sink) throws WSDLException { PrintWriter pw = new PrintWriter(sink); String javaEncoding = (sink instanceof OutputStreamWriter) ? ((OutputStreamWriter)sink).getEncoding() : null; String xmlEncoding = DOM2Writer.java2XMLEncoding(javaEncoding); if (xmlEncoding == null) { throw new WSDLException(WSDLException.CONFIGURATION_ERROR, "Unsupported Java encoding for writing " + "wsdl file: '" + javaEncoding + "'."); } pw.println(Constants.XML_DECL_START + xmlEncoding + Constants.XML_DECL_END); printDefinition(wsdlDef, pw); }
/** * Serialize this node into the writer as XML. * The specified Map associates prefixes with namespace URLs. */ public static void serializeAsXML(Node node, Map namespaces, Writer writer) { ObjectRegistry namespaceStack = new ObjectRegistry(namespaces); namespaceStack.register("xml", NS_URI_XML); PrintWriter pw = new PrintWriter(writer); String javaEncoding = (writer instanceof OutputStreamWriter) ? ((OutputStreamWriter) writer).getEncoding() : null; print(node, namespaceStack, pw, java2XMLEncoding(javaEncoding)); }
@Test public void stdoutAdatperWithAppendNewLine() throws IOException { BufferedWriter bufferedWriter = TestUtils.getPropertyValue(this.newlineAdapterHandler, "writer", BufferedWriter.class); Writer writer = TestUtils.getPropertyValue(bufferedWriter, "out", Writer.class); assertEquals(OutputStreamWriter.class, writer.getClass()); Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding()); assertEquals(Charset.defaultCharset(), writerCharset); Object lock = TestUtils.getPropertyValue(writer, "lock"); assertEquals(System.out, lock); bufferedWriter = Mockito.spy(bufferedWriter); DirectFieldAccessor dfa = new DirectFieldAccessor(this.newlineAdapterHandler); dfa.setPropertyValue("writer", bufferedWriter); this.newlineAdapterHandler.handleMessage(new GenericMessage<String>("bar")); verify(bufferedWriter, times(1)).write(eq("bar")); verify(bufferedWriter, times(1)).newLine(); }
@Test public void stdoutAdapterWithProvidedCharset() throws IOException { BufferedWriter bufferedWriter = TestUtils.getPropertyValue(this.stdoutAdapterWithProvidedCharsetHandler, "writer", BufferedWriter.class); Writer writer = TestUtils.getPropertyValue(bufferedWriter, "out", Writer.class); assertEquals(OutputStreamWriter.class, writer.getClass()); Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding()); assertEquals(Charset.forName("UTF-8"), writerCharset); Object lock = TestUtils.getPropertyValue(writer, "lock"); assertEquals(System.out, lock); bufferedWriter = Mockito.spy(bufferedWriter); DirectFieldAccessor dfa = new DirectFieldAccessor(this.stdoutAdapterWithProvidedCharsetHandler); dfa.setPropertyValue("writer", bufferedWriter); this.stdoutAdapterWithProvidedCharsetHandler.handleMessage(new GenericMessage<String>("bar")); verify(bufferedWriter, times(1)).write(eq("bar")); }
@Test public void stdoutAdapterWithDefaultCharset() throws IOException { BufferedWriter bufferedWriter = TestUtils.getPropertyValue(this.stdoutAdapterWithDefaultCharsetHandler, "writer", BufferedWriter.class); Writer writer = TestUtils.getPropertyValue(bufferedWriter, "out", Writer.class); assertEquals(OutputStreamWriter.class, writer.getClass()); Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding()); assertEquals(Charset.defaultCharset(), writerCharset); Object lock = TestUtils.getPropertyValue(writer, "lock"); assertEquals(System.out, lock); bufferedWriter = Mockito.spy(bufferedWriter); DirectFieldAccessor dfa = new DirectFieldAccessor(this.stdoutAdapterWithDefaultCharsetHandler); dfa.setPropertyValue("writer", bufferedWriter); this.stdoutAdapterWithDefaultCharsetHandler.handleMessage(new GenericMessage<String>("foo")); verify(bufferedWriter, times(1)).write(eq("foo")); assertEquals(23, TestUtils.getPropertyValue(this.stdoutAdapterWithDefaultCharsetHandler, "order")); }
@Test public void stderrAdapter() throws IOException { BufferedWriter bufferedWriter = TestUtils.getPropertyValue(this.stderrAdapterHandler, "writer", BufferedWriter.class); Writer writer = TestUtils.getPropertyValue(bufferedWriter, "out", Writer.class); assertEquals(OutputStreamWriter.class, writer.getClass()); Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding()); assertEquals(Charset.defaultCharset(), writerCharset); Object lock = TestUtils.getPropertyValue(writer, "lock"); assertEquals(System.err, lock); bufferedWriter = Mockito.spy(bufferedWriter); DirectFieldAccessor dfa = new DirectFieldAccessor(this.stderrAdapterHandler); dfa.setPropertyValue("writer", bufferedWriter); this.stderrAdapterHandler.handleMessage(new GenericMessage<String>("bar")); verify(bufferedWriter, times(1)).write(eq("bar")); assertEquals(34, TestUtils.getPropertyValue(this.stderrAdapterHandler, "order")); }
@Test //INT-2275 public void stdoutInsideNestedChain() throws IOException { List<?> handlers = TestUtils.getPropertyValue(this.stdoutChainHandler, "handlers", List.class); assertEquals(2, handlers.size()); Object chainHandler = handlers.get(1); assertTrue(chainHandler instanceof MessageHandlerChain); List<?> nestedChainHandlers = TestUtils.getPropertyValue(chainHandler, "handlers", List.class); assertEquals(1, nestedChainHandlers.size()); Object stdoutHandler = nestedChainHandlers.get(0); assertTrue(stdoutHandler instanceof CharacterStreamWritingMessageHandler); BufferedWriter bufferedWriter = TestUtils.getPropertyValue(stdoutHandler, "writer", BufferedWriter.class); Writer writer = TestUtils.getPropertyValue(bufferedWriter, "out", Writer.class); assertEquals(OutputStreamWriter.class, writer.getClass()); Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding()); assertEquals(Charset.defaultCharset(), writerCharset); Object lock = TestUtils.getPropertyValue(writer, "lock"); assertEquals(System.out, lock); bufferedWriter = Mockito.spy(bufferedWriter); DirectFieldAccessor dfa = new DirectFieldAccessor(stdoutHandler); dfa.setPropertyValue("writer", bufferedWriter); this.stdoutInsideNestedChain.send(new GenericMessage<String>("foo")); verify(bufferedWriter, times(1)).write(eq("foobar")); } }
private void setOutputUsingWriter(Writer writer) throws IOException { fWriter = writer; if (writer instanceof OutputStreamWriter) { String charset = ((OutputStreamWriter) writer).getEncoding(); if (charset != null && !charset.equalsIgnoreCase("utf-8")) { fEncoder = Charset.forName(charset).newEncoder(); } } }
private void setOutputUsingWriter(Writer writer) throws IOException { fWriter = writer; if (writer instanceof OutputStreamWriter) { String charset = ((OutputStreamWriter) writer).getEncoding(); if (charset != null && !charset.equalsIgnoreCase("utf-8")) { fEncoder = Charset.forName(charset).newEncoder(); } } }
private String getDefaultEncodingName() { if (defaultEncodingName == null) { defaultEncodingName = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); } return defaultEncodingName; }
@Override public String getEncoding() { if(!MockFramework.isEnabled()){ return super.getEncoding(); } return stream.getEncoding(); }