Example usage for java.io FileOutputStream getChannel

List of usage examples for java.io FileOutputStream getChannel

Introduction

In this page you can find the example usage for java.io FileOutputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file output stream.

Usage

From source file:org.apache.manifoldcf.agents.output.filesystem.FileOutputConnector.java

/** Add (or replace) a document in the output data store using the connector.
 * This method presumes that the connector object has been configured, and it is thus able to communicate with the output data store should that be
 * necessary./*from   w w  w  .  j  ava2 s  .  c  om*/
 * The OutputSpecification is *not* provided to this method, because the goal is consistency, and if output is done it must be consistent with the
 * output description, since that was what was partly used to determine if output should be taking place.  So it may be necessary for this method to decode
 * an output description string in order to determine what should be done.
 *@param documentURI is the URI of the document.  The URI is presumed to be the unique identifier which the output data store will use to process
 * and serve the document.  This URI is constructed by the repository connector which fetches the document, and is thus universal across all output connectors.
 *@param outputDescription is the description string that was constructed for this document by the getOutputDescription() method.
 *@param document is the document data to be processed (handed to the output data store).
 *@param authorityNameString is the name of the authority responsible for authorizing any access tokens passed in with the repository document.  May be null.
 *@param activities is the handle to an object that the implementer of an output connector may use to perform operations, such as logging processing activity.
 *@return the document status (accepted or permanently rejected).
 */
@Override
public int addOrReplaceDocumentWithException(String documentURI, VersionContext outputDescription,
        RepositoryDocument document, String authorityNameString, IOutputAddActivity activities)
        throws ManifoldCFException, ServiceInterruption, IOException {
    // Establish a session
    getSession();

    FileOutputConfig config = getConfigParameters(null);

    FileOutputSpecs specs = new FileOutputSpecs(getSpecNode(outputDescription.getSpecification()));
    ;
    StringBuffer path = new StringBuffer();

    String errorCode = "OK";
    String errorDesc = null;

    try {
        /*
          * make file path
          */
        if (specs.getRootPath() != null) {
            path.append(specs.getRootPath());
        }

        // If the path does not yet exist at the root level, it is dangerous to create it.
        File currentPath = new File(path.toString());
        if (!currentPath.exists())
            throw new ManifoldCFException("Root path does not yet exist: '" + currentPath + "'");
        if (!currentPath.isDirectory())
            throw new ManifoldCFException("Root path is not a directory: '" + currentPath + "'");

        String filePath = documentURItoFilePath(documentURI);

        // Build path one level at a time.  This is needed because there may be a collision at
        // every level.
        int index = 0;
        while (true) {
            int currentIndex = filePath.indexOf("/", index);
            if (currentIndex == -1)
                break;
            String dirName = filePath.substring(index, currentIndex);
            File newPath = new File(currentPath, dirName);
            index = currentIndex + 1;
            int suffix = 1;
            while (true) {
                if (newPath.exists() && newPath.isDirectory())
                    break;
                // Try to create it.  If we fail, check if it now exists as a file.
                if (newPath.mkdir())
                    break;
                // Hmm, didn't create.  If it is a file, we suffered a collision, so try again with ".N" as a suffix.
                if (newPath.exists()) {
                    if (newPath.isDirectory())
                        break;
                    newPath = new File(currentPath, dirName + "." + suffix);
                    suffix++;
                } else {
                    errorCode = activities.CREATED_DIRECTORY;
                    errorDesc = "Could not create directory '\"+newPath+\"'.  Permission issue?";
                    throw new ManifoldCFException(errorDesc);
                }
            }
            // Directory successfully created!
            currentPath = newPath;
            // Go on to the next one.
        }

        // Path successfully created.  Now create file.
        FileOutputStream output = null;
        String fileName = filePath.substring(index);
        File outputPath = new File(currentPath, fileName);
        int fileSuffix = 1;
        while (true) {
            try {
                output = new FileOutputStream(outputPath);
                break;
            } catch (FileNotFoundException e) {
                // Figure out why it could not be created.
                if (outputPath.exists() && !outputPath.isFile()) {
                    // try a new file
                    outputPath = new File(currentPath, fileName + "." + fileSuffix);
                    fileSuffix++;
                    continue;
                }
                // Probably some other error
                errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT);
                errorDesc = "Could not create file '" + outputPath + "': " + e.getMessage();
                throw new ManifoldCFException(errorDesc, e);
            }
        }

        try {
            /*
              * lock file
              */
            FileChannel channel = output.getChannel();
            FileLock lock = channel.tryLock();
            if (lock == null) {
                errorCode = ServiceInterruption.class.getSimpleName().toUpperCase(Locale.ROOT);
                errorDesc = "Could not lock file: '" + outputPath + "'";
                throw new ServiceInterruption(errorDesc, null, 1000L, -1L, 10, false);
            }

            try {

                /*
                  * write file
                  */
                InputStream input = document.getBinaryStream();
                byte buf[] = new byte[65536];
                int len;
                while ((len = input.read(buf)) != -1) {
                    output.write(buf, 0, len);
                }
                output.flush();
            } finally {
                // Unlock
                try {
                    if (lock != null) {
                        lock.release();
                    }
                } catch (ClosedChannelException e) {
                }
            }
        } finally {
            try {
                output.close();
            } catch (IOException e) {
            }
        }
    } catch (URISyntaxException e) {
        errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT);
        errorDesc = "Failed to write document due to: " + e.getMessage();
        handleURISyntaxException(e);
        return DOCUMENTSTATUS_REJECTED;
    } catch (FileNotFoundException e) {
        errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT);
        errorDesc = "Failed to write document due to: " + e.getMessage();
        handleFileNotFoundException(e);
        return DOCUMENTSTATUS_REJECTED;
    } catch (SecurityException e) {
        errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT);
        errorDesc = "Failed to write document due to: " + e.getMessage();
        handleSecurityException(e);
        return DOCUMENTSTATUS_REJECTED;
    } catch (IOException e) {
        errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT);
        errorDesc = "Failed to write document due to: " + e.getMessage();
        handleIOException(e);
        return DOCUMENTSTATUS_REJECTED;
    } finally {
        activities.recordActivity(null, INGEST_ACTIVITY, new Long(document.getBinaryLength()), documentURI,
                errorCode, errorDesc);
    }

    return DOCUMENTSTATUS_ACCEPTED;
}

