Example usage for java.io RandomAccessFile setLength

List of usage examples for java.io RandomAccessFile setLength

Introduction

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

Prototype

public native void setLength(long newLength) throws IOException;

Source Link

Document

Sets the length of this file.

Usage

From source file:org.apache.sshd.server.filesystem.NativeSshFile.java

/**
 * Create output stream for writing./*w  w  w  . j av  a  2 s.co  m*/
 */
public OutputStream createOutputStream(final long offset) throws IOException {

    // permission check
    if (!isWritable()) {
        throw new IOException("No write permission : " + file.getName());
    }

    // create output stream
    final RandomAccessFile raf = new RandomAccessFile(file, "rw");
    raf.setLength(offset);
    raf.seek(offset);

    // The IBM jre needs to have both the stream and the random access file
    // objects closed to actually close the file
    return new FileOutputStream(raf.getFD()) {
        public void close() throws IOException {
            super.close();
            raf.close();
        }
    };
}

From source file:org.rhq.core.pluginapi.util.ResponseTimeLogParser.java

private void truncateLog(File logFile) throws IOException {
    log.debug("Truncating response-time log file: '" + logFile + "'...");
    RandomAccessFile randomAccessFile = null;

    try {/*  ww w  . ja v a2s  .c o  m*/
        String mode = "rws";
        randomAccessFile = new RandomAccessFile(logFile, mode);
        log.debug("Truncating response-time log file: setting length to 0.");
        randomAccessFile.setLength(0);
    } catch (SecurityException e) {
        /* User doesn't have permission to change the length, so
         * ignore this exception.
         */
        log.debug("Unable to truncate response-time log file.", e);
    } catch (FileNotFoundException e) {
        /* Can't happen.  We have just parsed this file.
         * Could be a permission error.  Log it.
         */
        log.error("Unable to truncate response-time log file.", e);
    } finally {
        if (null != randomAccessFile) {
            try {
                log.debug("Truncating response-time log file: closing file.");
                randomAccessFile.close();
            } catch (Exception e) {
                log.error("Unable to close response-time log file.", e);
            }
        }
    }
}

From source file:com.alibaba.otter.node.etl.common.pipe.impl.http.AbstractHttpPipe.java

protected EncryptedData encryptFile(File file) {
    // ?file path??
    EncryptedData encryptedData = null;/*from w w w.  j  a v a  2  s.  c  om*/
    try {
        encryptedData = EncryptUtils.encrypt(file.getPath().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // ignore
    }

    // ?
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(file, "rw");
        long origLength = file.length();
        int keyLength = ByteUtils.stringToBytes(encryptedData.getKey()).length;
        int crcLength = ByteUtils.stringToBytes(encryptedData.getCrc()).length;
        long totalLength = origLength + crcLength + keyLength;
        raf.setLength(totalLength);
        raf.seek(origLength);
        raf.write(ByteUtils.stringToBytes(encryptedData.getKey()), 0, keyLength);
        raf.seek(origLength + keyLength);
        raf.write(ByteUtils.stringToBytes(encryptedData.getCrc()), 0, crcLength);
    } catch (Exception e) {
        throw new PipeException("write_encrypted_error", e);
    } finally {
        IOUtils.closeQuietly(raf);
    }

    return encryptedData;
}

From source file:com.vincestyling.netroid.request.FileDownloadRequest.java

/**
 * In this method, we got the Content-Length, with the TemporaryFile length,
 * we can calculate the actually size of the whole file, if TemporaryFile not exists,
 * we'll take the store file length then compare to actually size, and if equals,
 * we consider this download was already done.
 * We used {@link RandomAccessFile} to continue download, when download success,
 * the TemporaryFile will be rename to StoreFile.
 *//* w  w  w.  j  a v a 2s  .  c om*/
