changeset 183:60ec35bb6d26

8086054: There should be no IAE if buffer is not ready for operation Summary: Buffer capacity is checked, free space is not counted no more Reviewed-by: alkonsta
author snazarki
date Tue, 09 Jun 2015 16:19:20 +0300
parents b69e341f7472
children 551d3edd1fcf
files src/se/classes/com/oracle/dio/utils/ExceptionMessage.java src/share/classes/com/oracle/dio/i2cbus/impl/I2CCombinedMessage.java src/share/classes/com/oracle/dio/i2cbus/impl/I2CSlaveImpl.java src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java src/share/classes/com/oracle/dio/spibus/impl/SPISlaveImpl.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java
diffstat 6 files changed, 56 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/utils/ExceptionMessage.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/se/classes/com/oracle/dio/utils/ExceptionMessage.java	Tue Jun 09 16:19:20 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -58,7 +58,9 @@
     public static final int DEVICE_OPEN_WITH_DEVICENAME_UNSUPPORTED  = DEVICE_FIRST + 22;
     public static final int BUFFER_IS_MODIFIED                       = DEVICE_FIRST + 23;
     public static final int CLONE_ERROR                              = DEVICE_FIRST + 24;
-    public static final int DEVICE_LAST = CLONE_ERROR;
+    public static final int ZERO_CAPACITY_BUFFER                     = DEVICE_FIRST + 25;
+    public static final int NULL_LISTENER                            = DEVICE_FIRST + 26;
+    public static final int DEVICE_LAST = NULL_LISTENER;
 
     public static final int ADC_FIRST = DEVICE_LAST + 1;
     public static final int ADC_ANOTHER_OPERATION_PROGRESS           = ADC_FIRST + 0;
@@ -66,8 +68,7 @@
     public static final int ADC_LESS_MINIMAL_INTERVAL                = ADC_FIRST + 2;
     public static final int ADC_CANNOT_START_ACQUISITION             = ADC_FIRST + 3;
     public static final int ADC_ARGUMENT_LOW_GREATER_THAN_HIGH       = ADC_FIRST + 4;
-    public static final int ADC_BUFFER_NO_SPACE                      = ADC_FIRST + 5;
-    public static final int ADC_LAST = ADC_BUFFER_NO_SPACE;
+    public static final int ADC_LAST = ADC_ARGUMENT_LOW_GREATER_THAN_HIGH;
 
     public static final int ATCMD_FIRST = ADC_LAST + 1;
     public static final int ATCMD_DATA_CONNECTION_UNSUPPORTED        = ATCMD_FIRST + 0;
@@ -82,17 +83,15 @@
     public static final int COUNTER_NOT_SUSPENDED                    = COUNTER_FIRST + 5;
     public static final int COUNTER_IS_STARTED                       = COUNTER_FIRST + 6;
     public static final int COUNTER_NONPOSITIVE_LIMIT_AND_INTERVAL   = COUNTER_FIRST + 7;
-    public static final int COUNTER_NULL_LISTENER                    = COUNTER_FIRST + 8;
-    public static final int COUNTER_IS_SUSPENDED                     = COUNTER_FIRST + 9;
+    public static final int COUNTER_IS_SUSPENDED                     = COUNTER_FIRST + 8;
     public static final int COUNTER_LAST = COUNTER_IS_SUSPENDED;
 
     public static final int DAC_FIRST = COUNTER_LAST + 1;
     public static final int DAC_GENERATION_IS_ACTIVE                 = DAC_FIRST + 0;
-    public static final int DAC_NO_BUFFER_DATA                       = DAC_FIRST + 1;
-    public static final int DAC_NONPOSITIVE_INTERVAL                 = DAC_FIRST + 2;
-    public static final int DAC_LESS_MINIMAL_INTERVAL                = DAC_FIRST + 3;
-    public static final int DAC_UNACCEPTABLE_VALUE                   = DAC_FIRST + 4;
-    public static final int DAC_CANNOT_START_CONVERSION              = DAC_FIRST + 5;
+    public static final int DAC_NONPOSITIVE_INTERVAL                 = DAC_FIRST + 1;
+    public static final int DAC_LESS_MINIMAL_INTERVAL                = DAC_FIRST + 2;
+    public static final int DAC_UNACCEPTABLE_VALUE                   = DAC_FIRST + 3;
+    public static final int DAC_CANNOT_START_CONVERSION              = DAC_FIRST + 4;
     public static final int DAC_LAST = DAC_CANNOT_START_CONVERSION;
 
     public static final int GPIO_FIRST = DAC_LAST + 1;
