Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC ( Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package com.ursaj.hfs.gc;
 import java.util.UUID;
 import static com.ursaj.hfs.util.HfsUtils.UUID_COMPARATOR;

Garbage collector response writer in format compatible with default GC reader.

Note! This class is NOT thread-safe and should not be used in multi-threaded environment.

 public class HfsGcResponseWriter implements Consumer<UUIDIOException>, Closeable {
Stream header.
     static final byte[] HEADER = new byte[] { 'G''C''R''\u0000' /* version */ };

Current writing thread.
     private final AtomicReference<Threadthread = new AtomicReference<Thread>();

Write buffer.
     private final ByteBuffer buffer = ByteBuffer.allocate(18 /* per record */);

Output stream to write binary data to.
     private final OutputStream out;

Previous written file ID.
     private volatile UUID last;

Creates an output stream filter built on top of the specified underlying output stream.

out The underlying output stream to be filtered.
Throws: If failed to write stream header into the output.
     public HfsGcResponseWriter(OutputStream outthrows IOException {
         if (out == null)
             throw new IllegalArgumentException("Underlying output stream cannot be null.");
         this. = out;
         // Write marker header.

Write file ID into the output stream.

obj File ID.
Always true.
Throws: In case of any IO exception.
     public boolean offer(UUID objthrows IOException {
         // Cheap protection against multi-threaded usage.
         if (!.compareAndSet(null, Thread.currentThread()))
             throw new IllegalStateException("Failed to write IDs concurrently.");
         try {
             // Protect against invalid IDs ordering.
             if ( != null && .compare(obj) >= 0)
                 throw new IllegalArgumentException("File IDs should follow in natural (unsigned) ordering"
                     + " [prev=" +  + ", id=" + obj + ']');
              = obj;
             // Clear output buffer.
             // Writes file ID into buffer with format acceptable by message marshaller.
             int hashCode = obj.hashCode();
             // Integrity verification bytes.
             .putShort((short) ((hashCode >> 16) ^ hashCode));
            // Expects buffer is fully filled.
            assert .remaining() == 0 : "Unexpected buffer state: " + ;
        finally {
        return true;
    public void close() throws IOException {

Write UUID into output.

out Object output to write UUID to.
uuid UUID to write into output.
    static void writeUUID(ByteBuffer outUUID uuid) {

Read UUID from the input.

in Object input to read UUID from.
UUID read from the input.
    static UUID readUUID(ByteBuffer in) {
        long mostSigBits = in.getLong();
        long leastSigBits = in.getLong();
        return new UUID(mostSigBitsleastSigBits);
New to GrepCode? Check out our FAQ X