Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

In this page you can find the example usage for java.io FileOutputStream write.

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file output stream.

Usage

From source file:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

private static void downloadAndSaveImageWithPrefix(Context context, String prefix, String fileName)
        throws IOException {
    String stringUrl = BASE_URL + prefix + fileName;
    URL url = new URL(stringUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream is = urlConnection.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    FileOutputStream fos = getFileOutputStream(context, fileName);

    ByteArrayBuffer baf = new ByteArrayBuffer(65535);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }/*from   ww  w  . ja v a2s.c  o m*/
    fos.write(baf.toByteArray());
    fos.close();
    bis.close();
}

From source file:net.sf.sripathi.ws.mock.util.FileUtil.java

public static void createFilesAndFolder(String root, ZipFile zip) {

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    Set<String> files = new TreeSet<String>();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (entry.getName().toLowerCase().endsWith(".wsdl") || entry.getName().toLowerCase().endsWith(".xsd"))
            files.add(entry.getName());// www.j av a 2 s  .  co m
    }

    File rootFolder = new File(root);
    if (!rootFolder.exists()) {
        throw new MockException("Unable to create file - " + root);
    }

    for (String fileStr : files) {

        String folder = root;

        String[] split = fileStr.split("/");

        if (split.length > 1) {

            for (int i = 0; i < split.length - 1; i++) {

                folder = folder + "/" + split[i];
                File f = new File(folder);
                if (!f.exists()) {
                    f.mkdir();
                }
            }
        }

        File file = new File(folder + "/" + split[split.length - 1]);
        FileOutputStream fos = null;
        InputStream zipStream = null;
        try {
            fos = new FileOutputStream(file);
            zipStream = zip.getInputStream(zip.getEntry(fileStr));
            fos.write(IOUtils.toByteArray(zipStream));
        } catch (Exception e) {
            throw new MockException("Unable to create file - " + fileStr);
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
            try {
                zipStream.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.janoz.usenet.support.LogUtil.java

public static InputStream dumpStreamAndReOffer(InputStream is) throws IOException {
    FileOutputStream fos = null;
    try {/*from w  w w.j  av a  2 s .co m*/
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        copyStream(is, os);
        byte[] data = os.toByteArray();
        fos = new FileOutputStream(File.createTempFile("dump", ".bin", new File(".")));
        fos.write(data);
        return new ByteArrayInputStream(data);
    } finally {
        if (fos != null) {
            fos.close();
        }
    }

}

From source file:de.uni_koeln.spinfo.maalr.sigar.SigarWrapper.java

private static void initialize() {
    logger.info("Initializing...");
    String libBase = "/hyperic-sigar-1.6.5/sigar-bin/lib/";
    try {/*from w ww.j  av  a 2 s  .c om*/
        String prefix = "libsigar";
        if (SystemUtils.IS_OS_WINDOWS) {
            prefix = "sigar";
        }
        String arch = getSystemArch();
        String suffix = getSystemSuffix();
        String resourceName = prefix + "-" + arch + "-" + suffix;

        logger.info("Native library: " + resourceName);

        String resource = libBase + resourceName;
        URL toLoad = SigarWrapper.class.getResource(resource);
        File home = new File(System.getProperty("user.home"));
        File libs = new File(home, ".maalr/libs/sigar");
        libs.mkdirs();
        File tmp = new File(libs, resourceName);
        tmp.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(tmp);
        InputStream in = toLoad.openStream();
        int i;
        while ((i = in.read()) != -1) {
            fos.write(i);
        }
        in.close();
        fos.close();
        logger.info("Library copied to " + tmp.getAbsolutePath());
        String oldPath = System.getProperty("java.library.path");
        System.setProperty("java.library.path", oldPath + SystemUtils.PATH_SEPARATOR + libs.getAbsolutePath());
        logger.info("java.library.path updated: " + System.getProperty("java.library.path"));
        executors = Executors.newScheduledThreadPool(1, new ThreadFactory() {

            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        });
        logger.info("Initialization completed.");
    } catch (Exception e) {
        logger.error("Failed to initialize!", e);
    }
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 *   ??0-F?4838??//w w  w  . ja v a2  s . c  o  m
 * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746  AD67EA2F3BE6E5AD
 * DES? D368DFE03120B5DF DES? 92A8FD8FEC2F0746 DES? 
 * ???".license"??
 * 
 * @param file
 * @param key
 */
public static void encrypt(File file, String key) {
    try {
        if (key.length() == 48) {
            byte[] bytK1 = string2Byte(key.substring(0, 16));
            byte[] bytK2 = string2Byte(key.substring(16, 32));
            byte[] bytK3 = string2Byte(key.substring(32, 48));

            FileInputStream fis = new FileInputStream(file);
            byte[] bytIn = new byte[(int) file.length()];
            for (int i = 0; i < file.length(); i++) {
                bytIn[i] = (byte) fis.read();
            }

            // 
            byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn, bytK1), bytK2), bytK3);
            String fileOut = file.getPath() + ".license";
            FileOutputStream fos = new FileOutputStream(fileOut);
            for (int i = 0; i < bytOut.length; i++) {
                fos.write((int) bytOut[i]);
            }
            fos.close();
            fis.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.clutch.ClutchAB.java

private static void downloadABMeta() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("guid", ClutchAPIClient.getFakeGUID());
    ClutchAPIClient.callMethod("get_ab_metadata", params, new ClutchAPIResponseHandler() {
        @Override/*from ww w .j a v a 2s .  c om*/
        public void onSuccess(JSONObject response) {
            JSONObject metadata = response.optJSONObject("metadata");
            try {
                FileOutputStream fos = context.openFileOutput("__clutchab.json", Context.MODE_PRIVATE);
                fos.write(metadata.toString().getBytes());
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e(TAG, "Could not open the Clutch AB metadata file for output");
            } catch (IOException e) {
                Log.e(TAG, "Could not write to the Clutch AB metadata file");
            }
        }

        @Override
        public void onFailure(Throwable e, JSONObject errorResponse) {
            Log.e(TAG, "Failed to connect to the Clutch server to send AB logs: " + errorResponse);
        }
    });
}

From source file:com.attilax.zip.FileUtil.java

/**
 * /* w  w  w.j av  a2 s  .  c  o m*/
 * 
 * @param source     ?
 * @param target         
 * @param cache        ?
 * @throws Exception
 */
public static void mv(String source, String target, int cache) throws Exception {
    if (source.trim().equals(target.trim()))
        return;
    byte[] cached = new byte[cache];
    FileInputStream fromFile = new FileInputStream(source);
    FileOutputStream toFile = new FileOutputStream(target);
    while (fromFile.read(cached) != -1) {
        toFile.write(cached);
    }
    toFile.flush();
    toFile.close();
    fromFile.close();
    new File(source).deleteOnExit();
}

From source file:com.liusoft.dlog4j.TextCacheManager.java

/**
 * //  w ww.  j a va  2  s.  co m
 * @param type
 * @param id
 * @param text
 */
public static void updateTextContent(int type, long id, String text) {
    if (text == null)
        return;
    String path = getFilePath(type, id);
    FileOutputStream fos = null;
    try {
        File f = new File(path);
        if (!f.getParentFile().exists())
            f.getParentFile().mkdirs();
        fos = new FileOutputStream(f);
        fos.write(text.getBytes());
    } catch (IOException e) {
        log.error("Exception occur when writing to " + path, e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:net.peacesoft.nutch.crawl.ReSolrWriter.java

public static void toFile(String fileName, byte[] data) {
    FileOutputStream fos = null;
    try {/* w  w  w .ja  v a 2  s  .  c  o m*/
        File f = new File(fileName);
        fos = new FileOutputStream(f);
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException ex) {
    } catch (Exception ex) {
        System.out.println("Loi khi xuat ra file: " + fileName);
    } finally {
        try {
            fos.close();
        } catch (Exception ex) {
        }
    }
}

From source file:net.jakobnielsen.imagga.upload.client.UploadClientIntegrationTest.java

public static File createTestFile() throws IOException {
    Base64 decoder = new Base64();
    byte[] imgBytes = decoder.decode(UploadClientIntegrationTest.TEST_IMAGE);
    File f = File.createTempFile("imagga", ".png");
    FileOutputStream osf = new FileOutputStream(f);
    osf.write(imgBytes);
    osf.flush();//from  ww  w .  j a  v a  2 s .  c  o m
    return f;
}