@Override
public byte[] handleResponse(HttpResponse response, Delivery delivery) throws IOException, ServerError {
    // Content-Length might be negative when use HttpURLConnection because it default header Accept-Encoding is gzip,
    // we can force set the Accept-Encoding as identity in prepare() method to slove this problem but also disable gzip response.
    HttpEntity entity = response.getEntity();
    long fileSize = entity.getContentLength();
    if (fileSize <= 0) {
        NetroidLog.d("Response doesn't present Content-Length!");
    }

    long downloadedSize = mTemporaryFile.length();
    boolean isSupportRange = HttpUtils.isSupportRange(response);
    if (isSupportRange) {
        fileSize += downloadedSize;

        // Verify the Content-Range Header, to ensure temporary file is part of the whole file.
        // Sometime, temporary file length add response content-length might greater than actual file length,
        // in this situation, we consider the temporary file is invalid, then throw an exception.
        String realRangeValue = HttpUtils.getHeader(response, "Content-Range");
        // response Content-Range may be null when "Range=bytes=0-"
        if (!TextUtils.isEmpty(realRangeValue)) {
            String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1);
            if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) {
                throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue
                        + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file ["
                        + mTemporaryFile + "].");
            }
        }
    }

    // Compare the store file size(after download successes have) to server-side Content-Length.
    // temporary file will rename to store file after download success, so we compare the
    // Content-Length to ensure this request already download or not.
    if (fileSize > 0 && mStoreFile.length() == fileSize) {
        // Rename the store file to temporary file, mock the download success. ^_^
        mStoreFile.renameTo(mTemporaryFile);

        // Deliver download progress.
        delivery.postDownloadProgress(this, fileSize, fileSize);

        return null;
    }

    RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw");

    // If server-side support range download, we seek to last point of the temporary file.
    if (isSupportRange) {
        tmpFileRaf.seek(downloadedSize);
    } else {
        // If not, truncate the temporary file then start download from beginning.
        tmpFileRaf.setLength(0);
        downloadedSize = 0;
    }

    InputStream in = null;
    try {
        in = entity.getContent();
        // Determine the response gzip encoding, support for HttpClientStack download.
        if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) {
            in = new GZIPInputStream(in);
        }
        byte[] buffer = new byte[6 * 1024]; // 6K buffer
        int offset;

        while ((offset = in.read(buffer)) != -1) {
            tmpFileRaf.write(buffer, 0, offset);

            downloadedSize += offset;
            delivery.postDownloadProgress(this, fileSize, downloadedSize);

            if (isCanceled()) {
                delivery.postCancel(this);
                break;
            }
        }
    } finally {
        try {
            // Close the InputStream
            if (in != null)
                in.close();
        } catch (Exception e) {
            NetroidLog.v("Error occured when calling InputStream.close");
        }

        try {
            // release the resources by "consuming the content".
            entity.consumeContent();
        } catch (Exception e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            NetroidLog.v("Error occured when calling consumingContent");
        }
        tmpFileRaf.close();
    }

    return null;
}

From source file:org.apache.hadoop.hdfs.TestFileAppend3.java

/**
 * TC7: Corrupted replicas are present.//  www . j  a  v a 2 s. co  m
 * @throws IOException an exception might be thrown
 */
@Test
public void testTC7() throws Exception {
    final short repl = 2;
    final Path p = new Path("/TC7/foo");
    System.out.println("p=" + p);

    //a. Create file with replication factor of 2. Write half block of data. Close file.
    final int len1 = (int) (BLOCK_SIZE / 2);
    {
        FSDataOutputStream out = fs.create(p, false, buffersize, repl, BLOCK_SIZE);
        AppendTestUtil.write(out, 0, len1);
        out.close();
    }
    DFSTestUtil.waitReplication(fs, p, repl);

    //b. Log into one datanode that has one replica of this block.
    //   Find the block file on this datanode and truncate it to zero size.
    final LocatedBlocks locatedblocks = fs.dfs.getNamenode().getBlockLocations(p.toString(), 0L, len1);
    assertEquals(1, locatedblocks.locatedBlockCount());
    final LocatedBlock lb = locatedblocks.get(0);
    final ExtendedBlock blk = lb.getBlock();
    assertEquals(len1, lb.getBlockSize());

    DatanodeInfo[] datanodeinfos = lb.getLocations();
    assertEquals(repl, datanodeinfos.length);
    final DataNode dn = cluster.getDataNode(datanodeinfos[0].getIpcPort());
    final File f = DataNodeTestUtils.getBlockFile(dn, blk.getBlockPoolId(), blk.getLocalBlock());
    final RandomAccessFile raf = new RandomAccessFile(f, "rw");
    AppendTestUtil.LOG.info("dn=" + dn + ", blk=" + blk + " (length=" + blk.getNumBytes() + ")");
    assertEquals(len1, raf.length());
    raf.setLength(0);
    raf.close();

    //c. Open file in "append mode".  Append a new block worth of data. Close file.
    final int len2 = (int) BLOCK_SIZE;
    {
        FSDataOutputStream out = fs.append(p);
        AppendTestUtil.write(out, len1, len2);
        out.close();
    }

    //d. Reopen file and read two blocks worth of data.
    AppendTestUtil.check(fs, p, len1 + len2);
}

