List of usage examples for java.io FileInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:net.sf.zekr.common.util.CryptoUtils.java
public static byte[] sign(String datafile, PrivateKey prvKey, String sigAlg) throws Exception { Signature sig = Signature.getInstance(sigAlg); sig.initSign(prvKey);//from w w w . j a va 2s . c o m FileInputStream fis = new FileInputStream(datafile); byte[] dataBytes = new byte[1024]; int nread = fis.read(dataBytes); while (nread > 0) { sig.update(dataBytes, 0, nread); nread = fis.read(dataBytes); } return sig.sign(); }
From source file:com.ibm.ids.example.ShowResult.java
public static String getVulnerableSource(String file) throws java.io.IOException, java.io.FileNotFoundException { FileInputStream fis = new FileInputStream(file); byte[] buf = new byte[100]; fis.read(buf); String ret = new String(buf); fis.close();//from w ww . j a v a2 s .c o m return ret; }
From source file:Main.java
public static byte[] readByteArray(File file) { if (!file.exists() || !file.isFile()) { return null; }/*from w ww .j av a 2 s . co m*/ byte[] data = null; FileInputStream stream = null; try { stream = new FileInputStream(file); data = new byte[(int) file.length()]; stream.read(data); } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return data; }
From source file:be.vds.jtbdive.core.utils.FileUtilities.java
public static void replaceAllInFile(File file, String[] oldValues, String[] newValues) throws IOException { byte[] b = new byte[(int) file.length()]; FileInputStream is = new FileInputStream(file); is.read(b); is.close();/*from w ww . j a v a2 s . c om*/ String s = new String(b); for (int i = 0; i < oldValues.length; i++) { s = s.replaceAll(oldValues[i], newValues[i]); } FileOutputStream os = new FileOutputStream(file); os.write(s.getBytes()); os.flush(); os.close(); }
From source file:Main.java
public static String[] getAvailableSchedulers() { File iofile = new File("/sys/block/mmcblk0/queue/scheduler"); String s = ""; FileInputStream fin2 = null; try {//from ww w. ja v a 2s . co m fin2 = new FileInputStream(iofile); byte fileContent[] = new byte[(int) iofile.length()]; fin2.read(fileContent); s = new String(fileContent).trim().split("\n")[0]; } catch (FileNotFoundException e) { //System.out.println("File not found" + e); } catch (IOException ioe) { //System.out.println("Exception while reading file " + ioe); } finally { try { if (fin2 != null) { fin2.close(); } } catch (IOException ioe) { //System.out.println("Error while closing stream: " + ioe); } } String[] IOSchedulers = s.replace("[", "").replace("]", "").split(" "); return IOSchedulers; }
From source file:com.kyon.klib.base.KFileUtils.java
public static String readFile(Context context, String fileName) throws IOException { String res = ""; try {/*from w w w . j a va2 s . c o m*/ FileInputStream fIn = context.openFileInput(fileName); int length = fIn.available(); byte[] buffer = new byte[length]; fIn.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fIn.close(); } catch (Exception e) { e.printStackTrace(); } return res; }
From source file:Main.java
public static String getCurrentScheduler() { File iofile = new File("/sys/block/mmcblk0/queue/scheduler"); String s = ""; FileInputStream fin2 = null; try {//from w w w.java2 s. co m fin2 = new FileInputStream(iofile); byte fileContent[] = new byte[(int) iofile.length()]; fin2.read(fileContent); s = new String(fileContent).trim().split("\n")[0]; } catch (FileNotFoundException e) { //System.out.println("File not found" + e); } catch (IOException ioe) { //System.out.println("Exception while reading file " + ioe); } finally { try { if (fin2 != null) { fin2.close(); } } catch (IOException ioe) { //System.out.println("Error while closing stream: " + ioe); } } int bropen = s.indexOf("["); int brclose = s.lastIndexOf("]"); return s.substring(bropen + 1, brclose); }
From source file:org.phpmaven.pear.impl.Helper.java
/** * Returns the binary file contents.//from w ww . j av a 2s.c om * @param uri URI of the resource. * @return the files content. * @throws IOException thrown on errors. */ public static byte[] getBinaryFileContents(String uri) throws IOException { // is it inside the local filesystem? if (uri.startsWith("file://")) { final File channelFile = new File(uri.substring(7)); final byte[] result = new byte[(int) channelFile.length()]; final FileInputStream fis = new FileInputStream(channelFile); fis.read(result); return result; } // try http connection final HttpClient client = new DefaultHttpClient(); final HttpGet httpget = new HttpGet(uri); final HttpResponse response = client.execute(httpget); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / " + response.getStatusLine().getReasonPhrase()); } final HttpEntity entity = response.getEntity(); if (entity == null) { throw new IOException("Empty response."); } return EntityUtils.toByteArray(entity); }
From source file:com.ewcms.common.io.HtmlFileUtil.java
public static byte[] readByte(String fileName) { try {/* w w w .j av a 2s . c om*/ fileName = normalizePath(fileName); FileInputStream fis = new FileInputStream(fileName); byte r[] = new byte[fis.available()]; fis.read(r); fis.close(); return r; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static KeyPair loadKeyPair(Context context, String name) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { // Read Public Key. File filePublicKey = new File(context.getFilesDir(), name + "_public.key"); FileInputStream fis = new FileInputStream(filePublicKey); byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; fis.read(encodedPublicKey); fis.close();//ww w. ja v a 2s.co m // Read Private Key. File filePrivateKey = new File(context.getFilesDir(), name + "_private.key"); fis = new FileInputStream(filePrivateKey); byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()]; fis.read(encodedPrivateKey); fis.close(); // Generate KeyPair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }