Example usage for java.io DataInputStream readChar

List of usage examples for java.io DataInputStream readChar

Introduction

In this page you can find the example usage for java.io DataInputStream readChar.

Prototype

public final char readChar() throws IOException 

Source Link

Document

See the general contract of the readChar method of DataInput.

Usage

From source file:org.ourgrid.common.executor.FolderBasedSandboxedUnixEnvironmentUtil.java

/**
 * A script created to run the command passed as a param. This is
 * the script that will be executed in an Unix secure environment.
 * /*from w w w. j  a v  a 2 s  .  co  m*/
 * @param command
 * @param dirName
 * @param envVars
 * @return
 * @throws ExecutorException
 */
public File createScript(String command, String dirName, Map<String, String> envVars) throws ExecutorException {

    File dir = new File(dirName); // The execution directory
    File commandFile = null; // The abstraction for the command
    boolean isScript = false; // Indicate if the command is already an script

    if (dirName == null) {// Check if dir is not null. Convert to "." to void problems
        dirName = ".";
    } else if (!dirName.equals(".") && !dir.isDirectory()) {
        throw new ExecutorException(command, new FileNotFoundException(dir.getAbsolutePath()));
    }

    /*
     * try to figure out if command is already a script note that this is
     * incomplete as it only works if the script is in dirName
     */
    try {
        commandFile = new File(dir, command);
        if (commandFile.exists() && commandFile.canRead()) {
            FileInputStream commandFIS = new FileInputStream(commandFile);
            DataInputStream commandDIS = new DataInputStream(commandFIS);
            if (commandDIS.readChar() == '#' && commandDIS.readChar() == '!') {
                isScript = true;
            }
            /** Close to avoid the "Too many open files" message and release resources */
            commandFIS.close();
            commandDIS.close();
        }
    } catch (FileNotFoundException e) {
        throw new ExecutorException(command, new FileNotFoundException(commandFile.getAbsolutePath()));
    } catch (IOException e) {
        throw new ExecutorException(command, e);
    }

    File temp;
    BufferedWriter writerTemp;
    String exportCommand = "export ";

    /** Try create the temporary script in fact.*/
    try {
        temp = TempFileManager.createTempFile("broker", ".tmp", dir);
        writerTemp = new BufferedWriter(new FileWriter(temp));

        /* If the command does not need any kind of environment variables */
        if (envVars != null) {
            if (!envVars.isEmpty()) {
                for (String key : envVars.keySet()) {
                    writerTemp.write(key + "=\'" + envVars.get(key) + "\'");
                    writerTemp.newLine();
                    exportCommand = exportCommand + " " + key;
                }
            }
        }

        writerTemp.write("PATH=$PATH:$PLAYPEN:$STORAGE:.");
        writerTemp.newLine();
        exportCommand = exportCommand + " PATH";
        writerTemp.write(exportCommand);
        writerTemp.newLine();

        if (isScript) {
            writerTemp.write("sh ");
        }

        // put the command in the script redirecting its output to files.
        // Ex. "ls -la > outputfile 2>errorfile"
        writerTemp.write(command);
        writerTemp.newLine();
        writerTemp.flush();
        writerTemp.close();

        return temp;

    } catch (IOException ioe) {
        throw new ExecutorException(ioe);
    }
}

From source file:org.structr.core.graph.SyncCommand.java

private static Object readObject(final DataInputStream inputStream, final byte type) throws IOException {

    switch (type) {

    case 0://from ww  w . ja  va2  s. c om
    case 1:
        return inputStream.readByte();

    case 2:
    case 3:
        return inputStream.readShort();

    case 4:
    case 5:
        return inputStream.readInt();

    case 6:
    case 7:
        return inputStream.readLong();

    case 8:
    case 9:
        return inputStream.readFloat();

    case 10:
    case 11:
        return inputStream.readDouble();

    case 12:
    case 13:
        return inputStream.readChar();

    case 14:
    case 15:
        return new String(deserializeData(inputStream), "UTF-8");

    // this doesn't work with very long strings
    //return inputStream.readUTF();

    case 16:
    case 17:
        return inputStream.readBoolean();
    }

    return null;
}

From source file:tachyon.io.Utils.java

public static String readString(DataInputStream is) throws IOException {
    int len = is.readInt();

    if (len == -1) {
        return null;
    } else if (len == 0) {
        return "";
    }/*from  w ww . j a  va  2  s . c  o  m*/

    char[] chars = new char[len];
    for (int k = 0; k < len; k++) {
        chars[k] = is.readChar();
    }
    return new String(chars);

}