Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

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

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

/**
 * Returns snapshot at index modulo the length of the list, if index is < 0, it returns a random snapshot
 */// w w  w .  ja  v a 2 s . c o  m
private static List<WifiReading> getSnapshot(int index, Context c) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can at least read the external storage
        File root = c.getExternalCacheDir();
        File[] snapshotFiles = root.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                if (filename.endsWith(FILE_EXTENSION)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        if (snapshotFiles.length > 0) {
            if (index < 0) {
                Random rand = new Random();
                index = rand.nextInt(snapshotFiles.length);
            } else {
                index = index % snapshotFiles.length;
            }
            try {
                // read file into a string
                FileInputStream fstream = new FileInputStream(snapshotFiles[index]);
                Log.d(TAG, snapshotFiles[index].getName());
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String fileContents = "";
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    fileContents += strLine;
                }

                // make a json array out of the string
                JSONArray json = new JSONArray(fileContents);

                // parse the json array
                return jsonToReadings(json);
            } catch (Exception e) {
                Log.e(TAG, "Could not read file.");
                return null;
            }
        } else {
            // there are no cached snapshots
            return null;
        }
    } else {
        // we cannot read the external storage
        return null;
    }
}

From source file:edu.indiana.d2i.htrc.io.SequentialDataCopyJob.java

@Override
public int run(String[] args) throws Exception {
    if (args.length != 5) {
        printUsage();/*from w w w .j  av  a  2 s .c om*/
    }

    String inputPath = args[0];
    String outputPath = args[1];
    int chunkSizeInMB = Integer.valueOf(args[2]);
    String dataAPIConfClassName = args[3];
    int maxIdsPerReq = Integer.valueOf(args[4]);

    logger.info("SequentialDataCopyJob ");
    logger.info(" - input: " + inputPath);
    logger.info(" - output: " + outputPath);
    logger.info(" - chunkSizeInMB: " + chunkSizeInMB);
    logger.info(" - dataAPIConfClassName: " + dataAPIConfClassName);
    logger.info(" - maxIdsPerReq: " + maxIdsPerReq);

    Configuration conf = getConf();
    Utilities.setDataAPIConf(conf, dataAPIConfClassName, maxIdsPerReq);

    HTRCDataAPIClient client = Utilities.creatDataAPIClient(conf);

    ChunkedWriter chunkWriter = new ChunkedWriter(getConf(), chunkSizeInMB, new Path(outputPath));

    Path input = new Path(inputPath);
    FileSystem fs = input.getFileSystem(conf);
    DataInputStream fsinput = new DataInputStream(fs.open(input));
    BufferedReader reader = new BufferedReader(new InputStreamReader(fsinput));
    String line = null;
    int idNumThreshold = 100;
    int idNum = 0;
    StringBuilder idList = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        idList.append(line + "|");
        if ((++idNum) >= idNumThreshold) {
            text2Seq(client.getID2Content(idList.toString()), chunkWriter);
            idList = new StringBuilder();
            idNum = 0;
        }
    }
    if (idList.length() > 0)
        text2Seq(client.getID2Content(idList.toString()), chunkWriter);

    chunkWriter.close();
    reader.close();

    return 0;
}

From source file:io.undertow.servlet.test.proprietry.TransferTestCase.java

@Test
public void testServletRequest() throws Exception {
    TestListener.init(2);/*w ww .  j  ava2 s.c om*/
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final byte[] response = HttpClientUtils.readRawResponse(result);
        Path file = Paths.get(TXServlet.class.getResource(TXServlet.class.getSimpleName() + ".class").toURI());
        byte[] expected = new byte[(int) Files.size(file)];
        DataInputStream dataInputStream = new DataInputStream(Files.newInputStream(file));
        dataInputStream.readFully(expected);
        dataInputStream.close();
        Assert.assertArrayEquals(expected, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:com.eufar.asmm.server.DownloadFunction.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("DownloadFunction - the function started");
    ServletContext context = getServletConfig().getServletContext();
    request.setCharacterEncoding("UTF-8");
    String dir = context.getRealPath("/tmp");
    ;//  w  w w . j  a v  a 2s .com
    String filename = "";
    File fileDir = new File(dir);
    try {
        System.out.println("DownloadFunction - create the file on server");
        filename = request.getParameterValues("filename")[0];
        String xmltree = request.getParameterValues("xmltree")[0];

        // format xml code to pretty xml code
        Document doc = DocumentHelper.parseText(xmltree);
        StringWriter sw = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndent(true);
        format.setIndentSize(4);
        XMLWriter xw = new XMLWriter(sw, format);
        xw.write(doc);

        Writer out = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(dir + "/" + filename), "UTF-8"));
        out.append(sw.toString());
        out.flush();
        out.close();
    } catch (Exception ex) {
        System.out.println("ERROR during rendering: " + ex);
    }
    try {
        System.out.println("DownloadFunction - send file to user");
        ServletOutputStream out = response.getOutputStream();
        File file = new File(dir + "/" + filename);
        String mimetype = context.getMimeType(filename);
        response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        response.setHeader("Pragma", "private");
        response.setHeader("Cache-Control", "private, must-revalidate");
        DataInputStream in = new DataInputStream(new FileInputStream(file));
        int length;
        while ((in != null) && ((length = in.read(bbuf)) != -1)) {
            out.write(bbuf, 0, length);
        }
        in.close();
        out.flush();
        out.close();
        FileUtils.cleanDirectory(fileDir);
    } catch (Exception ex) {
        System.out.println("ERROR during downloading: " + ex);
    }
    System.out.println("DownloadFunction - file ready to be donwloaded");
}

