Search in sources :

Example 86 with BufferedOutputStream

use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.

the class TMXValidator method save.

private boolean save(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 (Exception e) {
        LOGGER.error("", e);
    }
    return false;
}
Also used : FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 87 with BufferedOutputStream

use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.

the class TSFileHandler method saveFile.

@Override
public Map<String, Object> saveFile(File srcFile, File tgtFile) {
    if (srcFile == null || !srcFile.exists()) {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger16"), srcFile.getAbsoluteFile());
        logger.error(msg);
        return getErrorResult(msg, null);
    }
    if (tgtFile == null || !tgtFile.exists()) {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger17"), tgtFile.getAbsoluteFile());
        logger.error(msg);
        return getErrorResult(msg, null);
    }
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(srcFile);
        bis = new BufferedInputStream(fis);
    } catch (FileNotFoundException e) {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger16"), srcFile.getAbsoluteFile());
        logger.error(msg, e);
        return getErrorResult(msg, e);
    }
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
        fos = new FileOutputStream(tgtFile);
        bos = new BufferedOutputStream(fos);
    } catch (FileNotFoundException e) {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger17"), tgtFile.getAbsoluteFile());
        logger.error(msg, e);
        return getErrorResult(msg, e);
    }
    byte[] buffer = new byte[2048];
    try {
        while (bis.read(buffer) != -1) {
            bos.write(buffer, 0, buffer.length);
        }
        bos.flush();
        bos.close();
        bis.close();
        fos.close();
        fis.close();
    } catch (IOException e) {
        String msg = null;
        if (srcFile.getAbsolutePath().equals(tgtFile.getAbsolutePath())) {
            msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger18"), srcFile.getAbsolutePath());
        } else {
            msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger19"), new Object[] { srcFile.getAbsolutePath(), tgtFile.getAbsolutePath() });
        }
        logger.error(msg, e);
        return getErrorResult(msg, e);
    }
    return getSuccessResult(Messages.getString("file.TSFileHandler.logger20"));
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 88 with BufferedOutputStream

use of java.io.BufferedOutputStream in project translationstudio8 by heartsome.

the class PreMachineTranslation method executeTranslation.

/**
	 * 根据构建参数执行预翻译 ;
	 * @throws InterruptedException
	 */
public List<PreMachineTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    simpleMatchers = getMacthers();
    if (null == simpleMatchers || simpleMatchers.isEmpty()) {
        return this.transCounters;
    }
    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 PreMachineTranslationCounter(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;
                }
                keepCurrentMatchs(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) PreMachineTranslationCounter(net.heartsome.cat.ts.machinetranslation.bean.PreMachineTranslationCounter) 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) 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)

Example 89 with BufferedOutputStream

use of java.io.BufferedOutputStream in project powermock by powermock.

the class DebuggingClassWriter method toByteArray.

public byte[] toByteArray() {
    return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            byte[] b = DebuggingClassWriter.super.toByteArray();
            if (debugLocation != null) {
                String dirs = className.replace('.', File.separatorChar);
                try {
                    new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {
                        out.write(b);
                    } finally {
                        out.close();
                    }
                    if (traceEnabled) {
                        file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {
                            ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            TraceClassVisitor tcv = new TraceClassVisitor(null, pw);
                            cr.accept(tcv, 0);
                            pw.flush();
                        } finally {
                            out.close();
                        }
                    }
                } catch (IOException e) {
                    throw new CodeGenerationException(e);
                }
            }
            return b;
        }
    });
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) TraceClassVisitor(org.powermock.api.mockito.repackaged.asm.util.TraceClassVisitor) FileOutputStream(java.io.FileOutputStream) ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter)

Example 90 with BufferedOutputStream

use of java.io.BufferedOutputStream in project cassandra by apache.

the class OutputStreamBench method setUp.

@Setup
public void setUp(final Blackhole bh) {
    StringBuilder sb = new StringBuilder();
    for (int ii = 0; ii < 11; ii++) {
        sb.append(BufferedDataOutputStreamTest.fourByte);
        sb.append(BufferedDataOutputStreamTest.threeByte);
        sb.append(BufferedDataOutputStreamTest.twoByte);
    }
    smallM = sb.toString();
    sb = new StringBuilder();
    while (sb.length() < 1024 * 12) {
        sb.append(small);
    }
    large = sb.toString();
    sb = new StringBuilder();
    while (sb.length() < 1024 * 12) {
        sb.append(smallM);
    }
    largeM = sb.toString();
    hole = new BufferedOutputStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            bh.consume(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            bh.consume(b);
        }

        @Override
        public void write(byte[] b, int a, int c) throws IOException {
            bh.consume(b);
            bh.consume(a);
            bh.consume(c);
        }
    });
    streamA = new WrappedDataOutputStreamPlus(hole);
    streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() {

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }

        @Override
        public int write(ByteBuffer src) throws IOException {
            bh.consume(src);
            int remaining = src.remaining();
            src.position(src.limit());
            return remaining;
        }
    }, 8192);
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) BufferedOutputStream(java.io.BufferedOutputStream) ByteBuffer(java.nio.ByteBuffer)

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