Example usage for java.io DataInputStream readLong

List of usage examples for java.io DataInputStream readLong

Introduction

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

Prototype

public final long readLong() throws IOException 

Source Link

Document

See the general contract of the readLong method of DataInput.

Usage

From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java

private long readMetaFile(@NonNull File metaFile, @NonNull Map<String, List<String>> headers)
        throws IOException {
    final AtomicFile af = new AtomicFile(metaFile);
    final FileInputStream fis = af.openRead();
    final DataInputStream dat = new DataInputStream(new BufferPoolInputStream(fis));
    try {//from  w w w .java 2 s . c om
        final long expireTime = dat.readLong();
        int headersCount = dat.readInt();
        while (headersCount-- > 0) {
            final String name = dat.readUTF();
            int valuesCount = dat.readInt();
            final List<String> values = new ArrayList<>(valuesCount);
            while (valuesCount-- > 0) {
                values.add(dat.readUTF());
            }
            headers.put(name, values);
        }
        return expireTime;
    } finally {
        IOUtils.closeQuietly(dat);
    }
}

From source file:com.mtgi.analytics.test.AbstractPerformanceTestCase.java

private void runTest(ServerSocket listener, ProcessBuilder launcher, StatisticsMBean stats, byte[] jobData)
        throws IOException, InterruptedException {
    Process proc = launcher.start();

    //connect stdout and stderr to parent process streams
    if (log.isDebugEnabled()) {
        new PipeThread(proc.getInputStream(), System.out).start();
        new PipeThread(proc.getErrorStream(), System.err).start();
    } else {// ww w . java2  s .c  om
        NullOutput sink = new NullOutput();
        new PipeThread(proc.getInputStream(), sink).start();
        new PipeThread(proc.getErrorStream(), sink).start();
    }

    //wait for the incoming connection from the child process.
    Socket sock = listener.accept();
    try {
        //connection established, send the job.
        OutputStream os = sock.getOutputStream();
        os.write(jobData);
        os.flush();

        //read measurements back.
        DataInputStream dis = new DataInputStream(sock.getInputStream());
        for (long measure = dis.readLong(); measure >= 0; measure = dis.readLong())
            stats.add(measure);

        //send ack byte to tell the child proc its ok to hang up.
        os.write(1);
        os.flush();

    } finally {
        sock.close();
    }

    assertEquals("Child process ended successfully", 0, proc.waitFor());
}

From source file:be.fedict.eid.idp.protocol.openid.StatelessServerAssociationStore.java

private Association loadFromHandle(String handle)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, IOException, InvalidAlgorithmParameterException {
    byte[] encodedHandle = Base64.decodeBase64(handle);
    if (null != this.macSecretKeySpec) {
        byte[] signature = new byte[32];
        System.arraycopy(encodedHandle, 0, signature, 0, 32);
        byte[] toBeSigned = new byte[encodedHandle.length - 32];
        System.arraycopy(encodedHandle, 32, toBeSigned, 0, encodedHandle.length - 32);
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(this.macSecretKeySpec);
        byte[] actualSignature = mac.doFinal(toBeSigned);
        if (false == Arrays.equals(actualSignature, signature)) {
            return null;
        }//from ww w.j  a  v a2s .  c  o  m
        encodedHandle = toBeSigned;
    }
    byte[] iv = new byte[16];
    System.arraycopy(encodedHandle, 0, iv, 0, iv.length);
    byte[] encodedData = Arrays.copyOfRange(encodedHandle, 16, encodedHandle.length);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGO);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, this.secretKeySpec, ivParameterSpec);
    byte[] associationBytes = cipher.doFinal(encodedData);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(associationBytes);
    int typeByte = byteArrayInputStream.read();
    if (typeByte == 1) {
        byte[] macKeyBytes = new byte[160 / 8];
        byteArrayInputStream.read(macKeyBytes);
        DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
        long exp = dataInputStream.readLong();
        Date expDate = new Date(exp);
        return Association.createHmacSha1(handle, macKeyBytes, expDate);
    } else if (typeByte == 2) {
        byte[] macKeyBytes = new byte[256 / 8];
        byteArrayInputStream.read(macKeyBytes);
        DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
        long exp = dataInputStream.readLong();
        Date expDate = new Date(exp);
        return Association.createHmacSha256(handle, macKeyBytes, expDate);
    } else {
        return null;
    }
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

