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;
}
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();
}
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;
}
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();
}
}
}
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();
}
Aggregations