@@ -115,13 +114,12 @@
 
     public static final int I2CBUS_FIRST = GPIO_LAST + 1;
     public static final int I2CBUS_ALREADY_TRANSFERRED_MESSAGE       = I2CBUS_FIRST + 0;
-    public static final int I2CBUS_NULL_BUFFER                       = I2CBUS_FIRST + 1;
-    public static final int I2CBUS_NEGATIVE_SKIP_ARG                 = I2CBUS_FIRST + 2;
-    public static final int I2CBUS_DIFFERENT_BUS_SLAVE_OPERATION     = I2CBUS_FIRST + 3;
-    public static final int I2CBUS_BUFFER_GIVEN_TWICE                = I2CBUS_FIRST + 4;
-    public static final int I2CBUS_CLOSED_DEVICE                     = I2CBUS_FIRST + 5;
-    public static final int I2CBUS_FIRST_MESSAGE                     = I2CBUS_FIRST + 6;
-    public static final int I2CBUS_LAST_MESSAGE                      = I2CBUS_FIRST + 7;
+    public static final int I2CBUS_NEGATIVE_SKIP_ARG                 = I2CBUS_FIRST + 1;
+    public static final int I2CBUS_DIFFERENT_BUS_SLAVE_OPERATION     = I2CBUS_FIRST + 2;
+    public static final int I2CBUS_BUFFER_GIVEN_TWICE                = I2CBUS_FIRST + 3;
+    public static final int I2CBUS_CLOSED_DEVICE                     = I2CBUS_FIRST + 4;
+    public static final int I2CBUS_FIRST_MESSAGE                     = I2CBUS_FIRST + 5;
+    public static final int I2CBUS_LAST_MESSAGE                      = I2CBUS_FIRST + 6;
     public static final int I2CBUS_LAST = I2CBUS_LAST_MESSAGE;
 
     public static final int MMIO_FIRST = I2CBUS_LAST + 1;
@@ -149,32 +147,23 @@
     public static final int PWM_NONPOSITIVE_PERIOD                   = PWM_FIRST + 1;
     public static final int PWM_OUT_OF_RANGE_PERIOD                  = PWM_FIRST + 2;
     public static final int PWM_ILLEGAL_WIDTH_OR_COUNT               = PWM_FIRST + 3;
-    public static final int PWM_NULL_SRC                             = PWM_FIRST + 4;
-    public static final int PWM_NULL_LISTENER                        = PWM_FIRST + 5;
-    public static final int PWM_NO_DATA                              = PWM_FIRST + 6;
-    public static final int PWM_NULL_SRC1_OR_SRC2                    = PWM_FIRST + 7;
-    public static final int PWM_GENERATION_SESSION_ACTIVE            = PWM_FIRST + 8;
+    public static final int PWM_GENERATION_SESSION_ACTIVE            = PWM_FIRST + 4;
     public static final int PWM_LAST = PWM_GENERATION_SESSION_ACTIVE;
 
     public static final int SPIBUS_FIRST = PWM_LAST + 1;
-    public static final int SPIBUS_NULL_BUFFER                       = SPIBUS_FIRST + 0;
-    public static final int SPIBUS_SLAVE_WORD_LENGTH                 = SPIBUS_FIRST + 1;
-    public static final int SPIBUS_BYTE_NUMBER_BELIES_WORD_LENGTH    = SPIBUS_FIRST + 2;
+    public static final int SPIBUS_SLAVE_WORD_LENGTH                 = SPIBUS_FIRST + 0;
+    public static final int SPIBUS_BYTE_NUMBER_BELIES_WORD_LENGTH    = SPIBUS_FIRST + 1;
     public static final int SPIBUS_LAST = SPIBUS_BYTE_NUMBER_BELIES_WORD_LENGTH;
 
     public static final int UART_FIRST = SPIBUS_LAST + 1;
-    public static final int UART_CANT_GET_PORT_NAME               = UART_FIRST + 0;
+    public static final int UART_CANT_GET_PORT_NAME                  = UART_FIRST + 0;
     public static final int UART_UTF8_UNCONVERTIBLE_DEVNAME          = UART_FIRST + 1;