From source file:gridool.util.xfer.TransferUtils.java

public static void sendfile(@Nonnull final File file, final long fromPos, final long count,
        @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort,
        final boolean append, final boolean sync, @Nonnull final TransferClientHandler handler)
        throws IOException {
    if (!file.exists()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " does not exist");
    }/*  www.j  av a2  s .c  om*/
    if (!file.isFile()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " is not file");
    }
    if (!file.canRead()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " cannot read");
    }

    final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
    SocketChannel channel = null;
    Socket socket = null;
    final OutputStream out;
    try {
        channel = SocketChannel.open();
        socket = channel.socket();
        socket.connect(dstSockAddr);
        out = socket.getOutputStream();
    } catch (IOException e) {
        LOG.error("failed to connect: " + dstSockAddr, e);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
        throw e;
    }

    DataInputStream din = null;
    if (sync) {
        InputStream in = socket.getInputStream();
        din = new DataInputStream(in);
    }
    final DataOutputStream dos = new DataOutputStream(out);
    final StopWatch sw = new StopWatch();
    FileInputStream src = null;
    final long nbytes;
    try {
        src = new FileInputStream(file);
        FileChannel fc = src.getChannel();

        String fileName = file.getName();
        IOUtils.writeString(fileName, dos);
        IOUtils.writeString(writeDirPath, dos);
        long xferBytes = (count == -1L) ? fc.size() : count;
        dos.writeLong(xferBytes);
        dos.writeBoolean(append); // append=false
        dos.writeBoolean(sync);
        if (handler == null) {
            dos.writeBoolean(false);
        } else {
            dos.writeBoolean(true);
            handler.writeAdditionalHeader(dos);
        }

        // send file using zero-copy send
        nbytes = fc.transferTo(fromPos, xferBytes, channel);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent a file '" + file.getAbsolutePath() + "' of " + nbytes + " bytes to "
                    + dstSockAddr.toString() + " in " + sw.toString());
        }

        if (sync) {// receive ack in sync mode
            long remoteRecieved = din.readLong();
            if (remoteRecieved != xferBytes) {
                throw new IllegalStateException(
                        "Sent " + xferBytes + " bytes, but remote node received " + remoteRecieved + " bytes");
            }
        }

    } catch (FileNotFoundException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } catch (IOException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } finally {
        IOUtils.closeQuietly(src);
        IOUtils.closeQuietly(din, dos);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
    }
}

From source file:fileInfo.java

/**
 * Normal constructor with one filename; file is opened and saved.
 *//* www .  j a  v  a  2s  .  c  o  m*/
fileInfo(String filename) {
    symbol = new node[MAXLINECOUNT + 2];
    other = null; // allocated later!
    try {
        file = new DataInputStream(new FileInputStream(filename));
    } catch (IOException e) {
        System.err.println("Diff can't read file " + filename);
        System.err.println("Error Exception was:" + e);
        System.exit(1);
    }
}

From source file:com.cloudmaster.cmp.util.AlarmSystem.transfer.HttpSender.java