From source file:org.kchine.r.server.manager.ServerManager.java

public static RServices createRSsh(boolean keepAlive, String codeServerHostIp, int codeServerPort,
        Properties namingInfo, int memoryMinMegabytes, int memoryMaxMegabytes, String sshHostIp, int sshPort,
        String sshLogin, String sshPwd, String name, boolean showProgress, URL[] codeUrls, String logFile)
        throws BadSshHostException, BadSshLoginPwdException, Exception {

    if (showProgress) {
        createRSshProgressArea = new JTextArea();
        createRSshProgressBar = new JProgressBar(0, 100);
        createRSshProgressFrame = new JFrame("Create R Server via SSH");

        Runnable runnable = new Runnable() {
            public void run() {
                createRSshProgressArea.setFocusable(false);
                createRSshProgressBar.setIndeterminate(true);
                JPanel p = new JPanel(new BorderLayout());
                p.add(createRSshProgressBar, BorderLayout.SOUTH);
                p.add(new JScrollPane(createRSshProgressArea), BorderLayout.CENTER);
                createRSshProgressFrame.add(p);
                createRSshProgressFrame.pack();
                createRSshProgressFrame.setSize(300, 90);
                createRSshProgressFrame.setVisible(true);
                createRSshProgressFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                PoolUtils.locateInScreenCenter(createRSshProgressFrame);
            }//from w  w  w  . j a  v a 2  s.c o  m
        };

        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else {
            SwingUtilities.invokeLater(runnable);
        }
    }

    Connection conn = null;
    try {
        conn = new Connection(sshHostIp, sshPort);
        try {
            conn.connect();
        } catch (Exception e) {
            throw new BadSshHostException();
        }
        boolean isAuthenticated = conn.authenticateWithPassword(sshLogin, sshPwd);
        if (isAuthenticated == false)
            throw new BadSshLoginPwdException();

        InputStream is = ServerManager.class
                .getResourceAsStream("/org/kchine/r/server/manager/bootstrap/BootSsh.class");
        byte[] buffer = new byte[is.available()];
        try {
            for (int i = 0; i < buffer.length; ++i) {
                int b = is.read();
                buffer[i] = (byte) b;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String bootstrapDir = INSTALL_DIR + "classes/org/kchine/r/server/manager/bootstrap";
        new File(bootstrapDir).mkdirs();
        RandomAccessFile raf = new RandomAccessFile(bootstrapDir + "/BootSsh.class", "rw");
        raf.setLength(0);
        raf.write(buffer);
        raf.close();

        Session sess = null;
        try {
            sess = conn.openSession();
            sess.execCommand("mkdir -p RWorkbench/classes/org/kchine/r/server/manager/bootstrap");
            sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0);
        } finally {
            try {
                if (sess != null)
                    sess.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        new SCPClient(conn).put(bootstrapDir + "/BootSsh.class",
                "RWorkbench/classes/org/kchine/r/server/manager/bootstrap");
        try {
            sess = conn.openSession();

            String command = "java -classpath RWorkbench/classes org.kchine.r.server.manager.bootstrap.BootSsh"
                    + " " + new Boolean(keepAlive) + " " + codeServerHostIp + " " + codeServerPort + " "
                    + BootSsh.propertiesToString(namingInfo) + " " + "NULL" + " " + memoryMinMegabytes + " "
                    + memoryMaxMegabytes + " " + "System.out" + " "
                    + ((name == null || name.trim().equals("")) ? BootSsh.NO_NAME : name);

            if (codeUrls != null && codeUrls.length > 0) {
                for (int i = 0; i < codeUrls.length; ++i) {
                    command = command + " " + codeUrls[i];
                }
            }

            System.out.println("createRSsh command:" + command);
            sess.execCommand(command);

            InputStream stdout = new StreamGobbler(sess.getStdout());
            final BufferedReader brOut = new BufferedReader(new InputStreamReader(stdout));

            InputStream stderr = new StreamGobbler(sess.getStderr());
            final BufferedReader brErr = new BufferedReader(new InputStreamReader(stderr));
            final StringBuffer sshOutput = new StringBuffer();
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            String line = brOut.readLine();
                            if (line == null)
                                break;
                            sshOutput.append(line + "\n");
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Out Log Thread Died");
                }
            }).start();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            String line = brErr.readLine();
                            if (line == null)
                                break;
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Err Log Thread Died");
                }
            }).start();

            sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0);

            int eIndex = sshOutput.indexOf(BootSsh.STUB_END_MARKER);
            if (eIndex != -1) {
                int bIndex = sshOutput.indexOf(BootSsh.STUB_BEGIN_MARKER);
                String stub = sshOutput.substring(bIndex + BootSsh.STUB_BEGIN_MARKER.length(), eIndex);
                return (RServices) PoolUtils.hexToStub(stub, ServerManager.class.getClassLoader());
            } else {
                return null;
            }

        } finally {
            try {
                if (sess != null)
                    sess.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (showProgress) {
            createRSshProgressFrame.setVisible(false);
        }
    }
}

