List of usage examples for java.nio.charset CodingErrorAction IGNORE
CodingErrorAction IGNORE
To view the source code for java.nio.charset CodingErrorAction IGNORE.
Click Source Link
From source file:ChannelToWriter.java
/** * Read bytes from the specified channel, decode them using the specified * Charset, and write the resulting characters to the specified writer *///ww w. ja v a 2 s . c o m public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException { // Get and configure the CharsetDecoder we'll use CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); // Get the buffers we'll use, and the backing array for the CharBuffer. ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024); CharBuffer chars = CharBuffer.allocate(2 * 1024); char[] array = chars.array(); while (channel.read(bytes) != -1) { // Read from channel until EOF bytes.flip(); // Switch to drain mode for decoding // Decode the byte buffer into the char buffer. // Pass false to indicate that we're not done. decoder.decode(bytes, chars, false); // Put the char buffer into drain mode, and write its contents // to the Writer, reading them from the backing array. chars.flip(); writer.write(array, chars.position(), chars.remaining()); // Discard all bytes we decoded, and put the byte buffer back into // fill mode. Since all characters were output, clear that buffer. bytes.compact(); // Discard decoded bytes chars.clear(); // Clear the character buffer } // At this point there may still be some bytes in the buffer to decode // So put the buffer into drain mode call decode() a final time, and // finish with a flush(). bytes.flip(); decoder.decode(bytes, chars, true); // True means final call decoder.flush(chars); // Flush any buffered chars // Write these final chars (if any) to the writer. chars.flip(); writer.write(array, chars.position(), chars.remaining()); writer.flush(); }
From source file:com.stratio.parsers.XMLDumpParser.java
/** * * Based on wikixmlj by Delip Rao and Jason Smith. * Licensed under the Apache License 2.0. * Available at http://code.google.com/p/wikixmlj/ * * @return// w w w . j av a 2 s . c om * @throws FileNotFoundException * @throws IOException */ protected InputSource getInputSource() throws FileNotFoundException, IOException { if (inStream != null) { return new InputSource(inStream); } FileInputStream fis = new FileInputStream(dumpFile); InputStream is; InputStreamReader isr; if (dumpFile.endsWith(".gz")) { is = new GZIPInputStream(fis); } else if (dumpFile.endsWith(".bz2")) { byte[] ignoreBytes = new byte[2]; fis.read(ignoreBytes); //"B", "Z" bytes from commandline tools is = new CBZip2InputStream(fis); } else { is = fis; } CharsetDecoder decoder = Charsets.UTF_8.newDecoder(); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.replaceWith("?"); isr = new InputStreamReader(is, decoder); return new InputSource(isr); //return new InputSource(new BufferedReader(isr)); }
From source file:com.simba.demo.airplay.lab.SimbaHttpServer.java
@Override public void run() { // HTTP parameters for the server HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024); params.setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpTest/1.1"); params.setParameter(CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION, CodingErrorAction.IGNORE); // Create HTTP protocol processing chain HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] { // Use standard server-side protocol interceptors new ResponseDate(), new ResponseServer(), new ResponseContent(), new ResponseConnControl() }); // Create request handler registry HttpAsyncRequestHandlerRegistry reqistry = new HttpAsyncRequestHandlerRegistry(); // Register the default handler for all URIs reqistry.register("*", new SimbaHttpHandler()); // Create server-side HTTP protocol handler HttpAsyncService protocolHandler = new HttpAsyncService(httpproc, new DefaultConnectionReuseStrategy(), reqistry, params) {//from w ww. j av a2 s.co m @Override public void connected(final NHttpServerConnection conn) { LOGD(conn + ": connection open"); super.connected(conn); } @Override public void closed(final NHttpServerConnection conn) { LOGD(conn + ": connection closed"); super.closed(conn); } }; // Create HTTP connection factory NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory = new DefaultNHttpServerConnectionFactory( params); // Create server-side I/O event dispatch IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(protocolHandler, connFactory); // Create server-side I/O reactor try { ListeningIOReactor ioReactor = new DefaultListeningIOReactor(); // Listen of the given port InetSocketAddress isa = new InetSocketAddress(mPort); ioReactor.listen(isa); // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { LOGE("Interrupted"); } catch (IOException e) { LOGE("I/O error: " + e.getMessage()); } LOGD("Shutdown"); }
From source file:com.ea.core.bridge.ws.rest.client.AbstractRestClient.java
public AbstractRestClient(URL httpUrl) { super(httpUrl); HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() { @Override//from w ww . jav a2s.c om public HttpMessageParser<HttpResponse> create(SessionInputBuffer buffer, MessageConstraints constraints) { LineParser lineParser = new BasicLineParser() { @Override public Header parseHeader(final CharArrayBuffer buffer) { try { return super.parseHeader(buffer); } catch (ParseException ex) { return new BasicHeader(buffer.toString(), null); } } }; return new DefaultHttpResponseParser(buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints) { @Override protected boolean reject(final CharArrayBuffer line, int count) { // try to ignore all garbage preceding a status line infinitely return false; } }; } }; HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); SSLContext sslcontext = SSLContexts.createSystemDefault(); X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost") || host.equalsIgnoreCase("localhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return super.resolve(host); } } }; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory, dnsResolver); SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build(); connManager.setDefaultSocketConfig(socketConfig); connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200) .setMaxLineLength(2000).build(); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints).build(); connManager.setDefaultConnectionConfig(connectionConfig); connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); connManager.setMaxTotal(100); connManager.setDefaultMaxPerRoute(10); connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); CookieStore cookieStore = new BasicCookieStore(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setConnectionRequestTimeout(3000) .setConnectTimeout(3000).setSocketTimeout(3000).build(); client = HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) // .setProxy(new HttpHost("myproxy", 8080)) .setDefaultRequestConfig(defaultRequestConfig).build(); }
From source file:com.helger.pd.client.jdk6.PDClient.java
@Nonnull protected HttpClientBuilder createClientBuilder() { SSLConnectionSocketFactory aSSLSocketFactory = null; try {//from w w w. j av a 2 s .com // Set SSL context final KeyStore aKeyStore = KeyStoreHelper.loadKeyStore(PDClientConfiguration.getKeyStorePath(), PDClientConfiguration.getKeyStorePassword()); final SSLContext aSSLContext = SSLContexts.custom().loadKeyMaterial(aKeyStore, PDClientConfiguration.getKeyStoreKeyPassword(), new PrivateKeyStrategy() { public String chooseAlias(final Map<String, PrivateKeyDetails> aAliases, final Socket aSocket) { final String sAlias = PDClientConfiguration.getKeyStoreKeyAlias(); return aAliases.containsKey(sAlias) ? sAlias : null; } }).build(); // Allow TLSv1 protocol only aSSLSocketFactory = new SSLConnectionSocketFactory(aSSLContext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); } catch (final Throwable t) { s_aLogger.error("Failed to initialize keystore for service connection! Can only use http now!", t); } try { final RegistryBuilder<ConnectionSocketFactory> aRB = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()); if (aSSLSocketFactory != null) aRB.register("https", aSSLSocketFactory); final Registry<ConnectionSocketFactory> sfr = aRB.build(); final PoolingHttpClientConnectionManager aConnMgr = new PoolingHttpClientConnectionManager(sfr); aConnMgr.setDefaultMaxPerRoute(100); aConnMgr.setMaxTotal(200); aConnMgr.setValidateAfterInactivity(1000); final ConnectionConfig aConnectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build(); aConnMgr.setDefaultConnectionConfig(aConnectionConfig); return HttpClientBuilder.create().setConnectionManager(aConnMgr); } catch (final Exception ex) { throw new InitializationException("Failed to init HTTP client", ex); } }
From source file:edu.utsa.sifter.som.MainSOM.java
public static void main(String[] args) throws IOException, InterruptedException, CorruptIndexException, NoSuchFieldException { final File evPath = new File(args[0]); final File idxPath = new File(evPath, "primary-idx"); final long begin = System.currentTimeMillis(); // createIndex(path); final Path outPath = new Path(new Path(evPath.toString()), "docVectors.seq"); final Configuration hadoopConf = new Configuration(); final LocalFileSystem fs = FileSystem.getLocal(hadoopConf); final SequenceFile.Writer file = SequenceFile.createWriter(fs, hadoopConf, outPath, LongWritable.class, IntArrayWritable.class); final DirectoryReader dirReader = DirectoryReader.open(FSDirectory.open(idxPath)); final SifterConfig conf = new SifterConfig(); InputStream xmlProps = null;// ww w .ja va 2 s . com try { xmlProps = new FileInputStream("sifter_props.xml"); } catch (FileNotFoundException ex) { ; // swallow exeption } conf.loadFromXML(xmlProps); // safe with null final MainSOM builder = new MainSOM(dirReader, conf); IndexWriter writer = null; FileOutputStream somJSFile = null; try { builder.initTerms(); builder.writeVectors(file); file.close(); final SequenceFile.Reader seqRdr = new SequenceFile.Reader(fs, outPath, hadoopConf); writer = builder.createWriter(new File(evPath, "som-idx"), conf); somJSFile = new FileOutputStream(new File(evPath, "som.js")); final CharsetEncoder utf8 = Charset.forName("UTF-8").newEncoder(); utf8.onMalformedInput(CodingErrorAction.IGNORE); final Writer somJS = new BufferedWriter(new OutputStreamWriter(somJSFile, utf8)); builder.makeSOM(conf, seqRdr, writer, somJS); writer.forceMerge(1); } catch (Exception e) { e.printStackTrace(System.err); } finally { file.close(); if (writer != null) { writer.close(); } if (somJSFile != null) { somJSFile.close(); } dirReader.close(); System.out.println("Number of docs written: " + builder.getNumDocs()); System.out.println("Number of outlier docs: " + builder.getNumOutliers()); System.out.println("Total term dimensions: " + builder.getTermsMap().size()); System.out.println("Max terms per doc: " + builder.getMaxDocTerms()); System.out.println("Avg terms per doc: " + builder.getAvgDocTerms()); System.out.println("Duration: " + ((System.currentTimeMillis() - begin) / 1000) + " seconds"); conf.storeToXML(new FileOutputStream("sifter_props.xml")); } }
From source file:io.mandrel.requests.http.ApacheHttpRequester.java
public void init() { available = new Semaphore(maxParallel(), true); SSLContext sslContext = SSLContexts.createSystemDefault(); HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(); Registry<ConnectionSocketFactory> sessionStrategyRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override/*w w w. j a v a 2 s . com*/ public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("localhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return new InetAddress[] { nameResolver().resolve(host) }; } } }; // Create a connection manager with custom configuration. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( sessionStrategyRegistry, dnsResolver); // Create message constraints MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(maxHeaderCount) .setMaxLineLength(maxLineLength).build(); // Create connection configuration ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints).build(); connManager.setDefaultConnectionConfig(connectionConfig); // Configure total max or per route limits for persistent connections // that can be kept in the pool or leased by the connection manager. connManager.setMaxTotal(maxPersistentConnections()); connManager.setDefaultMaxPerRoute(maxPersistentConnections()); // TODO // Use custom credentials provider if necessary. // CredentialsProvider credentialsProvider = new // BasicCredentialsProvider(); // Create global request configuration defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setMaxRedirects(maxRedirects()) .setSocketTimeout(socketTimeout()).setConnectTimeout(connectTimeout()) .setConnectionRequestTimeout(requestTimeOut()).setRedirectsEnabled(followRedirects()).build(); // Create an HttpClient with the given custom dependencies and // configuration. client = HttpClients.custom().setConnectionManager(connManager) // .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(defaultRequestConfig).build(); }
From source file:com.github.lpezet.antiope.dao.DefaultHttpClientFactory.java
@Override public HttpClient createHttpClient(APIConfiguration pConfiguration) { // Use a custom connection factory to customize the process of // initialization of outgoing HTTP connections. Beside standard connection // configuration parameters HTTP connection factory can define message // parser / writer routines to be employed by individual connections. HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> oConnFactory = new ManagedHttpClientConnectionFactory( new DefaultHttpRequestWriterFactory(), new DefaultHttpResponseParserFactory()); SSLContext oSslContext = null; X509HostnameVerifier oHostnameVerifier = null; if (pConfiguration.isCheckSSLCertificates()) { oSslContext = SSLContexts.createSystemDefault(); oHostnameVerifier = new BrowserCompatHostnameVerifier(); } else {//ww w .j ava2 s. c o m final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) { } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager try { final SSLContext sslContext = SSLContext.getInstance(SSL); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager //final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); oSslContext = sslContext; } catch (NoSuchAlgorithmException e) { throw new APIClientException(e); } catch (KeyManagementException e) { throw new APIClientException(e); } oHostnameVerifier = new AllowAllHostnameVerifier(); } // Create a registry of custom connection socket factories for supported // protocol schemes. Registry<ConnectionSocketFactory> oSocketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register(HTTP, PlainConnectionSocketFactory.INSTANCE) .register(HTTPS, new SSLConnectionSocketFactory(oSslContext, oHostnameVerifier)).build(); // Use custom DNS resolver to override the system DNS resolution. DnsResolver oDnsResolver = new SystemDefaultDnsResolver(); /* { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return super.resolve(host); } } };*/ // Create a connection manager with custom configuration. PoolingHttpClientConnectionManager oConnManager = new PoolingHttpClientConnectionManager( oSocketFactoryRegistry, oConnFactory, oDnsResolver); // Create socket configuration SocketConfig oSocketConfig = SocketConfig.custom().setTcpNoDelay(true) .setSoTimeout(pConfiguration.getSocketTimeout()).build(); // Configure the connection manager to use socket configuration either // by default or for a specific host. oConnManager.setDefaultSocketConfig(oSocketConfig); // connManager.setSocketConfig(new HttpHost("somehost", 80), oSocketConfig); // Create message constraints MessageConstraints oMessageConstraints = MessageConstraints.custom().setMaxHeaderCount(200) .setMaxLineLength(2000).build(); // Create connection configuration ConnectionConfig oConnectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8) .setMessageConstraints(oMessageConstraints).build(); // Configure the connection manager to use connection configuration either // by default or for a specific host. oConnManager.setDefaultConnectionConfig(oConnectionConfig); // connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); // Configure total max or per route limits for persistent connections // that can be kept in the pool or leased by the connection manager. oConnManager.setMaxTotal(100); oConnManager.setDefaultMaxPerRoute(10); //oConnManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); // Use custom cookie store if necessary. CookieStore oCookieStore = new BasicCookieStore(); // Use custom credentials provider if necessary. // // Create global request configuration RequestConfig oDefaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) //.setExpectContinueEnabled(true) // WARNING: setting it to true slows things down by 4s!!!! .setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .setConnectTimeout(pConfiguration.getConnectionTimeout()).build(); CredentialsProvider oCredentialsProvider = new BasicCredentialsProvider(); HttpHost oProxy = null; if (pConfiguration.getProxyHost() != null && pConfiguration.getProxyPort() > 0) { String proxyHost = pConfiguration.getProxyHost(); int proxyPort = pConfiguration.getProxyPort(); String proxyUsername = pConfiguration.getProxyUsername(); String proxyPassword = pConfiguration.getProxyPassword(); String proxyDomain = pConfiguration.getProxyDomain(); String proxyWorkstation = pConfiguration.getProxyWorkstation(); oProxy = new HttpHost(proxyHost, proxyPort); if (proxyUsername != null && proxyPassword != null) { oCredentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); } } // Create an HttpClient with the given custom dependencies and configuration. CloseableHttpClient oHttpClient = HttpClients.custom().setConnectionManager(oConnManager) .setDefaultCookieStore(oCookieStore).setDefaultCredentialsProvider(oCredentialsProvider) .setProxy(oProxy).setDefaultRequestConfig(oDefaultRequestConfig).build(); return oHttpClient; /* RequestConfig oRequestConfig = RequestConfig.custom() .setConnectTimeout(pConfiguration.getConnectionTimeout()) .setSocketTimeout(pConfiguration.getSocketTimeout()) .setStaleConnectionCheckEnabled(true) .build(); */ }
From source file:modnlp.capte.AlignerUtils.java
public static String removeLongWhiteSpace(String s) { s = s.trim();/* w w w . java2 s .c o m*/ while (s.contains(" ")) // two white spaces { s = s.replaceAll(" ", " "); // the first arg should contain 2 spaces, the second only 1 } //System.out.println("Length before UTF8 cleaning " + s.length()); CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); try { ByteBuffer bytes; bytes = ByteBuffer.wrap(s.getBytes()); CharBuffer parsed = utf8Decoder.decode(bytes); s = parsed.toString(); } catch (Exception e) { e.printStackTrace(); } //This code removes all of the bytes with value of zero // Gerard 21st March 2012: Solved, bug with random spaces in segments byte[] b = s.getBytes(); Vector<Byte> vb = new Vector(); for (int i = 0; i < b.length; i++) { if (b[i] != 0) { vb.add(b[i]); } } Object[] ba = vb.toArray(); Byte[] bya = new Byte[ba.length]; byte[] byta = new byte[ba.length]; for (int i = 0; i < ba.length; i++) { bya[i] = (Byte) ba[i]; byta[i] = bya[i].byteValue(); } s = new String(byta); return s; }