You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/07/30 14:51:08 UTC
svn commit: r560953 - in /harmony/enhanced/classlib/trunk/modules/nio/src:
main/java/java/nio/MappedByteBufferAdapter.java
test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java
Author: tellison
Date: Mon Jul 30 05:51:07 2007
New Revision: 560953
URL: http://svn.apache.org/viewvc?view=rev&rev=560953
Log:
Fix for incorrect limit set when converting mapped byte buffers.
Added:
harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java
Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java?view=diff&rev=560953&r1=560952&r2=560953
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/MappedByteBufferAdapter.java Mon Jul 30 05:51:07 2007
@@ -21,7 +21,7 @@
import org.apache.harmony.nio.internal.DirectBuffer;
-class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
+final class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
private static final int CHAR_SIZE = 2;
@@ -101,10 +101,9 @@
}
public byte get() {
- if (this.position == this.limit) {
- throw new BufferUnderflowException();
- }
- return this.wrapped.get(this.position++);
+ byte result = this.wrapped.get();
+ this.position++;
+ return result;
}
public byte get(int index) {
@@ -112,12 +111,8 @@
}
public char getChar() {
- int newPosition = this.position + CHAR_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- char result = this.wrapped.getChar(this.position);
- this.position = newPosition;
+ char result = this.wrapped.getChar();
+ this.position += CHAR_SIZE;
return result;
}
@@ -126,12 +121,8 @@
}
public double getDouble() {
- int newPosition = this.position + DOUBLE_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- double result = this.wrapped.getDouble(this.position);
- this.position = newPosition;
+ double result = this.wrapped.getDouble();
+ this.position += DOUBLE_SIZE;
return result;
}
@@ -144,12 +135,8 @@
}
public float getFloat() {
- int newPosition = this.position + FLOAT_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- float result = this.wrapped.getFloat(this.position);
- this.position = newPosition;
+ float result = this.wrapped.getFloat();
+ this.position += FLOAT_SIZE;
return result;
}
@@ -158,12 +145,8 @@
}
public int getInt() {
- int newPosition = this.position + INTEGER_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- int result = this.wrapped.getInt(this.position);
- this.position = newPosition;
+ int result = this.wrapped.getInt();
+ this.position += INTEGER_SIZE;
return result;
}
@@ -172,12 +155,8 @@
}
public long getLong() {
- int newPosition = this.position + LONG_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- long result = this.wrapped.getLong(this.position);
- this.position = newPosition;
+ long result = this.wrapped.getLong();
+ this.position += LONG_SIZE;
return result;
}
@@ -186,12 +165,8 @@
}
public short getShort() {
- int newPosition = this.position + SHORT_SIZE;
- if (newPosition > this.limit) {
- throw new BufferUnderflowException();
- }
- short result = this.wrapped.getShort(this.position);
- this.position = newPosition;
+ short result = this.wrapped.getShort();
+ this.position += SHORT_SIZE;
return result;
}
@@ -213,10 +188,8 @@
}
public ByteBuffer put(byte b) {
- if (this.position == this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.put(this.position++, b);
+ this.wrapped.put(b);
+ this.position++;
return this;
}
@@ -233,12 +206,8 @@
}
public ByteBuffer putChar(char value) {
- int newPosition = this.position + CHAR_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putChar(this.position, value);
- this.position = newPosition;
+ this.wrapped.putChar(value);
+ this.position += CHAR_SIZE;
return this;
}
@@ -248,12 +217,8 @@
}
public ByteBuffer putDouble(double value) {
- int newPosition = this.position + DOUBLE_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putDouble(this.position, value);
- this.position = newPosition;
+ this.wrapped.putDouble(value);
+ this.position += DOUBLE_SIZE;
return this;
}
@@ -263,12 +228,8 @@
}
public ByteBuffer putFloat(float value) {
- int newPosition = this.position + FLOAT_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putFloat(this.position, value);
- this.position = newPosition;
+ this.wrapped.putFloat(value);
+ this.position += FLOAT_SIZE;
return this;
}
@@ -283,12 +244,8 @@
}
public ByteBuffer putInt(int value) {
- int newPosition = this.position + INTEGER_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putInt(this.position, value);
- this.position = newPosition;
+ this.wrapped.putInt(value);
+ this.position += INTEGER_SIZE;
return this;
}
@@ -298,12 +255,8 @@
}
public ByteBuffer putLong(long value) {
- int newPosition = this.position + LONG_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putLong(this.position, value);
- this.position = newPosition;
+ this.wrapped.putLong(value);
+ this.position += LONG_SIZE;
return this;
}
@@ -313,12 +266,8 @@
}
public ByteBuffer putShort(short value) {
- int newPosition = this.position + SHORT_SIZE;
- if (newPosition > this.limit) {
- throw new BufferOverflowException();
- }
- this.wrapped.putShort(this.position, value);
- this.position = newPosition;
+ this.wrapped.putShort(value);
+ this.position += SHORT_SIZE;
return this;
}
@@ -359,7 +308,7 @@
this.wrapped.free();
}
- public int getByteCapacity() {
- return wrapped.getByteCapacity();
- }
+ public int getByteCapacity() {
+ return wrapped.getByteCapacity();
+ }
}
Added: harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java?view=auto&rev=560953
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java Mon Jul 30 05:51:07 2007
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.harmony.nio.tests.java.nio;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+
+import junit.framework.TestCase;
+
+public class MappedByteBufferTest extends TestCase {
+
+ /**
+ * A regression test for failing to correctly set capacity of underlying
+ * wrapped buffer from a mapped byte buffer.
+ */
+ public void testasIntBuffer() throws IOException {
+ // Create temp file with 26 bytes and 5 ints
+ File tmpFile = File.createTempFile("harmony", "test"); //$NON-NLS-1$//$NON-NLS-2$
+ tmpFile.deleteOnExit();
+ FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
+ FileChannel fileChannel = fileOutputStream.getChannel();
+ ByteBuffer byteBuffer = ByteBuffer.allocateDirect(26 + 20);
+ for (int i = 0; i < 26; i++) {
+ byteBuffer.put((byte) ('A' + i));
+ }
+ for (int i = 0; i < 5; i++) {
+ byteBuffer.putInt(i + 1);
+ }
+ byteBuffer.rewind();
+ fileChannel.write(byteBuffer);
+ fileChannel.close();
+ fileOutputStream.close();
+
+ // Map file
+ FileInputStream fis = new FileInputStream(tmpFile);
+ FileChannel fc = fis.getChannel();
+ MappedByteBuffer mmb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc
+ .size());
+ int len = mmb.capacity();
+ assertEquals("Got wrong number of bytes", 46, len); //$NON-NLS-1$
+
+ // Read in our 26 bytes
+ for (int i = 0; i < 26; i++) {
+ byte b = mmb.get();
+ assertEquals("Got wrong byte value", (byte) 'A' + i, b); //$NON-NLS-1$
+ }
+
+ // Now convert to an IntBuffer to read our ints
+ IntBuffer ibuffer = mmb.asIntBuffer();
+ for (int i = 0; i < 5; i++) {
+ int val = ibuffer.get();
+ assertEquals("Got wrong int value", i + 1, val); //$NON-NLS-1$
+ }
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/MappedByteBufferTest.java
------------------------------------------------------------------------------
svn:eol-style = native