Example usage for java.io DataOutputStream writeLong

List of usage examples for java.io DataOutputStream writeLong

Introduction

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

Prototype

public final void writeLong(long v) throws IOException 

Source Link

Document

Writes a long to the underlying output stream as eight bytes, high byte first.

Usage

From source file:com.yahoo.omid.tso.TSOHandler.java

/**
 * Handle the FullAbortReport message//from www. j  a  v  a  2  s  .  c  om
 */
public void handle(FullAbortRequest msg, ChannelHandlerContext ctx) {
    synchronized (sharedState) {
        DataOutputStream toWAL = sharedState.toWAL;
        try {
            toWAL.writeByte(LoggerProtocol.FULLABORT);
            toWAL.writeLong(msg.startTimestamp);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sharedState.processFullAbort(msg.startTimestamp);
    }
    synchronized (sharedMsgBufLock) {
        queueFullAbort(msg.startTimestamp);
    }
}

From source file:com.codefollower.lealone.omid.tso.TSOHandler.java

private void createAbortedSnapshot() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream toWAL = new DataOutputStream(baos);

    long snapshot = sharedState.hashmap.getAndIncrementAbortedSnapshot();

    try {//from w  w w.  ja v  a2 s  . co m
        toWAL.writeByte(LoggerProtocol.SNAPSHOT);
        toWAL.writeLong(snapshot);
        for (AbortedTransaction aborted : sharedState.hashmap.halfAborted) {
            // ignore aborted transactions from last snapshot
            if (aborted.getSnapshot() < snapshot) {
                toWAL.writeByte(LoggerProtocol.ABORT);
                toWAL.writeLong(aborted.getStartTimestamp());
            }
        }
    } catch (IOException e) {
        // can't happen
        throw new RuntimeException(e);
    }

    sharedState.addRecord(baos.toByteArray(), noCallback, null);
}

From source file:org.gdg.frisbee.android.cache.ModelCache.java

private void writeExpirationToDisk(OutputStream os, DateTime expiresAt) throws IOException {

    DataOutputStream out = new DataOutputStream(os);

    out.writeLong(expiresAt.getMillis());

    out.close();/* w w w .j  a  v  a 2s  .com*/
}

From source file:org.apache.hadoop.hdfs.server.datanode.BlockSender.java

/**
 * Write checksum header to the output stream
 *//*from w w w .j av  a2 s  . co  m*/
private void writeChecksumHeader(DataOutputStream out) throws IOException {
    try {
        checksum.writeHeader(out);
        if (chunkOffsetOK) {
            out.writeLong(offset);
        }
        out.flush();
    } catch (IOException e) { //socket error
        throw ioeToSocketException(e);
    }
}

From source file:com.adito.notification.Notifier.java

void write(Message message) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Writing message " + message.getId() + " '" + message.getSubject() + "' to disk");
    FileOutputStream fout = new FileOutputStream(
            new File(queueDirectory, String.valueOf(message.getId()) + ".msg"));
    try {//from  w w w .j  a  v a 2 s  .  c  om
        DataOutputStream dout = new DataOutputStream(fout);
        dout.writeLong(message.getId());
        dout.writeUTF(message.getSinkName());
        dout.writeBoolean(message.isUrgent());
        dout.writeUTF(message.getSubject());
        for (Iterator i = message.getRecipients().iterator(); i.hasNext();) {
            Recipient r = (Recipient) i.next();
            dout.writeInt(r.getRecipientType());
            dout.writeUTF(r.getRecipientAlias() == null ? "" : r.getRecipientAlias());
            dout.writeUTF(r.getRealmName() == null ? "" : r.getRealmName());
        }
        dout.writeInt(0);
        for (Iterator i = message.getParameterNames(); i.hasNext();) {
            String key = (String) i.next();
            dout.writeInt(1);
            dout.writeUTF(key);
            dout.writeUTF(message.getParameter(key));
        }
        dout.writeInt(0);
        dout.writeUTF(message.getContent());
        dout.writeUTF(message.getLastMessage());
    } finally {
        fout.close();
    }
}

From source file:ch.scythe.hsr.api.TimeTableAPI.java

