Search in sources :

Example 66 with BufferedInputStream

use of java.io.BufferedInputStream in project eweb4j-framework by laiweiwei.

the class Props method readProperties.

// 读取properties的全部信息
public static synchronized String readProperties(Prop f, boolean isCreate) throws IOException {
    if (f == null || f.getPath().length() == 0)
        return null;
    String id = f.getId();
    String path = f.getPath();
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    I18N i18n = cb.getLocales();
    final String sufPro = ".properties";
    if (i18n != null) {
        for (Locale l : i18n.getLocale()) {
            String suffix1 = "_" + l.getLanguage() + "_" + l.getCountry();
            String tmpPath1 = path.replace(sufPro, suffix1 + sufPro);
            i18nIds.add(id);
            if (FileUtil.exists(ConfigConstant.CONFIG_BASE_PATH + tmpPath1)) {
                Prop p = new Prop();
                p.setGlobal("false");
                p.setId(id + suffix1);
                p.setPath(tmpPath1);
                // 递归,把国际化文件内容加载仅缓存
                readProperties(p, false);
                // 如果存在国际化文件,那么默认的文件允许不存在
                isCreate = false;
                continue;
            }
            String suffix2 = "_" + l.getLanguage();
            String tmpPath2 = path.replace(sufPro, suffix2 + sufPro);
            if (FileUtil.exists(ConfigConstant.CONFIG_BASE_PATH + tmpPath2)) {
                Prop p = new Prop();
                p.setGlobal("false");
                p.setId(id + suffix2);
                p.setPath(tmpPath2);
                // 递归,把国际化文件内容加载仅缓存
                readProperties(p, false);
                // 如果存在国际化文件,那么默认的文件允许不存在
                isCreate = false;
                continue;
            }
        }
    }
    String error = null;
    String filePath = ConfigConstant.CONFIG_BASE_PATH + path;
    String global = f.getGlobal();
    Properties properties = new Properties();
    InputStream in = null;
    Hashtable<String, String> tmpHt = new Hashtable<String, String>();
    try {
        in = new BufferedInputStream(new FileInputStream(filePath));
        properties.load(in);
        //第一遍全部加进来
        Props.loadProperty(properties, tmpHt);
        //渲染 ${} 引用值
        Pattern pattern = Pattern.compile(RegexList.property_single_regexp);
        for (Iterator<Entry<String, String>> it = tmpHt.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> e = it.next();
            String key = e.getKey();
            String property = e.getValue();
            Props.renderVarable(pattern, key, property, tmpHt);
        }
        if ("true".equalsIgnoreCase(global) || "1".equalsIgnoreCase(global)) {
            globalMap.putAll(tmpHt);
            log.debug("global | map -> " + tmpHt.toString());
        } else if (id != null && id.length() > 0) {
            props.put(id, tmpHt);
            log.debug("id -> " + id + " | map -> " + tmpHt.toString());
        }
    } catch (FileNotFoundException e) {
        log.warn(filePath + ", file not found!", e);
        if (isCreate) {
            boolean flag = FileUtil.createFile(filePath);
            if (flag) {
                error = filePath + " create success";
                Props.writeProperties(filePath, "framework", "eweb4j");
                log.warn(error);
            } else {
                log.warn(filePath + " create fail");
            }
        }
    } finally {
        if (in != null)
            in.close();
    }
    return error;
}
Also used : Locale(org.eweb4j.config.bean.Locale) Pattern(java.util.regex.Pattern) Prop(org.eweb4j.config.bean.Prop) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) FileNotFoundException(java.io.FileNotFoundException) ConfigBean(org.eweb4j.config.bean.ConfigBean) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Entry(java.util.Map.Entry) BufferedInputStream(java.io.BufferedInputStream) I18N(org.eweb4j.config.bean.I18N)

Example 67 with BufferedInputStream

use of java.io.BufferedInputStream in project LiveSDK-for-Android by liveservices.

the class LiveConnectClient method toByteArray.

/**
     * Converts an InputStream to a {@code byte[]}.
     *
     * @param is to convert to a {@code byte[]}.
     * @return a new {@code byte[]} from the InputStream.
     * @throws IOException if there was an error reading or closing the InputStream.
     */
private static byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    OutputStream out = new BufferedOutputStream(byteOut);
    is = new BufferedInputStream(is);
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
    } finally {
        // we want to perform silent close operations
        closeSilently(is);
        closeSilently(out);
    }
    return byteOut.toByteArray();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 68 with BufferedInputStream

use of java.io.BufferedInputStream in project ARChon-Packager by bpear96.

the class activityInstalled method unpackZip.

private boolean unpackZip(String path, String zipname) {
    //Used to unpack template.zip
    InputStream is;
    ZipInputStream zis;
    try {
        String filename;
        is = new FileInputStream(path + zipname);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;
        while ((ze = zis.getNextEntry()) != null) {
            filename = ze.getName();
            // it will generate an Exception...
            if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
            }
            FileOutputStream fout = new FileOutputStream(path + filename);
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }
            fout.close();
            zis.closeEntry();
        }
        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 69 with BufferedInputStream

use of java.io.BufferedInputStream in project japid42 by branaway.

the class JapidRenderer method recoverClasses.

/**
	 * @author Bing Ran (bing.ran@gmail.com)
	 */
@SuppressWarnings("unchecked")
private static void recoverClasses() {
    String templateRoot = getClassCacheRoot();
    FileInputStream fos;
    File file = new File(new File(templateRoot), JAPID_CLASSES_CACHE);
    try {
        if (file.exists()) {
            // discard it if the file is too old
            long t = System.currentTimeMillis();
            if (t - file.lastModified() > 10000) {
                // too old
                JapidFlags.debug("the japid cache was too old. discarded.");
                file.delete();
            } else {
                fos = new FileInputStream(file);
                BufferedInputStream bos = new BufferedInputStream(fos);
                ObjectInputStream ois = new ObjectInputStream(bos);
                String version = (String) ois.readObject();
                JapidFlags.debug("Japid version: " + VERSION + ". JapidCache version: " + version);
                if (!version.equals(VERSION)) {
                    JapidFlags.debug("Japid classes mismatch. Discard cache.");
                } else {
                    japidClasses = (Map<String, RendererClass>) ois.readObject();
                    resourceJars = (HashSet<File>) ois.readObject();
                    HashSet<File> versionCheckedDirs = (HashSet<File>) ois.readObject();
                    JapidFlags.setVersionCheckedDirs(versionCheckedDirs);
                    JapidFlags.debug("recovered Japid classes from cache");
                }
                ois.close();
            }
        }
    } catch (Exception e) {
        JapidFlags.info("error in recovering class cache. Ignored: " + e);
    // e.printStackTrace();
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) HashSet(java.util.HashSet)

Example 70 with BufferedInputStream

use of java.io.BufferedInputStream in project japid42 by branaway.

the class JapidRenderer method readSource.

static String readSource(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(bis, "UTF-8"));
    StringBuilder b = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
        b.append(line + "\n");
    }
    br.close();
    return b.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56