private void verify(EventQueueBackingStore backingStore, long expectedVersion, List<Long> expectedPointers)
        throws Exception {
    FlumeEventQueue queue = new FlumeEventQueue(backingStore, inflightTakes, inflightPuts);
    List<Long> actualPointers = Lists.newArrayList();
    FlumeEventPointer ptr;//from  w  w w. j  ava2  s. c  o m
    while ((ptr = queue.removeHead(0L)) != null) {
        actualPointers.add(ptr.toLong());
    }
    Assert.assertEquals(expectedPointers, actualPointers);
    Assert.assertEquals(10, backingStore.getCapacity());
    DataInputStream in = new DataInputStream(new FileInputStream(checkpoint));
    long actualVersion = in.readLong();
    Assert.assertEquals(expectedVersion, actualVersion);
    in.close();
}

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

public final void handleRequest(@Nonnull final SocketChannel inChannel, @Nonnull final Socket socket)
        throws IOException {
    final StopWatch sw = new StopWatch();
    if (!inChannel.isBlocking()) {
        inChannel.configureBlocking(true);
    }//w ww.j a va 2s. co  m

    InputStream in = socket.getInputStream();
    DataInputStream dis = new DataInputStream(in);

    String fname = IOUtils.readString(dis);
    String dirPath = IOUtils.readString(dis);
    long len = dis.readLong();
    boolean append = dis.readBoolean();
    boolean ackRequired = dis.readBoolean();
    boolean hasAdditionalHeader = dis.readBoolean();
    if (hasAdditionalHeader) {
        readAdditionalHeader(dis, fname, dirPath, len, append, ackRequired);
    }

    final File file;
    if (dirPath == null) {
        file = new File(baseDir, fname);
    } else {
        File dir = FileUtils.resolvePath(baseDir, dirPath);
        file = new File(dir, fname);
    }

    preFileAppend(file, append);
    final FileOutputStream dst = new FileOutputStream(file, append);
    final String fp = file.getAbsolutePath();
    final ReadWriteLock filelock = accquireLock(fp, locks);
    final FileChannel fileCh = dst.getChannel();
    final long startPos = file.length();
    try {
        NIOUtils.transferFully(inChannel, 0, len, fileCh); // REVIEWME really an atomic operation?
    } finally {
        IOUtils.closeQuietly(fileCh, dst);
        releaseLock(fp, filelock, locks);
        postFileAppend(file, startPos, len);
    }
    if (ackRequired) {
        OutputStream out = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        dos.writeLong(len);
        postAck(file, startPos, len);
    }

    if (LOG.isDebugEnabled()) {
        SocketAddress remoteAddr = socket.getRemoteSocketAddress();
        LOG.debug("Received a " + (append ? "part of file '" : "file '") + file.getAbsolutePath() + "' of "
                + len + " bytes from " + remoteAddr + " in " + sw.toString());
    }
}

From source file:xbird.util.xfer.RecievedFileWriter.java

