public static String getTxtHistory(File file) { String res = ""; if (!file.exists()) { return ""; } try { FileInputStream fin = new FileInputStream(file.getAbsolutePath()); byte[] buffer = new byte[fin.available()]; fin.read(buffer); res = new StringBuilder(String.valueOf(res)).append(EncodingUtils.getString(buffer, "UTF-8")).toString(); fin.close(); } catch (Exception e) { e.printStackTrace(); } return res; }
String fileTxtRead(String fileName) { String res=""; try { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte [] buffer = new byte[length]; fis.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fis.close(); } catch (IOException e) { e.printStackTrace(); } return res; }
public static String readCountryFromAsset(Context context, String assetName) { String content = ""; try { InputStream is = context.getAssets().open(assetName); if (is == null) { return content; } DataInputStream dIs = new DataInputStream(is); byte[] buffer = new byte[dIs.available()]; dIs.read(buffer); content = EncodingUtils.getString(buffer, "UTF-8"); is.close(); return content; } catch (IOException e) { e.printStackTrace(); return content; } }
/** Constructor to use when message contents are known */ NTLMMessage(String messageBody, int expectedType) throws NTLMEngineException { messageContents = Base64.decodeBase64(EncodingUtils.getBytes(messageBody, DEFAULT_CHARSET)); // Look for NTLM message if (messageContents.length < SIGNATURE.length) throw new NTLMEngineException("NTLM message decoding error - packet too short"); int i = 0; while (i < SIGNATURE.length) { if (messageContents[i] != SIGNATURE[i]) throw new NTLMEngineException( "NTLM message expected - instead got unrecognized bytes"); i++; } // Check to be sure there's a type 2 message indicator next int type = readULong(SIGNATURE.length); if (type != expectedType) throw new NTLMEngineException("NTLM type " + Integer.toString(expectedType) + " message expected - instead got type " + Integer.toString(type)); currentOutputPosition = messageContents.length; }
/** * Handles the given exception and generates an HTTP response to be sent * back to the client to inform about the exceptional condition encountered * in the course of the request processing. * * @param ex the exception. * @param response the HTTP response. */ protected void handleException(final HttpException ex, final HttpResponse response) { if (ex instanceof MethodNotSupportedException) { response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); } else if (ex instanceof UnsupportedHttpVersionException) { response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED); } else if (ex instanceof ProtocolException) { response.setStatusCode(HttpStatus.SC_BAD_REQUEST); } else { response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } String message = ex.getMessage(); if (message == null) { message = ex.toString(); } byte[] msg = EncodingUtils.getAsciiBytes(message); ByteArrayEntity entity = new ByteArrayEntity(msg); entity.setContentType("text/plain; charset=US-ASCII"); response.setEntity(entity); }
private OpenForReadResult readDataUri(Uri uri) { String uriAsString = uri.getSchemeSpecificPart(); int commaPos = uriAsString.indexOf(','); if (commaPos == -1) { return null; } String[] mimeParts = uriAsString.substring(0, commaPos).split(";"); String contentType = null; boolean base64 = false; if (mimeParts.length > 0) { contentType = mimeParts[0]; } for (int i = 1; i < mimeParts.length; ++i) { if ("base64".equalsIgnoreCase(mimeParts[i])) { base64 = true; } } String dataPartAsString = uriAsString.substring(commaPos + 1); byte[] data = base64 ? Base64.decode(dataPartAsString, Base64.DEFAULT) : EncodingUtils.getBytes(dataPartAsString, "UTF-8"); InputStream inputStream = new ByteArrayInputStream(data); return new OpenForReadResult(uri, inputStream, contentType, data.length, null); }
private void readDataFromFile() { ThreadPoolUtils.getInstance().execute(new Runnable() { @Override public void run() { try { FileInputStream fis = openFileInput(filename); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); String fileContent = EncodingUtils.getString(buffer, "UTF-8"); parseDataFromJson(fileContent); sendParseDataMessage(REFRESH_ERROR); } catch (IOException e) { e.printStackTrace(); } } }); }
private void readDataFromFile() { new Thread(new Runnable() { @Override public void run() { try { FileInputStream fis; fis = openFileInput(mFileName); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); final String fileContent = EncodingUtils.getString(buffer, "UTF-8"); runOnUiThread(new Runnable() { @Override public void run() { parseWeatherData(fileContent); } }); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
/** Constructor to use when message contents are known */ NTLMMessage(final String messageBody, final int expectedType) throws NTLMEngineException { messageContents = Base64.decode(EncodingUtils.getBytes(messageBody, DEFAULT_CHARSET), Base64.NO_WRAP); // Look for NTLM message if (messageContents.length < SIGNATURE.length) { throw new NTLMEngineException("NTLM message decoding error - packet too short"); } int i = 0; while (i < SIGNATURE.length) { if (messageContents[i] != SIGNATURE[i]) { throw new NTLMEngineException( "NTLM message expected - instead got unrecognized bytes"); } i++; } // Check to be sure there's a type 2 message indicator next final int type = readULong(SIGNATURE.length); if (type != expectedType) { throw new NTLMEngineException("NTLM type " + Integer.toString(expectedType) + " message expected - instead got type " + Integer.toString(type)); } currentOutputPosition = messageContents.length; }
@Test public void testBasicAuthentication() throws Exception { final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""); final BasicScheme authscheme = new BasicScheme(); authscheme.processChallenge(challenge); final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpContext context = new BasicHttpContext(); final Header authResponse = authscheme.authenticate(creds, request, context); final String expected = "Basic " + EncodingUtils.getAsciiString( Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass"))); Assert.assertEquals(AUTH.WWW_AUTH_RESP, authResponse.getName()); Assert.assertEquals(expected, authResponse.getValue()); Assert.assertEquals("test", authscheme.getRealm()); Assert.assertTrue(authscheme.isComplete()); Assert.assertFalse(authscheme.isConnectionBased()); }
@Test public void testBasicProxyAuthentication() throws Exception { final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); final Header challenge = new BasicHeader(AUTH.PROXY_AUTH, "Basic realm=\"test\""); final BasicScheme authscheme = new BasicScheme(); authscheme.processChallenge(challenge); final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpContext context = new BasicHttpContext(); final Header authResponse = authscheme.authenticate(creds, request, context); final String expected = "Basic " + EncodingUtils.getAsciiString( Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass"))); Assert.assertEquals(AUTH.PROXY_AUTH_RESP, authResponse.getName()); Assert.assertEquals(expected, authResponse.getValue()); Assert.assertEquals("test", authscheme.getRealm()); Assert.assertTrue(authscheme.isComplete()); Assert.assertFalse(authscheme.isConnectionBased()); }
public static void loadWebapp(WebView webView, AppSettings appSettings, boolean doLogin) { Context context = webView.getContext(); Uri url; try { url = Uri.parse(appSettings.getProfilePathFull()); } catch (Exception e) { webView.loadData(context.getString(R.string.no_valid_path), "text/html", "UTF-16"); return; } String url_s = url.toString(); if (appSettings.isProfileEmpty()) { webView.loadData(context.getString(R.string.no_valid_path), "text/html", "UTF-16"); } else { webView.loadUrl(url_s); if (doLogin) { url_s += "?a=checklogin"; String postData = "name=" + appSettings.getProfileLoginUsername() + "&password=" + appSettings.getProfileLoginPassword(); webView.postUrl(url_s, EncodingUtils.getBytes(postData, "base64")); } } }
public static String getHtmlString(String urlString) { try { URL url = new URL(urlString); URLConnection ucon = url.openConnection(); InputStream instr = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(instr); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return EncodingUtils.getString(baf.toByteArray(), "utf-8"); } catch (Exception e) { Log.d("win","lllll"+e.toString()); return ""; } }