List of usage examples for java.io InputStreamReader read
public int read(char cbuf[], int offset, int length) throws IOException
From source file:com.knowgate.dfs.FileSystem.java
/** * <p>Convert a text file from one character set to another</p> * The input file is overwritten./* w w w . jav a2 s. co m*/ * @param sFilePath File Path * @param sOldCharset Original file character set * @param sNewCharset New character set * @throws FileNotFoundException * @throws IOException * @throws UnsupportedEncodingException */ public static void convert(String sFilePath, String sOldCharset, String sNewCharset) throws FileNotFoundException, IOException, UnsupportedEncodingException { if (DebugFile.trace) { DebugFile .writeln("Begin FileSystem.convert(" + sFilePath + "," + sOldCharset + "," + sNewCharset + ")"); DebugFile.incIdent(); } final int iBufferSize = 8192; int iReaded; char[] cBuffer = new char[iBufferSize]; FileInputStream oFileStrm = new FileInputStream(sFilePath); BufferedInputStream oInStrm = new BufferedInputStream(oFileStrm); InputStreamReader oStrm = new InputStreamReader(oInStrm, sOldCharset); FileOutputStream oOutStrm = new FileOutputStream(sFilePath + ".tmp"); while (true) { iReaded = oStrm.read(cBuffer, 0, iBufferSize); if (-1 == iReaded) break; oOutStrm.write(new String(cBuffer, 0, iReaded).getBytes(sNewCharset)); } oOutStrm.close(); oStrm.close(); oInStrm.close(); oFileStrm.close(); File oOld = new File(sFilePath); oOld.delete(); File oNew = new File(sFilePath + ".tmp"); oNew.renameTo(oOld); if (DebugFile.trace) { DebugFile.decIdent(); DebugFile.writeln("End FileSystem.convert()"); } }
From source file:com.mibr.android.intelligentreminder.INeedToo.java
public synchronized void writeLogTo(OutputStream out) throws FileNotFoundException, IOException { FileInputStream fis = null;/* w w w .j ava2s. c o m*/ fis = getLogInputStream(); java.io.InputStreamReader isr = new InputStreamReader(fis); PrintWriter pw = new PrintWriter(out); char[] cc = new char[4096]; try { int cnt = isr.read(cc, 0, 4096); int c = 0; while (cnt > 0) { pw.write(cc, 0, cnt); cnt = isr.read(cc, 0, 4096); } } finally { try { isr.close(); } catch (Exception e) { } try { pw.flush(); } catch (Exception e2) { } try { pw.close(); } catch (Exception e2) { } } }
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
protected String processEAVMapping(InputStream inputStream) { try {//w w w . j av a 2 s . c om final InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8"); final char[] chars = new char[500]; int readNum = 0; final StringBuilder sb = new StringBuilder(); while ((readNum = reader.read(chars, 0, 500)) > 0) { sb.append(chars, 0, readNum); } String eav = sb.toString(); eav = eav.replaceAll(HbConstants.EAV_TABLE_PREFIX_PARAMETER_REGEX, getPersistenceOptions().getSQLTableNamePrefix()); final boolean extraLazy = getPersistenceOptions().isFetchAssociationExtraLazy(); eav = eav.replaceAll(HbConstants.EAV_COLLECTIONLAZY_REGEX, (extraLazy ? "extra" : "false")); return eav; } catch (Exception e) { throw new IllegalArgumentException(e); } }
From source file:com.knowgate.dfs.FileSystem.java
/** * <p>Read a text file into a String</p> * @param sFilePath Full path of file to be readed. Like "file:///tmp/myfile.txt" or "ftp://myhost:21/dir/myfile.txt" * @param sEncoding Text Encoding for file {UTF-8, ISO-8859-1, ...}<BR> * if <b>null</b> then if first two bytes of file are FF FE then UTF-8 will be assumed<BR> * else ISO-8859-1 will be assumed.//from w w w.j a v a2s .c om * @return String with full contents of file * @throws FileNotFoundException * @throws IOException * @throws OutOfMemoryError * @throws MalformedURLException * @throws FTPException */ public String readfilestr(String sFilePath, String sEncoding) throws MalformedURLException, FTPException, FileNotFoundException, IOException, OutOfMemoryError { if (DebugFile.trace) { DebugFile.writeln("Begin FileSystem.readfilestr(" + sFilePath + "," + sEncoding + ")"); DebugFile.incIdent(); } String sRetVal; String sLower = sFilePath.toLowerCase(); if (sLower.startsWith("file://")) sFilePath = sFilePath.substring(7); if (sLower.startsWith("http://") || sLower.startsWith("https://")) { URL oUrl = new URL(sFilePath); if (user().equals("anonymous") || user().length() == 0) { ByteArrayOutputStream oStrm = new ByteArrayOutputStream(); DataHandler oHndlr = new DataHandler(oUrl); oHndlr.writeTo(oStrm); sRetVal = oStrm.toString(sEncoding); oStrm.close(); } else { if (null == oHttpCli) { oHttpCli = new DefaultHttpClient(); } // fi (oHttpCli) oHttpCli.getCredentialsProvider().setCredentials( new AuthScope(oUrl.getHost(), AuthScope.ANY_PORT, realm()), new UsernamePasswordCredentials(user(), password())); HttpGet oGet = null; try { oGet = new HttpGet(sFilePath); HttpResponse oResp = oHttpCli.execute(oGet); HttpEntity oEnty = oResp.getEntity(); int nLen = (int) oEnty.getContentLength(); if (nLen > 0) { byte[] aRetVal = new byte[nLen]; InputStream oBody = oEnty.getContent(); oBody.read(aRetVal, 0, nLen); oBody.close(); sRetVal = new String(aRetVal, sEncoding); } else { sRetVal = ""; } // fi } finally { oHttpCli.getConnectionManager().shutdown(); oHttpCli = null; } } // fi } else if (sLower.startsWith("ftp://")) { FTPClient oFTPC = null; boolean bFTPSession = false; splitURI(sFilePath); try { if (DebugFile.trace) DebugFile.writeln("new FTPClient(" + sHost + ")"); oFTPC = new FTPClient(sHost); if (DebugFile.trace) DebugFile.writeln("FTPClient.login(" + sUsr + "," + sPwd + ")"); oFTPC.login(sUsr, sPwd); bFTPSession = true; if (DebugFile.trace) DebugFile.writeln("FTPClient.chdir(" + sPath + ")"); oFTPC.chdir(sPath); ByteArrayOutputStream oStrm = new ByteArrayOutputStream(); oFTPC.setType(FTPTransferType.BINARY); if (DebugFile.trace) DebugFile.writeln("FTPClient.get(" + sPath + sFile + "," + sFile + ",false)"); oFTPC.get(oStrm, sFile); sRetVal = oStrm.toString(sEncoding); oStrm.close(); } catch (FTPException ftpe) { throw new FTPException(ftpe.getMessage()); } finally { if (DebugFile.trace) DebugFile.writeln("FTPClient.quit()"); try { if (bFTPSession) oFTPC.quit(); } catch (Exception ignore) { } } } else { File oFile = new File(sFilePath); int iFLen = (int) oFile.length(); if (iFLen > 0) { byte byBuffer[] = new byte[3]; char aBuffer[] = new char[iFLen]; BufferedInputStream oBfStrm; FileInputStream oInStrm; InputStreamReader oReader; if (sEncoding == null) { oInStrm = new FileInputStream(oFile); oBfStrm = new BufferedInputStream(oInStrm, iFLen); sEncoding = new CharacterSetDetector().detect(oBfStrm, "ISO-8859-1"); if (DebugFile.trace) DebugFile.writeln("encoding is " + sEncoding); oBfStrm.close(); oInStrm.close(); } // fi oInStrm = new FileInputStream(oFile); oBfStrm = new BufferedInputStream(oInStrm, iFLen); oReader = new InputStreamReader(oBfStrm, sEncoding); int iReaded = oReader.read(aBuffer, 0, iFLen); // Skip FF FE character mark for Unidode files int iSkip = ((int) aBuffer[0] == 65279 || (int) aBuffer[0] == 65533 || (int) aBuffer[0] == 65534 ? 1 : 0); oReader.close(); oBfStrm.close(); oInStrm.close(); oReader = null; oInStrm = null; oFile = null; sRetVal = new String(aBuffer, iSkip, iReaded - iSkip); } else sRetVal = ""; } // fi (iFLen>0) if (DebugFile.trace) { DebugFile.decIdent(); DebugFile.writeln("End FileSystem.readfilestr() : " + String.valueOf(sRetVal.length())); } return sRetVal; }
From source file:com.naryx.tagfusion.cfm.xml.ws.javaplatform.DynamicWebServiceStubGenerator.java
@SuppressWarnings("deprecation") private String getWSDLContents(String wsdlURL, CallParameters cp) throws cfmRunTimeException { try {//from ww w .j av a 2 s . co m String wsdlL = wsdlURL.toLowerCase(); String contents = null; if (wsdlL.startsWith("<?xml version")) { // The location is the WSDL itself (unexpected) contents = wsdlURL; } else { InputStream is = null; InputStreamReader isr = null; StringBuilder buffy = null; HttpGet method = null; try { if (wsdlL.startsWith("http:") || wsdlL.startsWith("https:")) { // Read from network DefaultHttpClient client = new DefaultHttpClient(); // Set the timeout int timeout = cp.getTimeout(); client.getParams().setParameter("http.connection.timeout", timeout); client.getParams().setParameter("http.socket.timeout", timeout); if (cp.getUsername() != null || cp.getPassword() != null) { // Set any credentials client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(cp.getUsername(), cp.getPassword())); } if (cp.getProxyServer() != null) { // Set the proxy HttpHost proxy = new HttpHost(cp.getProxyServer(), (cp.getProxyPort() == -1) ? cp.getDefaultProxyPort() : cp.getProxyPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (cp.getProxyUser() != null || cp.getProxyPassword() != null) { // Set the proxy credentials client.getCredentialsProvider().setCredentials( new AuthScope(cp.getProxyServer(), cp.getProxyPort()), new UsernamePasswordCredentials(cp.getProxyUser(), cp.getProxyPassword())); } } // Create the method and get the response method = new HttpGet(wsdlURL); client.getParams().setParameter("http.protocol.handle-redirects", true); HttpResponse response = client.execute(method); switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: throw new cfmRunTimeException( catchDataFactory.extendedException("errorCode.runtimeError", "Failed to access WSDL: " + wsdlURL + ". Proxy authentication is required.", response.getStatusLine().toString())); case HttpStatus.SC_UNAUTHORIZED: throw new cfmRunTimeException( catchDataFactory.extendedException("errorCode.runtimeError", "Failed to access WSDL: " + wsdlURL + ". Authentication is required.", response.getStatusLine().toString())); case HttpStatus.SC_USE_PROXY: throw new cfmRunTimeException( catchDataFactory.extendedException("errorCode.runtimeError", "Failed to access WSDL: " + wsdlURL + ". The use of a proxy is required.", response.getStatusLine().toString())); } if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw new cfmRunTimeException(catchDataFactory.extendedException( "errorCode.runtimeError", "Failed to access WSDL: " + wsdlURL, response.getStatusLine().toString())); is = response.getEntity().getContent(); } else { // Just try to read off disk File f = new File(wsdlURL); is = new FileInputStream(f); } // Read the data char[] buf = new char[4096]; int read = -1; buffy = new StringBuilder(); isr = new InputStreamReader(is); while ((read = isr.read(buf, 0, buf.length)) != -1) buffy.append(buf, 0, read); contents = buffy.toString(); } finally { if (isr != null) isr.close(); if (is != null) is.close(); if (method != null) method.releaseConnection(); } } // Calc the sum and return return contents; } catch (IOException ex) { throw new cfmRunTimeException( catchDataFactory.generalException("errorCode.runtimeError", "Failed to access WSDL located at " + wsdlURL + ". There may be an error in the target WSDL. " + ex.getMessage())); } }