Example usage for java.io RandomAccessFile getChannel

List of usage examples for java.io RandomAccessFile getChannel

Introduction

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

Prototype

public final FileChannel getChannel() 

Source Link

Document

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

Usage

From source file:org.alfresco.repo.content.filestore.FileContentReader.java

@Override
protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException {
    try {//from  w w w.  j a  v a 2 s .c  o  m
        // the file must exist
        if (!file.exists()) {
            throw new IOException("File does not exist: " + file);
        }
        // create the channel
        ReadableByteChannel channel = null;
        if (allowRandomAccess) {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); // won't create it
            channel = randomAccessFile.getChannel();
        } else {
            InputStream is = new FileInputStream(file);
            channel = Channels.newChannel(is);
        }
        // done
        if (logger.isDebugEnabled()) {
            logger.debug("Opened read channel to file: \n" + "   file: " + file + "\n" + "   random-access: "
                    + allowRandomAccess);
        }
        return channel;
    } catch (Throwable e) {
        throw new ContentIOException("Failed to open file channel: " + this, e);
    }
}

From source file:com.cerema.cloud2.lib.resources.files.ChunkedUploadRemoteFileOperation.java

@Override
protected int uploadFile(OwnCloudClient client) throws IOException {
    int status = -1;

    FileChannel channel = null;//from   www . java  2  s  .  co  m
    RandomAccessFile raf = null;
    try {
        File file = new File(mLocalPath);
        raf = new RandomAccessFile(file, "r");
        channel = raf.getChannel();
        mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file);
        synchronized (mDataTransferListeners) {
            ((ProgressiveDataTransferer) mEntity).addDatatransferProgressListeners(mDataTransferListeners);
        }

        long offset = 0;
        String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-"
                + Math.abs((new Random()).nextInt(9000) + 1000) + "-";
        long totalLength = file.length();
        long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE);
        String chunkSizeStr = String.valueOf(CHUNK_SIZE);
        String totalLengthStr = String.valueOf(file.length());
        for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) {
            if (chunkIndex == chunkCount - 1) {
                chunkSizeStr = String.valueOf(CHUNK_SIZE * chunkCount - totalLength);
            }
            if (mPutMethod != null) {
                mPutMethod.releaseConnection(); // let the connection available
                                                // for other methods
            }
            mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
            if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
                mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
            }
            mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
            mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
            mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
            ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
            mPutMethod.setRequestEntity(mEntity);
            if (mCancellationRequested.get()) {
                mPutMethod.abort();
                // next method will throw an exception
            }
            status = client.executeMethod(mPutMethod);

            if (status == 400) {
                InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
                InputStream is = new ByteArrayInputStream(mPutMethod.getResponseBodyAsString().getBytes());
                try {
                    mForbiddenCharsInServer = xmlParser.parseXMLResponse(is);

                } catch (Exception e) {
                    mForbiddenCharsInServer = false;
                    Log_OC.e(TAG, "Exception reading exception from server", e);
                }
            }

            client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
            Log_OC.d(TAG, "Upload of " + mLocalPath + " to " + mRemotePath + ", chunk index " + chunkIndex
                    + ", count " + chunkCount + ", HTTP result status " + status);

            if (!isSuccess(status))
                break;
        }

    } finally {
        if (channel != null)
            channel.close();
        if (raf != null)
            raf.close();
        if (mPutMethod != null)
            mPutMethod.releaseConnection(); // let the connection available for other methods
    }
    return status;
}

From source file:org.apache.hadoop.io.TestIOUtils.java

@Test
public void testWriteFully() throws IOException {
    final int INPUT_BUFFER_LEN = 10000;
    final int HALFWAY = 1 + (INPUT_BUFFER_LEN / 2);
    byte[] input = new byte[INPUT_BUFFER_LEN];
    for (int i = 0; i < input.length; i++) {
        input[i] = (byte) (i & 0xff);
    }/*from   w  w w .java  2  s  .c  o m*/
    byte[] output = new byte[input.length];

    try {
        RandomAccessFile raf = new RandomAccessFile(TEST_FILE_NAME, "rw");
        FileChannel fc = raf.getChannel();
        ByteBuffer buf = ByteBuffer.wrap(input);
        IOUtils.writeFully(fc, buf);
        raf.seek(0);
        raf.read(output);
        for (int i = 0; i < input.length; i++) {
            assertEquals(input[i], output[i]);
        }
        buf.rewind();
        IOUtils.writeFully(fc, buf, HALFWAY);
        for (int i = 0; i < HALFWAY; i++) {
            assertEquals(input[i], output[i]);
        }
        raf.seek(0);
        raf.read(output);
        for (int i = HALFWAY; i < input.length; i++) {
            assertEquals(input[i - HALFWAY], output[i]);
        }
    } finally {
        File f = new File(TEST_FILE_NAME);
        if (f.exists()) {
            f.delete();
        }
    }
}