From source file:org.apache.hive.hcatalog.pig.TestHCatLoader.java

@Test
public void testGetInputBytes() throws Exception {
    assumeTrue(!TestUtil.shouldSkip(storageFormat, DISABLED_STORAGE_FORMATS));
    File file = new File(TEST_WAREHOUSE_DIR + "/" + SPECIFIC_SIZE_TABLE + "/part-m-00000");
    file.deleteOnExit();//from  w  ww  .j a va 2s.c o  m
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    randomAccessFile.setLength(2L * 1024 * 1024 * 1024);
    randomAccessFile.close();
    Job job = new Job();
    HCatLoader hCatLoader = new HCatLoader();
    hCatLoader.setUDFContextSignature("testGetInputBytes");
    hCatLoader.setLocation(SPECIFIC_SIZE_TABLE, job);
    ResourceStatistics statistics = hCatLoader.getStatistics(file.getAbsolutePath(), job);
    assertEquals(2048, (long) statistics.getmBytes());
}

From source file:org.wso2.msf4j.internal.router.HttpServerTest.java

public void testStreamUploadFailure() throws IOException {
    //create a random file to be uploaded.
    int size = 20 * 1024;
    File fname = tmpFolder.newFile();
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);
    randf.close();/*  ww  w  .j  a v a2s.co m*/

    HttpURLConnection urlConn = request("/test/v1/stream/upload/fail", HttpMethod.PUT);
    Files.copy(fname, urlConn.getOutputStream());
    Assert.assertEquals(500, urlConn.getResponseCode());
    urlConn.disconnect();
}

From source file:org.wso2.msf4j.internal.router.HttpServerTest.java

public void testChunkAggregatedUploadFailure() throws IOException {
    //create a random file to be uploaded.
    int size = 78 * 1024;
    File fname = tmpFolder.newFile();
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);
    randf.close();/*from   w w  w .  ja  v  a  2s .co m*/

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(fname, urlConn.getOutputStream());
    Assert.assertEquals(500, urlConn.getResponseCode());
    urlConn.disconnect();
}

From source file:org.wso2.msf4j.internal.router.HttpServerTest.java

@Test
public void testChunkAggregatedUpload() throws IOException {
    //create a random file to be uploaded.
    int size = 69 * 1024;
    File fname = tmpFolder.newFile();
    RandomAccessFile randf = new RandomAccessFile(fname, "rw");
    randf.setLength(size);
    randf.close();//from  w  w  w  .  ja va2s.c  o  m

    //test chunked upload
    HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT);
    urlConn.setChunkedStreamingMode(1024);
    Files.copy(fname, urlConn.getOutputStream());
    Assert.assertEquals(200, urlConn.getResponseCode());

    Assert.assertEquals(size, Integer.parseInt(getContent(urlConn).split(":")[1].trim()));
    urlConn.disconnect();
}