Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) 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 http://www.apache.org/licenses/LICENSE-2.0 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.

Parameters:
in The underlying input stream to be filtered.
Throws:
java.io.IOException 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.

Returns:
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:
java.io.IOException In case of any IO exception.
 
     @Override
     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;
     }
 
     @Override
     public void close() throws IOException {
         .close();
    }

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

Parameters:
length Length of data block to read.
throwCorrupted Throw EOF exception if no enough data to build data block.
Returns:
The next read data block or null if no requested data found.
Throws:
java.io.IOException In case of any IO exception.
    @Nullable
    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