public ResponseObject send(TransportObject object) throws Exception {
    ResponseObject rs = new ResponseObject();
    ByteArrayOutputStream bOs = null;
    DataOutputStream dOs = null;/*from   w  ww  .j a  v a2  s .c om*/
    DataInputStream dIs = null;
    HttpClient client;
    PostMethod meth = null;
    byte[] rawData;
    try {
        bOs = new ByteArrayOutputStream();
        dOs = new DataOutputStream(bOs);
        object.toStream(dOs);
        bOs.flush();
        rawData = bOs.toByteArray();

        client = new HttpClient();
        client.setConnectionTimeout(this.timeout);
        client.setTimeout(this.datatimeout);
        client.setHttpConnectionFactoryTimeout(this.timeout);

        meth = new PostMethod(object.getValue(SERVER_URL));
        // meth = new UTF8PostMethod(url);
        meth.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODING);
        // meth.addParameter(SERVER_ARGS, new String(rawData,"UTF-8"));

        // meth.setRequestBody(new String(rawData));
        // meth.setRequestBody(new String(rawData,"UTF-8"));

        byte[] base64Array = Base64.encode(rawData).getBytes();
        meth.setRequestBody(new String(base64Array));

        // System.out.println(new String(rawData));

        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        client.executeMethod(meth);

        dIs = new DataInputStream(meth.getResponseBodyAsStream());

        if (meth.getStatusCode() == HttpStatus.SC_OK) {

            Header errHeader = meth.getResponseHeader(HDR_ERROR);

            if (errHeader != null) {
                rs.setError(meth.getResponseBodyAsString());
                return rs;
            }

            rs = ResponseObject.fromStream(dIs);

            return rs;
        } else {
            meth.releaseConnection();
            throw new IOException("Connection failure: " + meth.getStatusLine().toString());
        }
    } finally {
        if (meth != null) {
            meth.releaseConnection();
        }
        if (bOs != null) {
            bOs.close();
        }
        if (dOs != null) {
            dOs.close();
        }
        if (dIs != null) {
            dIs.close();
        }
    }
}

From source file:jetbrains.exodus.entitystore.FileSystemBlobVaultOld.java

protected FileSystemBlobVaultOld(@NotNull final String parentDirectory, @NotNull final String blobsDirectory,
        @NotNull final String blobExtension, @NotNull final BlobHandleGenerator blobHandleGenerator,
        final int expectedVersion) throws IOException {
    this.blobsDirectory = blobsDirectory;
    this.blobExtension = blobExtension;
    location = new File(parentDirectory, blobsDirectory);
    this.blobHandleGenerator = blobHandleGenerator;
    size = new AtomicLong(UNKNOWN_SIZE);
    //noinspection ResultOfMethodCallIgnored
    location.mkdirs();/*from  w  ww .ja v  a 2 s. c o  m*/
    // load version
    final File versionFile = new File(location, VERSION_FILE);
    if (versionFile.exists()) {
        try (DataInputStream input = new DataInputStream(new FileInputStream(versionFile))) {
            version = input.readInt();
        }
        if (expectedVersion != version) {
            throw new UnexpectedBlobVaultVersionException("Unexpected FileSystemBlobVault version: " + version);
        }
    } else {
        final File[] files = location.listFiles();
        final boolean hasFiles = files != null && files.length > 0;
        if (!hasFiles) {
            version = expectedVersion;
        } else {
            version = EXPECTED_VERSION;
            if (expectedVersion != version) {
                throw new UnexpectedBlobVaultVersionException(
                        "Unexpected FileSystemBlobVault version: " + version);
            }
        }
        try (DataOutputStream output = new DataOutputStream(new FileOutputStream(versionFile))) {
            output.writeInt(expectedVersion);
        }
    }
}

From source file:com.aliyun.odps.ogg.handler.datahub.DatahubHandler.java

public static int getSkipSendTimes(String fileName, OggAlarm oggAlarm) {
    File handlerInfoFile = new File(fileName);
    if (handlerInfoFile.exists() && !handlerInfoFile.isDirectory()) {
        DataInputStream in = null;
        try {/*w  ww  .ja v  a  2  s  .  com*/
            in = new DataInputStream(new FileInputStream(handlerInfoFile));
            return in.readInt();
        } catch (IOException e) {
            logger.warn("Error reading handler info file, may cause duplication ", e);
            oggAlarm.warn("Error reading handler info file, may cause duplication ", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.warn("Close handler info file failed. ", e);
                    oggAlarm.warn("Close handler info file failed. ", e);
                }
            }
        }
    }
    return 0;
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

/**
 * Load Stored data/*w w  w. jav a  2  s  .  c om*/
 *
 * @param input  input stream to read the double array trie from
 * @return double array trie, not null
 * @throws IOException if an IO error occured during reading the double array trie
 */
public static DoubleArrayTrie read(InputStream input) throws IOException {
    DoubleArrayTrie trie = new DoubleArrayTrie();
    DataInputStream dis = new DataInputStream(new BufferedInputStream(input));

    trie.compact = dis.readBoolean();
    int baseCheckSize = dis.readInt(); // Read size of baseArr and checkArr
    int tailSize = dis.readInt(); // Read size of tailArr
    ReadableByteChannel channel = Channels.newChannel(dis);

    ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpBaseBuffer);
    tmpBaseBuffer.rewind();
    trie.baseBuffer = tmpBaseBuffer.asIntBuffer();

    ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpCheckBuffer);
    tmpCheckBuffer.rewind();
    trie.checkBuffer = tmpCheckBuffer.asIntBuffer();

    ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2);
    channel.read(tmpTailBuffer);
    tmpTailBuffer.rewind();
    trie.tailBuffer = tmpTailBuffer.asCharBuffer();

    input.close();
    return trie;
}