Example usage for java.io DataInputStream close

List of usage examples for java.io DataInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

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

private void testInputStream(int inputSize, int bufferSize, int readBufferSize) throws IOException {
    LOG.info("Running test input stream with inputSize: " + inputSize + ", bufferSize: " + bufferSize
            + ", readBufferSize: " + readBufferSize);
    setUp(inputSize);/*  w  w  w. ja  va  2s. co  m*/
    ByteArrayInputStream is = new ByteArrayInputStream(input);
    DataInputStream dis = BufferedByteInputStream.wrapInputStream(is, bufferSize, readBufferSize);

    int totalRead = 0;
    int outputCursor = 0;
    while (totalRead < inputSize) {
        if (rand.nextBoolean()) {
            // read single byte
            output[outputCursor++] = dis.readByte();
            totalRead++;
        } else {
            int count = rand.nextInt(inputSize - totalRead) + 1;
            byte[] bytes = new byte[count];
            int bytesRead = dis.read(bytes, 0, count);
            System.arraycopy(bytes, 0, output, outputCursor, bytesRead);
            outputCursor += bytesRead;
            totalRead += bytesRead;
        }
    }

    assertEquals(inputSize, totalRead);
    assertTrue(Arrays.equals(input, output));

    dis.close();
    dis.close(); // multiple close should work
    dis.close(); // multiple close should work
}

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

