Description
Attempt to make a file executable.
License
Open Source License
Parameter
Parameter | Description |
---|
file | file |
Exception
Parameter | Description |
---|
IOException | on any error |
Declaration
public static void makeExecutable(File file) throws IOException
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
/**//from w w w . ja va 2s . com
* Default buffer size for stream utility methods
*/
public static int BUFFER_SIZE = 8192;
/**
* Attempt to make a file executable. Only current works on systems that
* have the <b>chmod</b> command available.
*
* @param file file
* @throws IOException on any error
*/
public static void makeExecutable(File file) throws IOException {
Process p = Runtime.getRuntime().exec(new String[] { "chmod", "ug+rx", file.getAbsolutePath() });
try {
copy(p.getErrorStream(), new ByteArrayOutputStream());
} finally {
try {
if (p.waitFor() != 0) {
throw new IOException("Failed to set execute permission. Return code " + p.exitValue() + ".");
}
} catch (InterruptedException e) {
}
}
}
/**
* Copy from an input stream to an output stream. It is up to the caller to
* close the streams.
*
* @param in input stream
* @param out output stream
* @throws IOException on any error
*/
public static void copy(InputStream in, OutputStream out) throws IOException {
copy(in, out, -1);
}
/**
* Copy the specified number of bytes from an input stream to an output
* stream. It is up to the caller to close the streams.
*
* @param in input stream
* @param out output stream
* @param count number of bytes to copy
* @throws IOException on any error
*/
public static void copy(InputStream in, OutputStream out, long count) throws IOException {
copy(in, out, count, BUFFER_SIZE);
}
/**
* Copy the specified number of bytes from an input stream to an output
* stream. It is up to the caller to close the streams.
*
* @param in input stream
* @param out output stream
* @param count number of bytes to copy
* @param bufferSize buffer size
* @throws IOException on any error
*/
public static void copy(InputStream in, OutputStream out, long count, int bufferSize) throws IOException {
byte buffer[] = new byte[bufferSize];
int i = bufferSize;
if (count >= 0) {
while (count > 0) {
if (count < bufferSize)
i = in.read(buffer, 0, (int) count);
else
i = in.read(buffer, 0, bufferSize);
if (i == -1)
break;
count -= i;
out.write(buffer, 0, i);
}
} else {
while (true) {
i = in.read(buffer, 0, bufferSize);
if (i < 0)
break;
out.write(buffer, 0, i);
}
}
}
}
Related
- javaExecutable(File jre)
- launchExecutable(String executable, List args, Preferences preferences)
- makeCurrentDirectoryThatOfExecutable(String executable)
- makeExecutable(File file)
- makeExecutable(File file)
- makeExecutable(File file)
- makeExecutable(File target)
- makeExecutable(String path)
- parallel(ExecutorService executor, Collection extends Callable>> tasks)