Example usage for com.liferay.portal.kernel.util FileUtil move

List of usage examples for com.liferay.portal.kernel.util FileUtil move

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil move.

Prototype

public static boolean move(String sourceFileName, String destinationFileName) 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.util.JQTFastStart.java

License:Open Source License

protected void doConvert(File inputFile, File outputFile) throws IOException {

    validate(inputFile, outputFile);/*from   w  ww.  ja v a  2s .  co  m*/

    RandomAccessFile randomAccessInputFile = null;
    RandomAccessFile randomAccessOutputFile = null;

    try {
        randomAccessInputFile = new RandomAccessFile(inputFile, "r");

        Atom atom = null;
        Atom ftypAtom = null;

        boolean ftypFound = false;
        boolean mdatFound = false;
        boolean isFastStart = false;

        while (randomAccessInputFile.getFilePointer() < randomAccessInputFile.length()) {

            atom = new Atom(randomAccessInputFile);

            if (!atom.isTopLevelAtom()) {
                throw new IOException("Non top level atom was found " + atom.getType());
            }

            if (ftypFound && !mdatFound && atom.isMOOV()) {
                isFastStart = true;

                break;
            }

            if (atom.isFTYP()) {
                ftypAtom = atom;

                ftypAtom.fillBuffer(randomAccessInputFile);

                ftypFound = true;
            } else if (atom.isMDAT()) {
                mdatFound = true;

                randomAccessInputFile.skipBytes((int) atom.getSize());
            } else {
                randomAccessInputFile.skipBytes((int) atom.getSize());
            }
        }

        if (isFastStart) {
            if (_log.isInfoEnabled()) {
                _log.info("The movie is already a fast start MP4");
            }

            FileUtil.move(inputFile, outputFile);

            return;
        }

        if (!atom.isMOOV()) {
            throw new IOException("Last atom was not of type MOOV");
        }

        randomAccessInputFile.seek(atom.getOffset());

        Atom moovAtom = atom;

        moovAtom.fillBuffer(randomAccessInputFile);

        if (moovAtom.hasCompressedMoovAtom()) {
            throw new IOException("Compressed MOOV atoms are unsupported");
        }

        moovAtom.patchAtom();

        randomAccessInputFile.seek(ftypAtom.getOffset() + ftypAtom.getSize());

        randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");

        randomAccessOutputFile.setLength(0);

        randomAccessOutputFile.write(ftypAtom.getBuffer());
        randomAccessOutputFile.write(moovAtom.getBuffer());

        byte[] buffer = new byte[1024 * 1024];

        while ((randomAccessInputFile.getFilePointer() + buffer.length) < moovAtom.getOffset()) {

            int read = randomAccessInputFile.read(buffer);

            randomAccessOutputFile.write(buffer, 0, read);
        }

        int bufferSize = (int) (moovAtom.getOffset() - randomAccessInputFile.getFilePointer());

        buffer = new byte[bufferSize];

        randomAccessInputFile.readFully(buffer);

        randomAccessOutputFile.write(buffer);
    } finally {
        if (randomAccessInputFile != null) {
            randomAccessInputFile.close();
        }

        if (randomAccessOutputFile != null) {
            randomAccessOutputFile.close();
        }
    }
}

From source file:com.liferay.portlet.plugininstaller.action.InstallPluginAction.java

License:Open Source License

protected int remoteDeploy(String url, URL urlObj, ActionRequest actionRequest, boolean failOnError)
        throws Exception {

    int responseCode = HttpServletResponse.SC_OK;

    GetMethod getMethod = null;//w ww . jav  a  2 s  .  com

    String deploymentContext = ParamUtil.getString(actionRequest, "deploymentContext");

    try {
        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(url);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        getMethod = new GetMethod(url);

        String fileName = null;

        if (Validator.isNotNull(deploymentContext)) {
            fileName = BaseDeployer.DEPLOY_TO_PREFIX + deploymentContext + ".war";
        } else {
            fileName = url.substring(url.lastIndexOf(CharPool.SLASH) + 1);

            int pos = fileName.lastIndexOf(CharPool.PERIOD);

            if (pos != -1) {
                deploymentContext = fileName.substring(0, pos);
            }
        }

        PluginPackageUtil.registerPluginPackageInstallation(deploymentContext);

        responseCode = httpClient.executeMethod(hostConfiguration, getMethod);

        if (responseCode != HttpServletResponse.SC_OK) {
            if (failOnError) {
                SessionErrors.add(actionRequest, "errorConnectingToUrl",
                        new Object[] { String.valueOf(responseCode) });
            }

            return responseCode;
        }

        long contentLength = getMethod.getResponseContentLength();

        String progressId = ParamUtil.getString(actionRequest, Constants.PROGRESS_ID);

        ProgressInputStream pis = new ProgressInputStream(actionRequest, getMethod.getResponseBodyAsStream(),
                contentLength, progressId);

        String deployDir = PrefsPropsUtil.getString(PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
                PropsValues.AUTO_DEPLOY_DEPLOY_DIR);

        String tmpFilePath = deployDir + StringPool.SLASH + _DOWNLOAD_DIR + StringPool.SLASH + fileName;

        File tmpFile = new File(tmpFilePath);

        if (!tmpFile.getParentFile().exists()) {
            tmpFile.getParentFile().mkdirs();
        }

        FileOutputStream fos = new FileOutputStream(tmpFile);

        try {
            pis.readAll(fos);

            if (_log.isInfoEnabled()) {
                _log.info("Downloaded plugin from " + urlObj + " has " + pis.getTotalRead() + " bytes");
            }
        } finally {
            pis.clearProgress();
        }

        getMethod.releaseConnection();

        if (pis.getTotalRead() > 0) {
            String destination = deployDir + StringPool.SLASH + fileName;

            File destinationFile = new File(destination);

            boolean moved = FileUtil.move(tmpFile, destinationFile);

            if (!moved) {
                FileUtil.copyFile(tmpFile, destinationFile);
                FileUtil.delete(tmpFile);
            }

            SessionMessages.add(actionRequest, "pluginDownloaded");
        } else {
            if (failOnError) {
                SessionErrors.add(actionRequest, UploadException.class.getName());
            }

            responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }
    } catch (MalformedURLException murle) {
        SessionErrors.add(actionRequest, "invalidUrl", murle);
    } catch (IOException ioe) {
        SessionErrors.add(actionRequest, "errorConnectingToUrl", ioe);
    } finally {
        if (getMethod != null) {
            getMethod.releaseConnection();
        }

        PluginPackageUtil.endPluginPackageInstallation(deploymentContext);
    }

    return responseCode;
}

From source file:com.sqli.liferay.imex.core.lar.LarExporter.java

License:Open Source License

public void doExport(Group group, File groupDir, boolean privateLayout, Map<String, String[]> parameterMap)
        throws Exception {

    long groupId = group.getGroupId();
    long[] layoutIds = null;
    Date startDate = null;/* w  w  w  .  j ava2 s . co m*/
    Date endDate = null;

    File larFile = LayoutLocalServiceUtil.exportLayoutsAsFile(groupId, privateLayout, layoutIds, parameterMap,
            startDate, endDate);

    FileUtil.move(larFile, LarNameUtil.getLarFile(group, groupDir, privateLayout));
}