@Test(timeout = 100000)
public void testGetMapOutputInfo() throws Exception {
    final ArrayList<Throwable> failures = new ArrayList<Throwable>(1);
    Configuration conf = new Configuration();
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
    conf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "simple");
    UserGroupInformation.setConfiguration(conf);
    File absLogDir = new File("target", TestShuffleHandler.class.getSimpleName() + "LocDir").getAbsoluteFile();
    conf.set(YarnConfiguration.NM_LOCAL_DIRS, absLogDir.getAbsolutePath());
    ApplicationId appId = ApplicationId.newInstance(12345, 1);
    String appAttemptId = "attempt_12345_1_m_1_0";
    String user = "randomUser";
    String userFolder = "randomUserFolder";
    String reducerId = "0";
    List<File> fileMap = new ArrayList<File>();
    createShuffleHandlerFiles(absLogDir, userFolder, appId.toString(), appAttemptId, conf, fileMap);
    ShuffleHandler shuffleHandler = new ShuffleHandler() {
        @Override/*  w  ww.j  a  v a 2  s  .  c om*/
        protected Shuffle getShuffle(Configuration conf) {
            // replace the shuffle handler with one stubbed for testing
            return new Shuffle(conf) {
                @Override
                protected void populateHeaders(List<String> mapIds, String outputBaseStr, String user,
                        int reduce, HttpRequest request, HttpResponse response, boolean keepAliveParam,
                        Map<String, MapOutputInfo> infoMap) throws IOException {
                    // Only set response headers and skip everything else
                    // send some dummy value for content-length
                    super.setResponseHeaders(response, keepAliveParam, 100);
                }

                @Override
                protected void verifyRequest(String appid, ChannelHandlerContext ctx, HttpRequest request,
                        HttpResponse response, URL requestUri) throws IOException {
                    // Do nothing.
                }

                @Override
                protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) {
                    if (failures.size() == 0) {
                        failures.add(new Error(message));
                        ctx.getChannel().close();
                    }
                }

                @Override
                protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user,
                        String mapId, int reduce, MapOutputInfo info) throws IOException {
                    // send a shuffle header
                    ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 5678, 5678, 1);
                    DataOutputBuffer dob = new DataOutputBuffer();
                    header.write(dob);
                    return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                }
            };
        }
    };
    shuffleHandler.init(conf);
    try {
        shuffleHandler.start();
        DataOutputBuffer outputBuffer = new DataOutputBuffer();
        outputBuffer.reset();
        Token<JobTokenIdentifier> jt = new Token<JobTokenIdentifier>("identifier".getBytes(),
                "password".getBytes(), new Text(user), new Text("shuffleService"));
        jt.write(outputBuffer);
        shuffleHandler.initializeApplication(new ApplicationInitializationContext(user, appId,
                ByteBuffer.wrap(outputBuffer.getData(), 0, outputBuffer.getLength()), userFolder));
        URL url = new URL(
                "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY)
                        + "/mapOutput?job=job_12345_0001&reduce=" + reducerId + "&map=attempt_12345_1_m_1_0");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
        conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
        conn.connect();
        try {
            DataInputStream is = new DataInputStream(conn.getInputStream());
            ShuffleHeader header = new ShuffleHeader();
            header.readFields(is);
            is.close();
        } catch (EOFException e) {
            // ignore
        }
        Assert.assertEquals("sendError called due to shuffle error", 0, failures.size());
    } finally {
        shuffleHandler.stop();
        FileUtil.fullyDelete(absLogDir);
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore.java

private void loadRMSequentialNumberState(RMState rmState) throws Exception {
    byte[] seqData = getDataWithRetries(dtSequenceNumberPath, false);
    if (seqData != null) {
        ByteArrayInputStream seqIs = new ByteArrayInputStream(seqData);
        DataInputStream seqIn = new DataInputStream(seqIs);

        try {/* w  w w .j a va 2s . co  m*/
            rmState.rmSecretManagerState.dtSequenceNumber = seqIn.readInt();
        } finally {
            seqIn.close();
        }
    }
}

From source file:org.apache.hadoop.raid.BlockFixer.java

private void computeMetdataAndSendFixedBlock(DatanodeInfo datanode, File localBlockFile, LocatedBlock block,
        long blockSize) throws IOException {

    LOG.info("Computing metdata");
    InputStream blockContents = null;
    DataInputStream blockMetadata = null;
    try {/*w w  w.j a v a  2  s. c  om*/
        blockContents = new FileInputStream(localBlockFile);
        blockMetadata = computeMetadata(getConf(), blockContents);
        blockContents.close();
        // Reopen
        blockContents = new FileInputStream(localBlockFile);
        sendFixedBlock(datanode, blockContents, blockMetadata, block, blockSize);
    } finally {
        if (blockContents != null) {
            blockContents.close();
            blockContents = null;
        }
        if (blockMetadata != null) {
            blockMetadata.close();
            blockMetadata = null;
        }
    }
}

From source file:com.chinamobile.bcbsp.comm.DiskManager.java

/**
 * Load All The Messages In The Specified Bucket(Multiple Files).
 * @param msgMap//w  w  w  .  j a  v a 2 s  .c om
 * @param superStep
 * @param bucket
 * @throws IOException e
 */
public void processMessageLoad(HashMap<String, ArrayList<IMessage>> msgMap, int superStep, int bucket)
        throws IOException {
    String tmpPath = DiskManager.workerDir + "/" + "superstep-" + superStep + "/" + "bucket-" + bucket;
    File bucketFile = new File(tmpPath);
    if (!bucketFile.exists()) {
        LOG.warn("<Bucket> (" + bucket + ") Is Not Exists While Loading From It");
        return;
    }
    FileInputStream fis;
    BufferedInputStream bis;
    DataInputStream dis;
    IMessage m;
    ArrayList<IMessage> mList;
    File[] listFiles = bucketFile.listFiles();
    String[] fileName;
    int count;
    for (int i = 0; i < listFiles.length; i++) {
        fileName = listFiles[i].getName().split("-");
        count = Integer.parseInt(fileName[fileName.length - 1]);
        fis = new FileInputStream(listFiles[i]);
        bis = new BufferedInputStream(fis, MetaDataOfMessage.MESSAGE_IO_BYYES);
        dis = new DataInputStream(bis);
        for (int j = 0; j < count; j++) {
            m = CommunicationFactory.createBspMessage();
            m.readFields(dis);
            // LOG.info("<><><><><>"+m.getDstVertexID()+"<><><><><>"+new
            // String(m.getData()));
            mList = msgMap.get(m.getDstVertexID());
            if (mList == null) {
                mList = new ArrayList<IMessage>();
            }
            mList.add(m);
            msgMap.put(m.getDstVertexID(), mList);
        }
        dis.close();
    }
}

From source file:com.chaosinmotion.securechat.messages.SCMessageQueue.java

/**
 *  Process a data packet from the back end notification service. A data
 *  packet response form the back end has the format:
 *
 *  first byte/*w  w w  .  jav a  2  s.  c o  m*/
 *  0x20        Message
 *  0x21        Token response
 *  0x22        Login failure
 *
 *  Note login success is implicit; if login worked, we start receiving
 *  message notifications, starting with the backlog of stored messages
 *  waiting for us
 */

private void processDataPacket(byte[] data) {
    if (data.length == 0)
        return;

    if (data[0] == 0x20) {
        /*
         *  Process received message.
         */

        ByteArrayInputStream bais = new ByteArrayInputStream(data, 1, data.length - 1);
        DataInputStream dis = new DataInputStream(bais);

        try {
            boolean toflag = dis.readBoolean();
            int messageID = dis.readInt();
            int senderID = dis.readInt();
            String ts = dis.readUTF();
            String senderName = dis.readUTF();
            int messagelen = dis.readInt();
            byte[] message = new byte[messagelen];
            dis.read(message);

            dis.close();

            insertMessage(senderID, senderName, toflag, messageID, DateUtils.parseServerDate(ts), message);

        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (data[0] == 0x21) {
        /*
         * Received token; rest is string
         */

        try {
            String token = new String(data, 1, data.length - 1, "UTF-8");
            loginPhaseTwo(token);
        } catch (UnsupportedEncodingException e) {
            // SHould never happen
        }
    } else if (data[0] == 0x22) {
        /*
         *  Login failure. Close connection and start polling
         */

        closeConnection();
        startPolling("Login failure");
    }
}

From source file:ClipboardExample.java

public Object nativeToJava(TransferData transferData) {
    if (isSupportedType(transferData)) {

        byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null)
            return null;

        MyType[] myData = new MyType[0];
        try {/*ww w.j a  v  a2s.  c  om*/
            ByteArrayInputStream in = new ByteArrayInputStream(buffer);
            DataInputStream readIn = new DataInputStream(in);
            while (readIn.available() > 20) {
                MyType datum = new MyType();
                int size = readIn.readInt();
                byte[] name = new byte[size];
                readIn.read(name);
                datum.firstName = new String(name);
                size = readIn.readInt();
                name = new byte[size];
                readIn.read(name);
                datum.lastName = new String(name);
                MyType[] newMyData = new MyType[myData.length + 1];
                System.arraycopy(myData, 0, newMyData, 0, myData.length);
                newMyData[myData.length] = datum;
                myData = newMyData;
            }
            readIn.close();
        } catch (IOException ex) {
            return null;
        }
        return myData;
    }

    return null;
}

From source file:org.apache.hadoop.fs.JHLogAnalyzer.java

/**
 * Result is combined from all reduce output files and is written to
 * RESULT_FILE in the format//  w w  w . j  a v  a 2  s  .  com
 * column 1: 
 */
private static void analyzeResult(FileSystem fs, int testType, long execTime, Path resFileName)
        throws IOException {
    LOG.info("Analyzing results ...");
    DataOutputStream out = null;
    BufferedWriter writer = null;
    try {
        out = new DataOutputStream(fs.create(resFileName));
        writer = new BufferedWriter(new OutputStreamWriter(out));
        writer.write("SERIES\tPERIOD\tTYPE\tSLOT_HOUR\n");
        FileStatus[] reduceFiles = fs.listStatus(OUTPUT_DIR);
        assert reduceFiles.length == JHLAPartitioner.NUM_REDUCERS;
        for (int i = 0; i < JHLAPartitioner.NUM_REDUCERS; i++) {
            DataInputStream in = null;
            BufferedReader lines = null;
            try {
                in = fs.open(reduceFiles[i].getPath());
                lines = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = lines.readLine()) != null) {
                    StringTokenizer tokens = new StringTokenizer(line, "\t*");
                    String attr = tokens.nextToken();
                    String dateTime = tokens.nextToken();
                    String taskType = tokens.nextToken();
                    double val = Long.parseLong(tokens.nextToken()) / (double) DEFAULT_TIME_INTERVAL_MSEC;
                    writer.write(attr.substring(2)); // skip the stat type "l:"
                    writer.write("\t");
                    writer.write(dateTime);
                    writer.write("\t");
                    writer.write(taskType);
                    writer.write("\t");
                    writer.write(String.valueOf((float) val));
                    writer.newLine();
                }
            } finally {
                if (lines != null)
                    lines.close();
                if (in != null)
                    in.close();
            }
        }
    } finally {
        if (writer != null)
            writer.close();
        if (out != null)
            out.close();
    }
    LOG.info("Analyzing results ... done.");
}