From source file:com.castis.sysComp.PoisConverterSysComp.java

private void writeFile(File file) throws IOException {

    Map<String, String> middleNodeMap = new HashMap<String, String>();

    String line = "";
    FileInputStream in = null;/*  ww w  .ja  va2  s .  c om*/
    Reader isReader = null;
    LineNumberReader bufReader = null;

    FileOutputStream fos = null;
    String dir = filePolling.getValidFileDirectory(resultDir);

    String fileName = file.getName();

    int index = fileName.indexOf("-");
    if (index != -1) {
        fileName = fileName.substring(index + 1, fileName.length());
    }

    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);
    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        String encodingCharset = FILE_CHARSET;
        in = new FileInputStream(file);
        isReader = new InputStreamReader(in, encodingCharset);
        bufReader = new LineNumberReader(isReader);

        boolean first = true;
        while ((line = bufReader.readLine()) != null) {

            if (line.length() == 0) {
                continue;
            }

            InputDataDTO data = new InputDataDTO();
            String result[] = line.split("\\|");

            if (first == true && result.length <= 1) {
                first = false;
                continue;
            }
            String platform = result[4];

            if (platform != null && platform.equalsIgnoreCase("stb"))
                platform = "STB";
            else if (platform != null && platform.equalsIgnoreCase("mobile")) {
                platform = "Mobile";
            }
            data.setPlatform(platform);

            List<TreeNodeDTO> tree = treeMap.get(platform);

            if (tree == null) {
                tree = getAxis(platform);
                treeMap.put(platform, tree);
            }

            String fullpath = getFullPath(tree, result[0]);

            data.setRegion(fullpath);
            data.setCategory(result[1]);
            data.setWeekday(result[2]);
            data.setHour(result[3]);
            data.setCount(Integer.parseInt(result[5]));

            List<subDataDTO> subDataList = writeNodeInfoOnFile(byteSize, byteBuffer, outByteCh, data, "Y");
            if (subDataList != null && subDataList.size() > 0) {
                writeMiddleNode(byteSize, byteBuffer, outByteCh, data, middleNodeMap, subDataList, "N");
            }
        }

        fos.close();

        index = fileName.indexOf("_");

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);
        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/viewCount/" + directory;
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line
                + "] : ";
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    } finally {
        if (in != null)
            in.close();
        if (isReader != null)
            isReader.close();
        if (bufReader != null)
            bufReader.close();
    }
}

From source file:com.castis.sysComp.PoisConverterSysComp.java