From source file:com.qubole.rubix.core.RemoteReadRequestChain.java

public Integer call() throws IOException {
    Thread.currentThread().setName(threadName);
    checkState(isLocked, "Trying to execute Chain without locking");

    if (readRequests.size() == 0) {
        return 0;
    }/*  www .  j a  v  a 2  s  .  c o m*/

    RandomAccessFile localFile = null;
    FileChannel fc = null;

    try {
        localFile = new RandomAccessFile(localFilename, "rw");
        fc = localFile.getChannel();
        for (ReadRequest readRequest : readRequests) {
            log.debug(String.format("Executing ReadRequest: [%d, %d, %d, %d, %d]",
                    readRequest.getBackendReadStart(), readRequest.getBackendReadEnd(),
                    readRequest.getActualReadStart(), readRequest.getActualReadEnd(),
                    readRequest.getDestBufferOffset()));
            inputStream.seek(readRequest.backendReadStart);
            MappedByteBuffer mbuf = fc.map(FileChannel.MapMode.READ_WRITE, readRequest.backendReadStart,
                    readRequest.getBackendReadLength());
            log.debug(String.format("Mapped file from %d till length %d", readRequest.backendReadStart,
                    readRequest.getBackendReadLength()));
            /*
             * MappedByteBuffer does not provide backing byte array, so cannot write directly to it via FSDataOutputStream.read
             * Instead, download to normal destination buffer (+offset buffer to get block boundaries) and then copy to MappedByteBuffer
             */

            int prefixBufferLength = (int) (readRequest.getActualReadStart()
                    - readRequest.getBackendReadStart());
            int suffixBufferLength = (int) (readRequest.getBackendReadEnd() - readRequest.getActualReadEnd());
            log.debug(
                    String.format("PrefixLength: %d SuffixLength: %d", prefixBufferLength, suffixBufferLength));

            // TODO: use single byte buffer for all three streams
            /* TODO: also GC cost can be lowered by shared buffer pool, a small one.
            IOUtils.copyLarge method. A single 4kB byte buffer can be used to copy whole file
                    
              */
            if (prefixBufferLength > 0) {
                byte[] prefixBuffer = new byte[prefixBufferLength];
                log.debug(String.format("Trying to Read %d bytes into prefix buffer", prefixBufferLength));
                totalPrefixRead += readAndCopy(prefixBuffer, 0, mbuf, prefixBufferLength);
                log.debug(String.format("Read %d bytes into prefix buffer", prefixBufferLength));
            }
            log.debug(String.format("Trying to Read %d bytes into destination buffer",
                    readRequest.getActualReadLength()));
            int readBytes = readAndCopy(readRequest.getDestBuffer(), readRequest.destBufferOffset, mbuf,
                    readRequest.getActualReadLength());
            totalRequestedRead += readBytes;
            log.debug(String.format("Read %d bytes into destination buffer", readBytes));
            if (suffixBufferLength > 0) {
                // If already in reading actually required data we get a eof, then there should not have been a suffix request
                checkState(readBytes == readRequest.getActualReadLength(),
                        "Acutal read less than required, still requested for suffix");
                byte[] suffixBuffer = new byte[suffixBufferLength];
                log.debug(String.format("Trying to Read %d bytes into suffix buffer", suffixBufferLength));
                totalSuffixRead += readAndCopy(suffixBuffer, 0, mbuf, suffixBufferLength);
                log.debug(String.format("Read %d bytes into suffix buffer", suffixBufferLength));
            }
        }
    } finally {
        if (fc != null) {
            fc.close();
        }
        if (localFile != null) {
            localFile.close();
        }
    }
    log.info(String.format("Read %d bytes from remote file, added %d to destination buffer",
            totalPrefixRead + totalRequestedRead + totalSuffixRead, totalRequestedRead));
    return totalRequestedRead;
}