From source file:it.unimi.dsi.sux4j.io.ChunkedHashStore.java

/** Returns the size of this store. Note that if you set up 
 * a {@linkplain #filter(Predicate) filter}, the first call to
 * this method will require a scan to the whole store. 
 * /* ww w .j a va2s.c o  m*/
 * @return the number of (possibly filtered) triples of this store.
 */

public long size() throws IOException {
    if (filter == null)
        return size;
    if (filteredSize == -1) {
        long c = 0;
        final long[] triple = new long[3];
        for (int i = 0; i < DISK_CHUNKS; i++) {
            if (filter == null)
                c += count[i];
            else {
                for (DataOutputStream d : dos)
                    d.flush();
                final DataInputStream dis = new DataInputStream(
                        new FastBufferedInputStream(new FileInputStream(file[i])));
                for (int j = 0; j < count[i]; j++) {
                    triple[0] = dis.readLong();
                    triple[1] = dis.readLong();
                    triple[2] = dis.readLong();
                    if (hashMask == 0)
                        dis.readLong();
                    if (filter.evaluate(triple))
                        c++;
                }
                dis.close();
            }
        }

        filteredSize = c;
    }
    return filteredSize;
}

From source file:bkampfbot.Instance.java

/**
 * Parst die Parameter und liest die Konfigurationsdatei aus.
 * //  w ww  .  ja  v  a  2  s  .c  o m
 * @param args
 * @throws FatalError
 * @throws IOException
 * @throws RestartLater
 */