public void parseRegionFile(File file) throws Exception {
    String line = "";
    FileInputStream in = null;/*w w  w.jav  a2 s.c  o  m*/
    Reader isReader = null;
    LineNumberReader bufReader = null;

    FileOutputStream fos = null;
    String fileName = file.getName();

    int index = fileName.indexOf("-");
    if (index != -1) {
        fileName = fileName.substring(index + 1, fileName.length());
    }

    String dir = filePolling.getValidFileDirectory(resultDir);

    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);

    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        in = new FileInputStream(file);
        isReader = new InputStreamReader(in, "UTF-16LE");
        bufReader = new LineNumberReader(isReader);

        boolean first = true;
        while ((line = bufReader.readLine()) != null) {

            byte[] utf8 = line.getBytes("UTF-8");
            String string = new String(utf8, "UTF-8");

            String data[] = string.split("\t");

            if (first == true) {
                first = false;

                if (data[0] == null || data[0].contains("region") == false) {
                    throw new DataParsingException("data parsing error(not formatted)");
                }
                continue;
            }

            if (data[0] == null || data[0].equals("")) {
                throw new DataParsingException("data parsing error(region id)");
            }
            if (data[1] == null || data[1].equals("")) {
                throw new DataParsingException("data parsing error(region name)");
            }
            if (data[2] == null || data[2].equals("")) {
                throw new DataParsingException("data parsing error(parent id)");
            }

            StringBuffer strBuffer = new StringBuffer();
            strBuffer.append(data[0]);
            strBuffer.append("\t");
            strBuffer.append(data[1]);
            strBuffer.append("\t");
            strBuffer.append(data[2]);

            strBuffer.append("\r\n");

            byte[] outByte = null;
            try {
                outByte = strBuffer.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
            }
            byteBuffer.put(outByte);
            byteBuffer.flip();
            try {
                outByteCh.write(byteBuffer);
            } catch (IOException e) {
            }
            byteBuffer.clear();
        }

        fos.close();

        index = fileName.indexOf("_");

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);
        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/" + directory;
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line
                + "] : ";
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    } finally {
        if (in != null)
            in.close();
        if (isReader != null)
            isReader.close();
        if (bufReader != null)
            bufReader.close();
    }
}

From source file:com.vegnab.vegnab.MainVNActivity.java

public void copyFile(File src, File dst) throws IOException {
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dst);
    FileChannel fromChannel = null, toChannel = null;
    try {/* ww  w .  java2s  .  c om*/
        fromChannel = in.getChannel();
        toChannel = out.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        if (fromChannel != null)
            fromChannel.close();
        if (toChannel != null)
            toChannel.close();
    }
    in.close();
    out.close();
    // must do following or file is not visible externally
    MediaScannerConnection.scanFile(getApplicationContext(), new String[] { dst.getAbsolutePath() }, null,
            null);
}

From source file:com.example.util.FileUtils.java

/**
 * Internal copy file method.//from  www. jav a 2  s.c  o  m
 * 
 * @param srcFile  the validated source file, must not be {@code null}
 * @param destFile  the validated destination file, must not be {@code null}
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
            pos += output.transferFrom(input, pos, count);
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}

From source file:com.matteoveroni.model.copy.FileUtils.java

/**
 * Internal copy file method./*from  w w w  . j a v a2s .  c  o m*/
 * 
 * @param srcFile  the validated source file, must not be {@code null}
 * @param destFile  the validated destination file, must not be {@code null}
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        bytesTransferedTotal = 0;
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;

            //pos += output.transferFrom(input, pos, count);

            //Split into into deceleration and assignment to count bytes transfered
            long bytesTransfered = output.transferFrom(input, pos, count);
            //complete original method
            pos += bytesTransfered;
            //update total bytes copied, so it can be used to calculate progress
            bytesTransferedTotal += bytesTransfered;
            System.out.println((int) Math.floor((100.0 / size) * bytesTransferedTotal) + "%");
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}

From source file:com.benefit.buy.library.http.query.AbstractAQuery.java

/**
 * Create a temporary file on EXTERNAL storage (sdcard) that holds the cached content of the url. Returns null if
 * url is not cached, or the system cannot create such file (sdcard is absent, such as in emulator). The returned
 * file is accessable to all apps, therefore it is ideal for sharing content (such as photo) via the intent
 * mechanism. <br>/*from  w  w  w. ja  v a2  s.c o m*/
 * <br>
 * Example Usage:
 * 
 * <pre>
 * Intent intent = new Intent(Intent.ACTION_SEND);
 * intent.setType(&quot;image/jpeg&quot;);
 * intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
 * startActivityForResult(Intent.createChooser(intent, &quot;Share via:&quot;), 0);
 * </pre>
 * 
 * <br>
 * The temp file will be deleted when AQUtility.cleanCacheAsync is invoked, or the file can be explicitly deleted
 * after use.
 * @param url The url of the desired cached content.
 * @param filename The desired file name, which might be used by other apps to describe the content, such as an
 *            email attachment.
 * @return temp file
 */