public final void handleRequest(@Nonnull final SocketChannel inChannel, @Nonnull final Socket socket)
        throws IOException {
    final StopWatch sw = new StopWatch();
    if (!inChannel.isBlocking()) {
        inChannel.configureBlocking(true);
    }// w  w w  .jav  a  2 s . co m

    InputStream in = socket.getInputStream();
    DataInputStream dis = new DataInputStream(in);

    String fname = IOUtils.readString(dis);
    String dirPath = IOUtils.readString(dis);
    long len = dis.readLong();
    boolean append = dis.readBoolean();
    boolean ackRequired = dis.readBoolean();
    boolean hasAdditionalHeader = dis.readBoolean();
    if (hasAdditionalHeader) {
        readAdditionalHeader(dis, fname, dirPath, len, append, ackRequired);
    }

    final File file;
    if (dirPath == null) {
        file = new File(baseDir, fname);
    } else {
        File dir = FileUtils.resolvePath(baseDir, dirPath);
        file = new File(dir, fname);
    }

    preFileAppend(file, append);
    final FileOutputStream dst = new FileOutputStream(file, append);
    final String fp = file.getAbsolutePath();
    final ReadWriteLock filelock = accquireLock(fp, locks);
    final FileChannel fileCh = dst.getChannel();
    final long startPos = file.length();
    try {
        NIOUtils.transferFullyFrom(inChannel, 0, len, fileCh); // REVIEWME really an atomic operation?
    } finally {
        IOUtils.closeQuietly(fileCh, dst);
        releaseLock(fp, filelock, locks);
        postFileAppend(file, startPos, len);
    }
    if (ackRequired) {
        OutputStream out = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        dos.writeLong(len);
        postAck(file, startPos, len);
    }

    if (LOG.isDebugEnabled()) {
        SocketAddress remoteAddr = socket.getRemoteSocketAddress();
        LOG.debug("Received a " + (append ? "part of file '" : "file '") + file.getAbsolutePath() + "' of "
                + len + " bytes from " + remoteAddr + " in " + sw.toString());
    }
}

From source file:com.facebook.infrastructure.db.Column.java

private IColumn defreeze(DataInputStream dis, String name) throws IOException {
    IColumn column = null;//from   ww  w.  j  a  v  a  2s .  c o  m
    boolean delete = dis.readBoolean();
    long ts = dis.readLong();
    int size = dis.readInt();
    byte[] value = new byte[size];
    dis.readFully(value);
    column = new Column(name, value, ts, delete);
    return column;
}

From source file:org.veronicadb.core.memorygraph.storage.SimpleLocalFileStorageSink.java

protected long readElementId(DataInputStream stream) throws IOException {
    return stream.readLong();
}

From source file:org.veronicadb.core.memorygraph.storage.SimpleLocalFileStorageSink.java

protected long readGraphId(DataInputStream stream) throws IOException {
    return stream.readLong();
}

From source file:org.apache.xmlgraphics.image.loader.impl.PNGFile.java

public PNGFile(InputStream stream) throws IOException, ImageException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }//w  ww . jav a 2  s. c  o m
    DataInputStream distream = null;
    try {
        distream = new DataInputStream(stream);
        final long magic = distream.readLong();
        if (magic != PNG_SIGNATURE) {
            final String msg = PropertyUtil.getString("PNGImageDecoder0");
            throw new ImageException(msg);
        }
        // only some chunks are worth parsing in the current implementation
        do {
            try {
                PNGChunk chunk;
                final String chunkType = PNGChunk.getChunkType(distream);
                if (chunkType.equals(PNGChunk.ChunkType.IHDR.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_IHDR_chunk(chunk);
                } else if (chunkType.equals(PNGChunk.ChunkType.PLTE.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_PLTE_chunk(chunk);
                } else if (chunkType.equals(PNGChunk.ChunkType.IDAT.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    this.streamVec.add(new ByteArrayInputStream(chunk.getData()));
                } else if (chunkType.equals(PNGChunk.ChunkType.IEND.name())) {
                    // chunk = PNGChunk.readChunk(distream);
                    PNGChunk.skipChunk(distream);
                    break; // fall through to the bottom
                } else if (chunkType.equals(PNGChunk.ChunkType.tRNS.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_tRNS_chunk(chunk);
                } else {
                    // chunk = PNGChunk.readChunk(distream);
                    PNGChunk.skipChunk(distream);
                }
            } catch (final Exception e) {
                log.error("Exception", e);
                final String msg = PropertyUtil.getString("PNGImageDecoder2");
                throw new RuntimeException(msg);
            }
        } while (true);
    } finally {
        IOUtils.closeQuietly(distream);
    }
}