public Instance(String[] args) throws FatalError, IOException {

    // configure plans
    PlanBoeseBeute.initiate();
    PlanBoeseKrieg.initiate();
    PlanBoeseRespekt.initiate();
    PlanAngriff.initiate();

    // reset config
    new Config();

    // reset user
    new User();

    this.parseArguments(args);

    if (this.modus.equals(Modus.help)) {
        Output.help();
    }

    // read config file
    try {
        File f = new File(Config.getConfigFile());
        FileInputStream fstream = new FileInputStream(f.getAbsoluteFile());
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine, configString = "";

        // Read File Line By Line
        while ((strLine = br.readLine()) != null) {

            // Kommentare werden ausgelassen
            if (strLine.length() > 0 && strLine.substring(0, 1).equals("#")) {
                configString += "\n";
                continue;
            }
            configString += strLine + "\n";
        }

        // Close the input stream
        in.close();

        try {
            JSONObject config = new JSONObject(new JSONTokener(configString));
            Config.parseJsonConfig(config);
        } catch (JSONException e) {
            int lines = configString.substring(0, e.getIndex()).replaceAll("[^\n]", "").length();
            throw new FatalError(
                    "Die Struktur der Konfigurationsdatei " + "stimmt nicht. Versuche den Inhalt der "
                            + "Datei mit einem externen Werkzeug zu " + "reparieren. Dafr gibt es Webseiten, "
                            + "die JSON-Objekte validieren knnen. Vermutlich in der Zeile " + lines + "."
                            + "\n\nAls Hinweis hier noch die Fehlerausgabe:\n" + e.getMessage()
                            + "\n\nEingabe war:\n" + e.getInputString());
        }
    } catch (FileNotFoundException e) {
        throw new FatalError("Die Konfigurationsdatei konnte nicht gefunden/geffnet werden.\n Datei: "
                + Config.getConfigFile() + "\n Fehler: " + e.getMessage());
    }

    if (Config.getUserName() == null || Config.getUserPassword() == null || Config.getHost() == null) {
        throw new FatalError("Die Konfigurationsdatei ist nicht vollstndig. "
                + "Es wird mindestens der Benutzername, das " + "Passwort und der Hostname bentigt.");
    }

}