Search in sources :

Example 81 with BufferedOutputStream

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL)

Example 82 with BufferedOutputStream

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Example 83 with BufferedOutputStream

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");
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedReader(java.io.BufferedReader) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet)

Example 84 with BufferedOutputStream

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();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 85 with BufferedOutputStream

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) PreTranslationCounter(net.heartsome.cat.ts.pretranslation.bean.PreTranslationCounter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) VTDNav(com.ximpleware.VTDNav) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1219 FileOutputStream (java.io.FileOutputStream)861 IOException (java.io.IOException)617 File (java.io.File)519 OutputStream (java.io.OutputStream)350 BufferedInputStream (java.io.BufferedInputStream)238 InputStream (java.io.InputStream)166 DataOutputStream (java.io.DataOutputStream)158 FileInputStream (java.io.FileInputStream)145 ZipOutputStream (java.util.zip.ZipOutputStream)121 FileNotFoundException (java.io.FileNotFoundException)113 ZipEntry (java.util.zip.ZipEntry)108 ByteArrayOutputStream (java.io.ByteArrayOutputStream)101 ZipFile (java.util.zip.ZipFile)62 URL (java.net.URL)57 XmlSerializer (org.xmlpull.v1.XmlSerializer)57 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)56 ObjectOutputStream (java.io.ObjectOutputStream)54 GZIPOutputStream (java.util.zip.GZIPOutputStream)51 PrintStream (java.io.PrintStream)46