List of usage examples for java.nio.charset CodingErrorAction REPLACE
CodingErrorAction REPLACE
To view the source code for java.nio.charset CodingErrorAction REPLACE.
Click Source Link
From source file:org.codehaus.groovy.grails.web.util.StreamByteBuffer.java
public String readAsString(Charset charset) throws CharacterCodingException { int unreadSize = totalBytesUnread(); if (unreadSize > 0) { CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer charbuffer = CharBuffer.allocate(unreadSize); ByteBuffer buf = null;/*from w w w. ja v a2s.com*/ while (prepareRead() != -1) { buf = currentReadChunk.readToNioBuffer(); boolean endOfInput = (prepareRead() == -1); CoderResult result = decoder.decode(buf, charbuffer, endOfInput); if (endOfInput) { if (!result.isUnderflow()) { result.throwException(); } } } CoderResult result = decoder.flush(charbuffer); if (buf.hasRemaining()) { throw new IllegalStateException("There's a bug here, buffer wasn't read fully."); } if (!result.isUnderflow()) { result.throwException(); } charbuffer.flip(); String str; if (charbuffer.hasArray()) { int len = charbuffer.remaining(); char[] ch = charbuffer.array(); if (len != ch.length) { ch = ArrayUtils.subarray(ch, 0, len); } str = StringCharArrayAccessor.createString(ch); } else { str = charbuffer.toString(); } return str; } return null; }
From source file:edu.ehu.galan.cvalue.model.Document.java
/** * Tries to convert the content of this document to UTF-8 using java * CharsetDecoders//from ww w .ja va2 s . c o m */ public void convertToUTF8() { FileInputStream istream = null; Writer out = null; try { istream = new FileInputStream(path); BufferedInputStream in = new BufferedInputStream(istream); CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder(); charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE); charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE); Reader inputReader = new InputStreamReader(in, charsetDecoder); StringWriter writer = new StringWriter(); IOUtils.copy(inputReader, writer); String theString = writer.toString(); FileUtils.deleteQuietly(new File(path)); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")); out.write(theString); out.close(); // System.out.println(""); } catch (FileNotFoundException ex) { logger.error("Error converting the file to utf8", ex); } catch (IOException ex) { logger.error("Error converting the file to utf8", ex); } finally { try { if (out != null) { out.close(); } if (istream != null) { istream.close(); } } catch (IOException ex) { logger.error("Error converting the file to utf8", ex); } } }
From source file:com.puppycrawl.tools.checkstyle.api.FileText.java
/** * Creates a new file text representation. * * <p>The file will be read using the specified encoding, replacing * malformed input and unmappable characters with the default * replacement character.//from ww w.j ava 2 s .c om * * @param file the name of the file * @param charsetName the encoding to use when reading the file * @throws NullPointerException if the text is null * @throws IOException if the file could not be read */ public FileText(File file, String charsetName) throws IOException { this.file = file; // We use our own decoder, to be sure we have complete control // about replacements. final CharsetDecoder decoder; try { charset = Charset.forName(charsetName); decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } catch (final UnsupportedCharsetException ex) { final String message = "Unsupported charset: " + charsetName; final UnsupportedEncodingException ex2 = new UnsupportedEncodingException(message); ex2.initCause(ex); throw ex2; } fullText = readFile(file, decoder); // Use the BufferedReader to break down the lines as this // is about 30% faster than using the // LINE_TERMINATOR.split(fullText, -1) method final ArrayList<String> textLines = new ArrayList<>(); final BufferedReader reader = new BufferedReader(new StringReader(fullText)); while (true) { final String line = reader.readLine(); if (line == null) { break; } textLines.add(line); } lines = textLines.toArray(new String[textLines.size()]); }
From source file:gov.nasa.ensemble.dictionary.nddl.ModelGenerator.java
private static StringBuilder readModelFile(File file) throws IOException { StringBuilder result = new StringBuilder(); FileInputStream fis = null;/*from w w w .j av a 2 s. c o m*/ InputStreamReader reader = null; int size; try { char[] buffer = new char[1024]; fis = new FileInputStream(file); FileWriter filewriter = new FileWriter("out"); String encname = filewriter.getEncoding(); filewriter.close(); Charset cs = Charset.forName(encname); CharsetDecoder csd = cs.newDecoder(); csd.onMalformedInput(CodingErrorAction.REPLACE); reader = new InputStreamReader(fis, csd); while ((size = reader.read(buffer, 0, 1024)) > 0) { result.append(buffer, 0, size); } } catch (Exception e) { // System.out.println(encoding); e.printStackTrace(); } finally { if (fis != null) { fis.close(); } if (reader != null) { reader.close(); } } return result; }
From source file:fi.jyu.student.jatahama.onlineinquirytool.server.LoadSaveServlet.java
private String formSafeAscii(String utfName) { asciiEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE); try {// ww w.jav a 2 s .com String ascii = new String(asciiEncoder.encode(CharBuffer.wrap(utfName)).array()).replaceAll("\"", "_"); return ascii; } catch (CharacterCodingException e) { log.warning("asciiEncoder failure: " + e.getMessage()); // Fallback to default name return defaultFilename; } }
From source file:com.examples.with.different.packagename.idnaming.ReaderInputStream.java
/** * Construct a new {@link ReaderInputStream}. * // w w w.ja v a 2 s .c om * @param reader the target {@link Reader} * @param charset the charset encoding * @param bufferSize the size of the input buffer in number of characters */ public ReaderInputStream(Reader reader, Charset charset, int bufferSize) { this(reader, charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
From source file:com.hypersocket.ui.ReaderInputStream.java
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charset the charset encoding/* w ww .j av a2 s. co m*/ * @param bufferSize the size of the input buffer in number of characters */ public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { this(reader, charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
From source file:org.gradle.testkit.runner.internal.io.WriterOutputStream.java
/** * Constructs a new {@link WriterOutputStream}. * * @param writer the target {@link Writer} * @param charset the charset encoding// w w w . java2 s. c o m * @param bufferSize the size of the output buffer in number of characters * @param writeImmediately If <tt>true</tt> the output buffer will be flushed after each * write operation, i.e. all available data will be written to the * underlying {@link Writer} immediately. If <tt>false</tt>, the * output buffer will only be flushed when it overflows or when * {@link #flush()} or {@link #close()} is called. */ public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) { this(writer, charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith("?"), bufferSize, writeImmediately); }
From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java
public JuxtaXMLParser(File file, String juxtaVersion, Charset encodingCharSet) throws FileNotFoundException { this.docType = DocumentType.UNKNOWN; this.juxtaVersion = juxtaVersion; this.encodingCharSet = encodingCharSet; this.decoder = this.encodingCharSet.newDecoder(); this.decoder.onMalformedInput(CodingErrorAction.REPLACE); this.decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); this.idOffsetMap = new HashMap<String, OffsetRange>(); this.file = file; this.revisionsToAccept = new ArrayList<Integer>(); this.currentRevisionIndex = 0; root = current = null;/*from ww w. j a v a2s . co m*/ flattenedText = new StringBuffer(); flatTextPosition = -1; xmlTextPosition = -1; lastXMLPosition = 0; excludeElements = new TagSet(); notableElements = new TagSet(); newlineElements = new TagSet(); elementsEncountered = new HashSet<String>(); lineToOffsetMap = new ArrayList<Integer>(); insideEntity = false; insideWhitespaceRun = false; // install our default special-case tag handlers tagHandlers = new LinkedList<JuxtaXMLParserTagHandler>(); biblioTagHandler = new JuxtaXMLParserBibliographicTagHandler(); addTagHandler(biblioTagHandler); docPointerHandler = new JuxtaXMLParserDocumentPointerTagHandler(); addTagHandler(docPointerHandler); milestoneTagHandler = new JuxtaXMLParserMilestoneTagHandler(); addTagHandler(milestoneTagHandler); }
From source file:edu.cmu.cs.lti.util.general.BasicConvenience.java
/** * Returns a reader for the specified file using the specified encoding. Any characters in the * input that are malformed or invalid for that encoding are replaced with the specified * substitution string./*from www. j av a2 s . co m*/ * * @param f * @param encoding * @param substitution * @return * @throws FileNotFoundException */ public static BufferedReader getEncodingSafeBufferedReader(File f, String encoding, String substitution) throws FileNotFoundException { BufferedReader cin; Charset cs = Charset.forName(encoding); CharsetDecoder decoder = cs.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith(substitution); InputStream is = new FileInputStream(f); if (f.toString().endsWith(".zip")) is = new ZipInputStream(is); cin = new BufferedReader(new InputStreamReader(is, decoder)); return cin; }