-    public static final int UART_NULL_SRC_OR_LISTENER                = UART_FIRST + 2;
-    public static final int UART_NULL_SRC1_OR_SRC2_OR_LISTENER       = UART_FIRST + 3;
-    public static final int UART_NULL_DST                            = UART_FIRST + 4;
-    public static final int UART_NULL_SRC                            = UART_FIRST + 5;
-    public static final int UART_ACTIVE_READ_OPERATION               = UART_FIRST + 6;
-    public static final int UART_ACTIVE_WRITE_OPERATION              = UART_FIRST + 7;
-    public static final int UART_UNKNOWN_SIGNAL_ID                   = UART_FIRST + 8;
-    public static final int UART_SIGNALS_NOT_BITWISE_COMBINATION     = UART_FIRST + 9;
-    public static final int UART_LISTENER_ALREADY_REGISTERED         = UART_FIRST + 10;
-    public static final int UART_NEGATIVE_TIMEOUT                    = UART_FIRST + 11;
+    public static final int UART_ACTIVE_READ_OPERATION               = UART_FIRST + 2;
+    public static final int UART_ACTIVE_WRITE_OPERATION              = UART_FIRST + 3;
+    public static final int UART_UNKNOWN_SIGNAL_ID                   = UART_FIRST + 4;
+    public static final int UART_SIGNALS_NOT_BITWISE_COMBINATION     = UART_FIRST + 5;
+    public static final int UART_LISTENER_ALREADY_REGISTERED         = UART_FIRST + 6;
+    public static final int UART_NEGATIVE_TIMEOUT                    = UART_FIRST + 7;
 
     private static final String strings[] = {
 
@@ -204,6 +193,8 @@
         "Opening with deviceName is unsupported",
         "Buffer was modified by application",
         "The object can't be cloned",
+        "The buffer has a zero-capacity",
+        "listener is null",
 
         // adc messages
         "Another operation on ADC channel is in progress",
@@ -211,7 +202,6 @@
         "'interval' is less than minimal sampling interval",
         "Cannot start acquisition",
         "Argument 'low' is greater than 'high'",
-        "No free space in buffer",
 
         // atcmd messages
         "Emulator does not support data connection",
@@ -225,12 +215,10 @@
         "Counting wasn't suspended",
         "Counting is already started",
         "Both limit and interval are equal or less than 0",
-        "Counting listener is null",
         "Counting is already suspended",
 
         // dac messages
         "Generation is already active",
-        "No data in buffer",
         "'interval' is negative or 0",
         "'interval' is less than minimal sampling interval",
         "'value' is out of an allowed range",
@@ -255,7 +243,6 @@
 
         // i2c bus messages
         "the message has already been transferred once",
-        "buffer is null",
         "'skip' argument is negative",
         "operation to a slave on a different bus",
         "the same buffer is given twice",
@@ -286,24 +273,15 @@
         "Period %d is negative or zero ",
         "Period %d is out of the supported range",
         "width or count is illegal",
-        "src buffer is null ",
-        "listener is null",
-        "No data in the buffer",
-        "src1 or src2 buffer is null",
         "pulse generation session is already active",
 
         // spi bus messages
-        "Buffer is null",
         "Slave Word Length is %d",
         "the number of bytes to receive/send belies word length",
 
         // uart messages
         "Cannot get serial port name",
         "Unable to convert dev name to UTF-8",
-        "src buffer or listener is null",
-        "src1, src2 buffer or listener is null",
-        "dst buffer is null",
-        "src buffer is null",
         "another synchronous or asynchronous read operation is already active",
         "another synchronous or asynchronous write operation is already active",
         "signalID is not one of the defined values",
--- a/src/share/classes/com/oracle/dio/i2cbus/impl/I2CCombinedMessage.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/share/classes/com/oracle/dio/i2cbus/impl/I2CCombinedMessage.java	Tue Jun 09 16:19:20 2015 +0300
@@ -65,10 +65,8 @@
             );
         }
 
-        if (null == message.buf) {
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.I2CBUS_NULL_BUFFER)
-            );
+        if (0 == message.buf.capacity()) {
+            throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.ZERO_CAPACITY_BUFFER));
         }
 
         if (0 > message.skip) {
--- a/src/share/classes/com/oracle/dio/i2cbus/impl/I2CSlaveImpl.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/share/classes/com/oracle/dio/i2cbus/impl/I2CSlaveImpl.java	Tue Jun 09 16:19:20 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
--- a/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Tue Jun 09 16:19:20 2015 +0300
@@ -191,6 +191,13 @@
         return tmp;
     }
 