private void updateCache(String dateString, Date cacheTimestamp, String login, String password)
        throws RequestException, ServerConnectionException, ResponseParseException, AccessDeniedException {

    Log.i(LOGGING_TAG, "Starting to read data from the server.");
    long before = System.currentTimeMillis();

    try {//from www . j  a v a 2 s.  c om

        HttpGet get = createHttpGet(URL + METHOD_GET_TIMETABLE + login, login, password);
        HttpClient httpclient = new DefaultHttpClient();

        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(get);
        InputStream jsonStream = null;
        int httpStatus = httpResponse.getStatusLine().getStatusCode();
        if (httpStatus == HttpStatus.SC_OK) {
            jsonStream = httpResponse.getEntity().getContent();
        } else if (httpStatus == HttpStatus.SC_UNAUTHORIZED) {
            throw new AccessDeniedException();
        } else {
            throw new RequestException("Request not successful. \nHTTP Status: " + httpStatus);
        }

        Log.i(LOGGING_TAG, "Finished reading from server.");

        // convert JSON to Java objects
        JsonTimetableWeek serverData = new GsonParser().parse(jsonStream);
        UiWeek uiWeek = DataAssembler.convert(serverData);

        // open streams to cache the files
        DataOutputStream cacheTimestampOutputStream = new DataOutputStream(
                context.openFileOutput(TIMETABLE_CACHE_TIMESTAMP, Context.MODE_PRIVATE));
        FileOutputStream xmlCacheOutputStream = context.openFileOutput(TIMETABLE_CACHE_SERIALIZED,
                Context.MODE_PRIVATE);

        // write data to streams
        ObjectOutputStream out = new ObjectOutputStream(xmlCacheOutputStream);
        out.writeObject(uiWeek);
        cacheTimestampOutputStream.writeLong(new Date().getTime());

        safeCloseStream(xmlCacheOutputStream);
        safeCloseStream(cacheTimestampOutputStream);

    } catch (UnsupportedEncodingException e) {
        throw new RequestException(e);
    } catch (IllegalStateException e) {
        throw new RequestException(e);
    } catch (IOException e) {
        throw new ServerConnectionException(e);
    }

    Log.i(LOGGING_TAG,
            "Read and parsed data from the server in " + (System.currentTimeMillis() - before) + "ms.");
}

From source file:com.yahoo.omid.tso.TSOHandler.java

public void createAbortedSnapshot() {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream toWAL = new DataOutputStream(baos);

    long snapshot = sharedState.hashmap.getAndIncrementAbortedSnapshot();

    try {//ww  w.ja v a2s.c  om
        toWAL.writeByte(LoggerProtocol.SNAPSHOT);
        toWAL.writeLong(snapshot);
        for (AbortedTransaction aborted : sharedState.hashmap.halfAborted) {
            // ignore aborted transactions from last snapshot
            if (aborted.getSnapshot() < snapshot) {
                toWAL.writeByte(LoggerProtocol.ABORT);
                toWAL.writeLong(aborted.getStartTimestamp());
            }
        }
    } catch (IOException e) {
        // can't happen
        throw new RuntimeException(e);
    }

    sharedState.addRecord(baos.toByteArray(), noCallback, null);
}

From source file:com.hadoopvietnam.cache.memcached.MemcachedCache.java

/**
 * Get the byte[] from a ArrayList&lt;Long&gt;.
 *
 * @param inListOfLongs the list of longs to convert
 * @return the byte[] representation of the ArrayList
 * @throws IOException thrown if any errors encountered
 *///  www  .  j av a2  s  . c o  m
protected byte[] getBytesFromList(final List<Long> inListOfLongs) throws IOException {
    if (inListOfLongs == null) {
        return null;
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(bytes);
    byte[] toReturn = null;
    try {
        for (Long oneLong : inListOfLongs) {
            out.writeLong(oneLong);
            out.flush();
        }

        toReturn = bytes.toByteArray();
    } finally {
        out.close();
    }

    return toReturn;
}

From source file:org.apache.hadoop.hive.accumulo.mr.TestHiveAccumuloTableInputFormat.java

private byte[] parseLongBytes(String s) throws IOException {
    long val = Long.parseLong(s);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
    DataOutputStream out = new DataOutputStream(baos);
    out.writeLong(val);
    out.close();//from  w w  w  .j  a v a  2 s . c om
    return baos.toByteArray();
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * //from  w  w w.ja v  a2 s. c o m
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = file.getFilePointer();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    file.write(headerBytes);

    final CRC32 crc32 = new CRC32();

    // signature header
    file.seek(0);
    file.write(SevenZFile.sevenZSignature);
    // version
    file.write(0);
    file.write(2);

    // start header
    final ByteArrayOutputStream startHeaderBaos = new ByteArrayOutputStream();
    final DataOutputStream startHeaderStream = new DataOutputStream(startHeaderBaos);
    startHeaderStream.writeLong(Long.reverseBytes(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE));
    startHeaderStream.writeLong(Long.reverseBytes(0xffffFFFFL & headerBytes.length));
    crc32.reset();
    crc32.update(headerBytes);
    startHeaderStream.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    startHeaderStream.flush();
    final byte[] startHeaderBytes = startHeaderBaos.toByteArray();
    crc32.reset();
    crc32.update(startHeaderBytes);
    file.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    file.write(startHeaderBytes);
}