From source file:org.pentaho.di.trans.steps.excelinput.PoiWorkBookTest.java

@Test
public void testResourceFree() throws Exception {
    FileLock lock = null;//from   ww w  .  j a va  2 s. c  o  m
    RandomAccessFile randomAccessFile = null;
    try {
        readData();
        File fileAfterRead = new File("testfiles/sample-file.xlsx");
        randomAccessFile = new RandomAccessFile(fileAfterRead, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        lock = fileChannel.tryLock();
        // check that we could lock file
        assertTrue(lock.isValid());
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

From source file:fi.vtt.nubomedia.armodule.Ar3DHandler.java

private String getFile(String path) throws IOException {
    RandomAccessFile in = new RandomAccessFile(new File(path), "r");
    FileChannel ch = in.getChannel();
    long size = ch.size();
    byte[] buf = new byte[(int) size];
    in.read(buf, 0, buf.length);// w ww.ja  v  a2s  . c  o m
    in.close();
    return new String(buf);
}

From source file:org.apache.hadoop.mapred.FadvisedFileRegion.java

public FadvisedFileRegion(RandomAccessFile file, long position, long count, boolean manageOsCache,
        int readaheadLength, ReadaheadPool readaheadPool, String identifier, int shuffleBufferSize,
        boolean shuffleTransferToAllowed) throws IOException {
    super(file.getChannel(), position, count);
    this.manageOsCache = manageOsCache;
    this.readaheadLength = readaheadLength;
    this.readaheadPool = readaheadPool;
    this.fd = file.getFD();
    this.identifier = identifier;
    this.fileChannel = file.getChannel();
    this.count = count;
    this.position = position;
    this.shuffleBufferSize = shuffleBufferSize;
    this.shuffleTransferToAllowed = shuffleTransferToAllowed;
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void appendSitemapIndex(String sitemap) throws IOException {
    File sitemapIndexFile = new File(mapResourcesRoot.getFile(), SITEMAP_INDEX_NAME);
    if (!sitemapIndexFile.exists()) {
        //if a new sitemap file
        List<String> lines = new ArrayList<String>();
        lines.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        lines.add("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        lines.add("</sitemapindex>");
        FileUtils.writeLines(sitemapIndexFile, lines);
    }/*from  ww w  .j av a2s  .c  o m*/

    RandomAccessFile rfile = new RandomAccessFile(sitemapIndexFile, "rw");
    FileChannel channel = rfile.getChannel();

    //this new content will append to end of file before XML end tag
    StringBuilder lines = new StringBuilder();
    lines.append("   <sitemap>\n");
    lines.append("     <loc>" + WebUtil.getHostAppURL() + SITEMAP_URL_CONTEXT + sitemap + "</loc>\n");
    lines.append("     <lastmod>" + TIME_FORMAT.format(new Date()) + " </lastmod>\n");
    lines.append("   </sitemap>\n");
    //the last tag will be overwrite, so append it again to new content. 
    lines.append(SITEMAP_INDEX_TAIL_FLAG);
    byte[] content = lines.toString().getBytes();

    ByteBuffer byteBuf = ByteBuffer.allocate(512);
    // seek first
    int len = 0, headIdx = 0;
    long tailIdx = channel.size() - 512;
    tailIdx = tailIdx < 0 ? 0 : tailIdx;

    long headPos = -1;
    StringBuilder header = new StringBuilder();
    while ((len = channel.read(byteBuf, tailIdx)) > 0) {
        byteBuf.rewind();
        byte[] dst = new byte[len];
        byteBuf.get(dst, 0, len);
        header.append(new String(dst, "UTF8"));
        headIdx = header.indexOf(SITEMAP_INDEX_TAIL_FLAG);
        if (headIdx != -1) {
            headPos = channel.size() - header.substring(headIdx).getBytes().length;
            break;
        }
    }
    FileLock lock = channel.tryLock(headPos, content.length, false);
    try {
        channel.write(ByteBuffer.wrap(content), headPos);
    } finally {
        lock.release();
    }

    channel.force(false);
    rfile.close();

}

From source file:org.pentaho.di.trans.steps.excelinput.PoiWorkBookIT.java

License:asdf

@Test
public void testResourceFree() throws Exception {
    FileLock lock = null;/*from  ww  w .ja va 2  s  . c  o m*/
    RandomAccessFile randomAccessFile = null;
    try {
        readData(sampleFile, null);
        File fileAfterRead = new File(sampleFile);
        randomAccessFile = new RandomAccessFile(fileAfterRead, "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        lock = fileChannel.tryLock();
        // check that we could lock file
        assertTrue(lock.isValid());
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

From source file:com.twinsoft.convertigo.beans.steps.WriteXMLStep.java

protected void writeFile(String filePath, NodeList nodeList) throws EngineException {
    if (nodeList == null) {
        throw new EngineException("Unable to write to xml file: element is Null");
    }//from www  .jav a  2  s. c om

    String fullPathName = getAbsoluteFilePath(filePath);
    synchronized (Engine.theApp.filePropertyManager.getMutex(fullPathName)) {
        try {
            String encoding = getEncoding();
            encoding = encoding.length() > 0 && Charset.isSupported(encoding) ? encoding : "UTF-8";
            if (!isReallyAppend(fullPathName)) {
                String tTag = defaultRootTagname.length() > 0 ? StringUtils.normalize(defaultRootTagname)
                        : "document";
                FileUtils.write(new File(fullPathName),
                        "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n<" + tTag + "/>", encoding);
            }

            StringBuffer content = new StringBuffer();

            /* do the content, only append child element */
            for (int i = 0; i < nodeList.getLength(); i++) {
                if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    content.append(XMLUtils.prettyPrintElement((Element) nodeList.item(i), true, true));
                }
            }

            /* detect current xml encoding */
            RandomAccessFile randomAccessFile = null;
            try {
                randomAccessFile = new RandomAccessFile(fullPathName, "rw");
                FileChannel fc = randomAccessFile.getChannel();
                ByteBuffer buf = ByteBuffer.allocate(60);
                int nb = fc.read(buf);
                String sbuf = new String(buf.array(), 0, nb, "ASCII");
                String enc = sbuf.replaceFirst("^.*encoding=\"", "").replaceFirst("\"[\\d\\D]*$", "");

                if (!Charset.isSupported(enc)) {
                    enc = encoding;
                }

                buf.clear();

                /* retrieve last header tag*/
                long pos = fc.size() - buf.capacity();
                if (pos < 0) {
                    pos = 0;
                }

                nb = fc.read(buf, pos);

                boolean isUTF8 = Charset.forName(enc) == Charset.forName("UTF-8");

                if (isUTF8) {
                    for (int i = 0; i < buf.capacity(); i++) {
                        sbuf = new String(buf.array(), i, nb - i, enc);
                        if (!sbuf.startsWith("")) {
                            pos += i;
                            break;
                        }
                    }
                } else {
                    sbuf = new String(buf.array(), 0, nb, enc);
                }

                int lastTagIndex = sbuf.lastIndexOf("</");
                if (lastTagIndex == -1) {
                    int iend = sbuf.lastIndexOf("/>");
                    if (iend != -1) {
                        lastTagIndex = sbuf.lastIndexOf("<", iend);
                        String tagname = sbuf.substring(lastTagIndex + 1, iend);
                        content = new StringBuffer(
                                "<" + tagname + ">\n" + content.toString() + "</" + tagname + ">");
                    } else {
                        throw new EngineException("Malformed XML file");
                    }
                } else {
                    content.append(sbuf.substring(lastTagIndex));

                    if (isUTF8) {
                        String before = sbuf.substring(0, lastTagIndex);
                        lastTagIndex = before.getBytes(enc).length;
                    }
                }
                fc.write(ByteBuffer.wrap(content.toString().getBytes(enc)), pos + lastTagIndex);
            } finally {
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
            }
        } catch (IOException e) {
            throw new EngineException("Unable to write to xml file", e);
        } finally {
            Engine.theApp.filePropertyManager.releaseMutex(fullPathName);
        }
    }
}