+    /* Ensures <code>buffer</code> is not null and not empty*/
+    protected void checkBuffer(Buffer buffer) {
+        if (0 == buffer.capacity()) {
+            throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.ZERO_CAPACITY_BUFFER));
+        }
+    }
+
     /** Tries to lock given pripheral. Waits <code>timeout</code>
      *  time if device is locked by other application.
      *
--- a/src/share/classes/com/oracle/dio/spibus/impl/SPISlaveImpl.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/share/classes/com/oracle/dio/spibus/impl/SPISlaveImpl.java	Tue Jun 09 16:19:20 2015 +0300
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.lang.Runnable;
 import java.nio.ByteBuffer;
+import java.nio.Buffer;
 import java.security.AccessController;
 import java.util.Objects;
 import java.util.Vector;
@@ -288,8 +289,9 @@
     }
 
     // checkWordLen ought to be called before checkBuffer to get byteNum is up to date
-    void checkBuffer(ByteBuffer buffer) {
-        Objects.requireNonNull(buffer, ExceptionMessage.format(ExceptionMessage.SPIBUS_NULL_BUFFER));
+    @Override
+    protected void checkBuffer(Buffer buffer) {
+        super.checkBuffer(buffer);
 
         if ((buffer.remaining() % byteNum) != 0) {
             throw new InvalidWordLengthException(
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Mon Jun 08 16:48:02 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Tue Jun 09 16:19:20 2015 +0300
@@ -30,6 +30,7 @@
 import java.security.AccessControlException;
 import java.security.AccessController;
 import java.util.Hashtable;
+import java.util.Objects;
 import java.util.StringTokenizer;
 import java.util.Timer;
 import java.util.TimerTask;
@@ -488,11 +489,8 @@
      */
     @Override
     public void startWriting(ByteBuffer src, OutputRoundListener<UART, ByteBuffer> listener) throws IOException, UnavailableDeviceException, ClosedDeviceException{
-        if(src == null || listener == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC_OR_LISTENER)
-            );
-        }
+        checkBuffer(src);
+        Objects.requireNonNull(listener, ExceptionMessage.format(ExceptionMessage.NULL_LISTENER));
 
         writeAsync(src, null, listener);
     }
@@ -504,11 +502,9 @@
     public void startWriting(ByteBuffer src1, ByteBuffer src2, OutputRoundListener<UART, ByteBuffer> listener) throws IOException,
         UnavailableDeviceException, ClosedDeviceException{
 
-        if(src1 == null || src2 == null || listener == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC1_OR_SRC2_OR_LISTENER)
-            );
-        }
+        checkBuffer(src1);
+        checkBuffer(src2);
+        Objects.requireNonNull(listener, ExceptionMessage.format(ExceptionMessage.NULL_LISTENER));
 
         writeAsync(src1, src2, listener);
     }
@@ -555,11 +551,8 @@
      */
     @Override
     public void startReading(ByteBuffer src, InputRoundListener<UART, ByteBuffer> listener) throws IOException, UnavailableDeviceException, ClosedDeviceException{
-        if(src == null || listener == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC_OR_LISTENER)
-            );
-        }
+        checkBuffer(src);
+        Objects.requireNonNull(listener, ExceptionMessage.format(ExceptionMessage.NULL_LISTENER));
         readAsync(src, null, listener);
     }
 
@@ -569,12 +562,9 @@
     @Override
     public void startReading(ByteBuffer src1, ByteBuffer src2, InputRoundListener<UART, ByteBuffer> listener) throws IOException,
             UnavailableDeviceException, ClosedDeviceException{
-
-        if(src1 == null || src2 == null || listener == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC1_OR_SRC2_OR_LISTENER)
-            );
-        }
+        checkBuffer(src1);
+        checkBuffer(src2);
+        Objects.requireNonNull(listener, ExceptionMessage.format(ExceptionMessage.NULL_LISTENER));
         readAsync(src1, src2, listener);
     }
 
@@ -661,11 +651,7 @@
             UnavailableDeviceException, ClosedDeviceException{
         int ret;
 
-        if (dst == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_DST)
-            );
-        }
+        checkBuffer(dst);
 
         if(!dst.hasRemaining()){
             ret = 0;
@@ -713,11 +699,8 @@
     @Override
     public int write(ByteBuffer src) throws IOException,
             UnavailableDeviceException, ClosedDeviceException{
-        if (src == null){
-            throw new NullPointerException(
-                ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC)
-            );
-        }
+
+        checkBuffer(src);
 
         checkPowerState();