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;

The file IDs reader of garbage collector response.

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

 public class HfsGcResponseReader implements Producer<UUIDIOException>, Closeable {
Underlying input stream to read response from.
     private final InputStream in;

Last read file ID.
     private UUID last;

Constructs GC response input stream.

in The underlying input stream to be filtered.
Throws: In case of any IO exception.
     public HfsGcResponseReader(InputStream inthrows IOException {
         if (in == null)
             throw new IllegalArgumentException("Underlying input stream cannot be null.");
         this. = in;
         byte[] expected = .;
         byte[] actual = read(expected.lengthfalse);
         if (!Arrays.equals(actualexpected))
             throw new IOException("Underlying input stream is not a GC response stream.");

Retrieve the next UUID from GC response stream.

Next alive file ID. If no item is available at the moment - current thread will be blocked. If no more file IDs might be produced - null-value will be returned.
Throws: In case of any IO exception.
     public UUID retrieve() throws IOException {
         byte[] buffer = read(18, true);
         if (buffer == null)
             return null;
         ByteBuffer wrap = ByteBuffer.wrap(buffer);
         // Read file ID from the buffer with format acceptable by message marshaller.
         UUID fileId = HfsGcResponseWriter.readUUID(wrap);
         int hashCode = fileId.hashCode();
         // Read and validate integrity verification code.
         if (wrap.getShort() != (short) ((hashCode >> 16) ^ hashCode))
             throw new IOException("Failed to validate stream integrity (stop reading).");
         if ( != null && .compare(fileId) >= 0)
             throw new IOException("Failed to validate streamed data order (stop reading).");
          = fileId;
         return fileId;
     public void close() throws IOException {

Reads the next data block from the stream or throws if end of the input has been reached.

length Length of data block to read.
throwCorrupted Throw EOF exception if no enough data to build data block.
The next read data block or null if no requested data found.
Throws: In case of any IO exception.
    private byte[] read(int lengthboolean throwCorruptedthrows IOException {
        byte[] buffer = new byte[length];
        int read;
        for (int offset = 0; offset < lengthoffset += read) {
            read = .read(bufferoffsetlength - offset);
            if (read == -1) {
                if (offset > 0 && throwCorrupted)
                    throw new EOFException("Underlying input stream ends unexpectedly.");
                return null;
        return buffer;
New to GrepCode? Check out our FAQ X