1 package com.github.davidmoten.rx2.internal.flowable.buffertofile;
2
3 import java.io.File;
4 import java.util.Arrays;
5
6 public final class Page {
7
8 private static final boolean debug = false;
9
10 private final int pageSize;
11 private final MemoryMappedFile mm;
12
13 public Page(File file, int pageSize) {
14 this.pageSize = pageSize;
15 this.mm = new MemoryMappedFile(file, pageSize);
16 }
17
18 public int length() {
19 return pageSize;
20 }
21
22 public void put(int position, byte[] bytes, int start, int length) {
23 if (debug)
24 println("put at " + this.hashCode() + ":" + position + " of "
25 + Arrays.toString(Arrays.copyOfRange(bytes, start, start + length)));
26 mm.putBytes(position, bytes, start, length);
27 }
28
29 public void putIntOrdered(int writePosition, int value) {
30 if (debug)
31 println("putIntOrdered at " + this.hashCode() + ":" + writePosition + " of " + value);
32 mm.putOrderedInt(writePosition, value);
33 }
34
35 public void putInt(int writePosition, int value) {
36 if (debug)
37 println("putInt at " + this.hashCode() + ":" + writePosition + " of " + value);
38 mm.putInt(writePosition, value);
39 }
40
41 public void get(byte[] dst, int offset, int readPosition, int length) {
42 if (debug)
43 println("getting at " + this.hashCode() + ":" + readPosition + " length=" + length);
44 mm.getBytes(readPosition, dst, offset, length);
45 }
46
47 public int getIntVolatile(int readPosition) {
48 int n = mm.getIntVolatile(readPosition);
49 if (debug)
50 println("getting int volatile at " + this.hashCode() + ":" + readPosition + "=" + n);
51 return n;
52 }
53
54 public void close() {
55 mm.close();
56 }
57
58 static void println(String s) {
59 System.out.println(Thread.currentThread().getName() + ": " + s);
60 }
61
62 public int avail(int position) {
63 return pageSize - position;
64 }
65
66 public int getInt(int readPosition) {
67 return mm.getInt(readPosition);
68 }
69
70 public void putByte(int currentWritePosition, byte b) {
71 mm.putByte(currentWritePosition, b);
72 }
73
74 public byte getByte(int readPosition) {
75 return mm.getByte(readPosition);
76 }
77 }