public File makeSharedFile(String url, String filename) {
    File file = null;
    try {
        File cached = getCachedFile(url);
        if (cached != null) {
            File temp = AQUtility.getTempDir();
            if (temp != null) {
                file = new File(temp, filename);
                file.createNewFile();
                FileInputStream fis = new FileInputStream(cached);
                FileOutputStream fos = new FileOutputStream(file);
                FileChannel ic = fis.getChannel();
                FileChannel oc = fos.getChannel();
                try {
                    ic.transferTo(0, ic.size(), oc);
                } finally {
                    AQUtility.close(fis);
                    AQUtility.close(fos);
                    AQUtility.close(ic);
                    AQUtility.close(oc);
                }
            }
        }
    } catch (Exception e) {
        AQUtility.debug(e);
    }
    return file;
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetImpl.java

/**
 * Sets the offset in the meta file so that the
 * last checksum will be overwritten.//from ww  w .ja  va  2  s  .c  o m
 */
@Override // FsDatasetSpi
public void adjustCrcChannelPosition(ExtendedBlock b, ReplicaOutputStreams streams, int checksumSize)
        throws IOException {
    FileOutputStream file = (FileOutputStream) streams.getChecksumOut();
    FileChannel channel = file.getChannel();
    long oldPos = channel.position();
    long newPos = oldPos - checksumSize;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Changing meta file offset of block " + b + " from " + oldPos + " to " + newPos);
    }
    channel.position(newPos);
}

From source file:JNLPAppletLauncher.java

private void validateCache(URLConnection conn, File nativeFile, File indexFile) throws IOException {

    // Lock the cache directory
    final String lckFileName = "cache.lck";
    File lckFile = new File(cacheDir, lckFileName);
    lckFile.createNewFile();/*from  w ww .  ja v a 2  s. c o m*/
    final FileOutputStream lckOut = new FileOutputStream(lckFile);
    final FileChannel lckChannel = lckOut.getChannel();
    final FileLock lckLock = lckChannel.lock();

    try {
        // Check to see whether the cached jar file exists and is valid
        boolean valid = false;
        long cachedTimeStamp = readTimeStamp(indexFile);
        long urlTimeStamp = conn.getLastModified();

        if (nativeFile.exists() && urlTimeStamp > 0 && urlTimeStamp == readTimeStamp(indexFile)) {

            valid = true;
        }

        // Validate the cache, download the jar if needed
        if (!valid) {
            if (VERBOSE) {
                System.err.println("processNativeJar: downloading " + nativeFile.getAbsolutePath());
            }
            indexFile.delete();
            nativeFile.delete();

            // Copy from URL to File
            int len = conn.getContentLength();
            if (VERBOSE) {
                System.err.println("Content length = " + len + " bytes");
            }

            int totalNumBytes = copyURLToFile(conn, nativeFile);
            if (DEBUG) {
                System.err.println("processNativeJar: " + conn.getURL().toString() + " --> "
                        + nativeFile.getAbsolutePath() + " : " + totalNumBytes + " bytes written");
            }

            // Write timestamp to index file.
            writeTimeStamp(indexFile, urlTimeStamp);

        } else {
            if (DEBUG) {
                System.err
                        .println("processNativeJar: using previously cached: " + nativeFile.getAbsolutePath());
            }
        }
    } finally {
        // Unlock the cache directory
        lckLock.release();
    }
}

From source file:com.androidquery.AbstractAQuery.java

/**
 * Create a temporary file on EXTERNAL storage (sdcard) that holds the cached content of the url.
 * Returns null if url is not cached, or the system cannot create such file (sdcard is absent, such as in emulator).
 * /*  w w w. j ava 2 s  . c om*/
 * The returned file is accessable to all apps, therefore it is ideal for sharing content (such as photo) via the intent mechanism.
 * 
 * <br>
 * <br>
 * Example Usage:
 * 
 * <pre>
 *   Intent intent = new Intent(Intent.ACTION_SEND);
 *   intent.setType("image/jpeg");
 *   intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
 *   startActivityForResult(Intent.createChooser(intent, "Share via:"), 0);
 * </pre>
 * 
 * <br>
 * The temp file will be deleted when AQUtility.cleanCacheAsync is invoked, or the file can be explicitly deleted after use.
 * 
 * @param url The url of the desired cached content.
 * @param filename The desired file name, which might be used by other apps to describe the content, such as an email attachment.
 * @return temp file
 * 
 */

public File makeSharedFile(String url, String filename) {

    File file = null;

    try {

        File cached = getCachedFile(url);

        if (cached != null) {

            File temp = AQUtility.getTempDir();

            if (temp != null) {

                file = new File(temp, filename);
                file.createNewFile();

                FileInputStream fis = new FileInputStream(cached);
                FileOutputStream fos = new FileOutputStream(file);

                FileChannel ic = fis.getChannel();
                FileChannel oc = fos.getChannel();

                try {
                    ic.transferTo(0, ic.size(), oc);
                } finally {
                    AQUtility.close(fis);
                    AQUtility.close(fos);
                    AQUtility.close(ic);
                    AQUtility.close(oc);
                }

            }
        }

    } catch (Exception e) {
        AQUtility.debug(e);
    }

    return file;
}