Guest User

For http://stackoverflow.com/questions/17243652/

a guest
Jun 21st, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1.     private int getOffset(int capacity, int items) {
  2.         return ((getMaxCapacity() + 1) * items + capacity) * BLOCK_SIZE;
  3.     }
  4.  
  5.  
  6.     @Override
  7.     public int getSolution(int capacity, int items) {
  8.         if (items == syncsPassed ) {
  9.             return previousItemSolutions[capacity];
  10.         }
  11.         ByteBuffer byteBuffer = ByteBuffer.allocate(BLOCK_SIZE);
  12.         int offset = getOffset(capacity, items);
  13.         try {
  14.             FileChannel channel = cacheFile.getChannel();
  15.             int readBytes = channel.read(byteBuffer, offset);
  16.  
  17.             System.out.println(readBytes);
  18.  
  19.             return byteBuffer.getInt();
  20.         } catch (IOException e) {
  21.             e.printStackTrace();
  22.             close();
  23.             throw new IllegalStateException(e);
  24.         }
  25.     }
  26.  
  27.     public void sync(int items) {
  28.         checkTrue(items == syncsPassed,
  29.                 "Illegal sync: only " + syncsPassed + " rows were synchronized, but sync is called for row " + items);
  30.         try {
  31.             int offset = getOffset(0, items);
  32.             FileChannel channel = cacheFile.getChannel().position(offset);
  33.             IntBuffer buffer = IntBuffer.wrap(currentItemSolutions);
  34.             ByteBuffer byteBuffer = ByteBuffer.allocate(currentItemSolutions.length * BLOCK_SIZE);
  35.             byteBuffer.asIntBuffer().put(buffer);
  36.             channel.write(byteBuffer);
  37.             channel.force(false);
  38.         } catch (IOException e) {
  39.             close();
  40.             throw new RuntimeException(e);
  41.         }
  42.         this.previousItemSolutions = this.currentItemSolutions.clone();
  43.         this.currentItemSolutions = new int[getMaxCapacity() + 1];
  44.         syncsPassed++;
  45.     }
Add Comment
Please, Sign In to add comment