160 StringBuffer sb = new StringBuffer();
161 sb.setLength(2);
162 ByteBuffer[] dests = new ByteBuffer[4];
163 Path p = DirectIOTest.createTempFile();
164
165 initTestFile(p);
166
167 try (FileChannel fc = FileChannel.open(p,
168 StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
169 ExtendedOpenOption.DIRECT)) {
170 int randomNumber = -1;
171
172 for (int i = 0; i < 4; i++) {
173 dests[i] = ByteBuffer.allocateDirect
174 (charsPerGroup + alignment - 1).alignedSlice(alignment);
175 for (int j = 0; j < charsPerGroup; j++) {
176 dests[i].put(j, (byte)'a');
177 }
178 }
179
180 randomNumber = generator.nextInt(100);
181 long offset = randomNumber * charsPerGroup;
182 fc.position(offset);
183 fc.read(dests, 1, 2);
184
185 for (int i = 0; i < 4; i++) {
186 if (i == 1 || i == 2) {
187 for (int j = 0; j < 2; j++) {
188 byte aByte = dests[i].get(j);
189 sb.setCharAt(j, (char)aByte);
190 }
191 int result = Integer.parseInt(sb.toString());
192 int expectedResult = randomNumber + i - 1;
193 if (result != expectedResult) {
194 err.println("I expected " + expectedResult);
195 err.println("I got " + result);
196 throw new Exception("Read test failed");
197 }
198 } else {
199 for (int k = 0; k < charsPerGroup; k++) {
200 if (dests[i].get(k) != (byte)'a')
|
160 StringBuffer sb = new StringBuffer();
161 sb.setLength(2);
162 ByteBuffer[] dests = new ByteBuffer[4];
163 Path p = DirectIOTest.createTempFile();
164
165 initTestFile(p);
166
167 try (FileChannel fc = FileChannel.open(p,
168 StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
169 ExtendedOpenOption.DIRECT)) {
170 int randomNumber = -1;
171
172 for (int i = 0; i < 4; i++) {
173 dests[i] = ByteBuffer.allocateDirect
174 (charsPerGroup + alignment - 1).alignedSlice(alignment);
175 for (int j = 0; j < charsPerGroup; j++) {
176 dests[i].put(j, (byte)'a');
177 }
178 }
179
180 // The size of the test FileChannel is 100*charsPerGroup.
181 // As the channel bytes will be scattered into two buffers
182 // each of size charsPerGroup, the offset cannot be greater
183 // than 98*charsPerGroup, so the value of randomNumber must
184 // be in the range [0,98], i.e., 0 <= randomNumber < 99.
185 randomNumber = generator.nextInt(99);
186 long offset = randomNumber * charsPerGroup;
187 fc.position(offset);
188 fc.read(dests, 1, 2);
189
190 for (int i = 0; i < 4; i++) {
191 if (i == 1 || i == 2) {
192 for (int j = 0; j < 2; j++) {
193 byte aByte = dests[i].get(j);
194 sb.setCharAt(j, (char)aByte);
195 }
196 int result = Integer.parseInt(sb.toString());
197 int expectedResult = randomNumber + i - 1;
198 if (result != expectedResult) {
199 err.println("I expected " + expectedResult);
200 err.println("I got " + result);
201 throw new Exception("Read test failed");
202 }
203 } else {
204 for (int k = 0; k < charsPerGroup; k++) {
205 if (dests[i].get(k) != (byte)'a')
|