use of java.io.BufferedOutputStream in project PhotoNoter by yydcdut.
the class RxFeedBack method httpConnection.
private JSONObject httpConnection(JSONObject infoJosn, String url) throws IOException {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(url)).openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
String deviceInfo = infoJosn.toString();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
bufferedOutputStream.write(deviceInfo.getBytes());
bufferedOutputStream.flush();
if (httpURLConnection.getResponseCode() != 200) {
return null;
}
JSONObject responseJson = getResponse(httpURLConnection.getInputStream());
httpURLConnection.disconnect();
return responseJson;
} catch (MalformedURLException e) {
YLog.e(e);
} catch (JSONException jsonException) {
YLog.e(jsonException);
}
return null;
}
use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.
the class DatabaseConfiger method saveToFile.
/**
* 保存文件
* @param xm
* XMLModifier对象
* @param fileName
* 文件名
* @return 是否保存成功;
*/
private boolean saveToFile(XMLModifier xm, File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
return true;
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
return false;
}
use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.
the class Txt2Tmx method writeTmx.
private void writeTmx(File2TmxConvertBean bean, FileOutputStream fos, IProgressMonitor monitor) throws Exception {
monitor.setTaskName(Messages.getString("converter.docx2tmx.docx.readdocx"));
double total = 0;
double count = 0;
int worked = 0;
int tmp = 0;
InputStreamReader freader = null;
try {
File file = new File(bean.sourceFilePath);
String encoding = FileEncodingDetector.detectFileEncoding(file);
freader = new InputStreamReader(new FileInputStream(file), encoding);
total = getLineNumber(file, encoding);
if (total < 1) {
throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
}
} catch (FileNotFoundException e) {
LOGGER.error("无法读取txt文件", e);
throw e;
}
BufferedReader reader = new BufferedReader(freader);
BufferedOutputStream bos = new BufferedOutputStream(fos);
try {
// 语言代码
String rowStr = reader.readLine();
count++;
String[] rowArr = rowStr.split("\t");
if (rowArr == null || rowArr.length < 2) {
throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
}
Set<String> testSet = new HashSet<String>();
testSet.addAll(Arrays.asList(rowArr));
if (testSet.size() != rowArr.length) {
throw new Exception(Messages.getString("converter.common.vaild.duplicatelangcode.error"));
}
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 0; i < rowArr.length; i++) {
// check langcode
if (rowArr[i].trim().isEmpty()) {
throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
}
map.put(i, LanguageUtils.convertLangCode(rowArr[i]));
}
bean.srcLangCode = rowArr[0];
if (bean.srcLangCode == null || bean.srcLangCode.isEmpty()) {
Exception e = new Exception(Messages.getString("converter.docx2tmx.docx.invaild"));
throw e;
}
if (bean.appendExistTmxFilePath != null) {
if (!bean.srcLangCode.equals(appendSrclang)) {
Exception e = new Exception(Messages.getString("converter.common.appendtmx.diffsrcLang.error"));
throw e;
}
} else {
bos.write(TmxTemple.getDefaultTmxPrefix(bean.srcLangCode).getBytes());
}
String tuContent = null;
while ((rowStr = reader.readLine()) != null) {
if (monitor.isCanceled()) {
return;
}
// 设置进度
count++;
tmp = (int) ((count / total) * 100);
if (tmp > worked) {
monitor.worked(tmp - worked);
worked = tmp;
}
rowArr = rowStr.split("\t");
tuContent = createTu(rowArr, map, bean);
bos.write('\r');
bos.write('\n');
bos.write(tuContent.getBytes());
}
bos.write("</body></tmx>".getBytes());
bos.flush();
monitor.done();
} catch (IOException e) {
throw e;
} finally {
try {
if (reader != null) {
reader.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
LOGGER.error("close reader&writer");
}
}
}
use of java.io.BufferedOutputStream in project xUtils3 by wyouflf.
the class IOUtil method copy.
public static void copy(InputStream in, OutputStream out) throws IOException {
if (!(in instanceof BufferedInputStream)) {
in = new BufferedInputStream(in);
}
if (!(out instanceof BufferedOutputStream)) {
out = new BufferedOutputStream(out);
}
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.
the class PreTranslation method executeTranslation.
/**
* 根据构建参数执行预翻译 ;
* @throws InterruptedException
*/
public List<PreTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask("", this.xlfFiles.size());
monitor.setTaskName(Messages.getString("pretranslation.PreTranslation.task1"));
try {
for (String xlfPath : xlfFiles) {
if (monitor != null && monitor.isCanceled()) {
throw new InterruptedException();
}
currentCounter = new PreTranslationCounter(xlfPath);
this.transCounters.add(currentCounter);
VTDNav vn = xlfHandler.getVnMap().get(xlfPath);
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vu.getVTDNav());
int tuNumber = xlfHandler.getNodeCount(xlfPath, "/xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]");
currentCounter.setTuNumber(tuNumber);
ap.selectXPath("/xliff/file");
String srcLang = "";
String tgtLang = "";
XMLModifier xm = new XMLModifier(vn);
IProgressMonitor monitor2 = new SubProgressMonitor(monitor, 1);
monitor2.beginTask(Messages.getString("pretranslation.PreTranslation.task2"), tuNumber);
while (ap.evalXPath() != -1) {
// 循环 file 节点
String _srcLang = vu.getCurrentElementAttribut("source-language", "");
String _tgtLang = vu.getCurrentElementAttribut("target-language", "");
if (!_srcLang.equals("")) {
srcLang = _srcLang;
}
if (!_tgtLang.equals("")) {
tgtLang = _tgtLang;
}
if (srcLang.equals("") || tgtLang.equals("")) {
continue;
}
if (updateStrategy == PreTransParameters.KEEP_OLD_TARGET) {
keepCurrentMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
} else if (updateStrategy == PreTransParameters.KEEP_BEST_MATCH_TARGET) {
keepHigherMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
} else if (updateStrategy == PreTransParameters.KEEP_NEW_TARGET) {
overwriteMatchs(vu, srcLang, tgtLang, xm, monitor2);
}
}
monitor2.done();
FileOutputStream fos = new FileOutputStream(xlfPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
}
} catch (XPathParseException e) {
logger.error("", e);
e.printStackTrace();
} catch (NavException e) {
logger.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
logger.error("", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
logger.error("", e);
e.printStackTrace();
} catch (FileNotFoundException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
monitor.done();
return this.transCounters;
}
Aggregations