javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/NativeError.java
changeset 23 98ccebc37403
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Landmark store manager for handling landmark stores
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.media;
       
    20 
       
    21 import java.io.IOException;
       
    22 
       
    23 /**
       
    24  * A utility class for declaring and handling native SymbianOS error codes. All
       
    25  * error codes that are used in Java from native code should be declared here
       
    26  * and referenced from this class.
       
    27  */
       
    28 public final class NativeError
       
    29 {
       
    30     public static final String NATIVE_ERROR_MESSAGE = "OS error = ";
       
    31 
       
    32     // SymbianOS error codes
       
    33     public static final int KErrNone = 0;
       
    34 
       
    35     public static final int KErrNotFound = -1;
       
    36 
       
    37     public static final int KErrGeneral = -2;
       
    38 
       
    39     public static final int KErrCancel = -3;
       
    40 
       
    41     public static final int KErrNoMemory = -4;
       
    42 
       
    43     public static final int KErrNotSupported = -5;
       
    44 
       
    45     public static final int KErrArgument = -6;
       
    46 
       
    47     public static final int KErrOverflow = -9;
       
    48 
       
    49     public static final int KErrAlreadyExists = -11;
       
    50 
       
    51     public static final int KErrPathNotFound = -12;
       
    52 
       
    53     public static final int KErrDied = -13;
       
    54 
       
    55     public static final int KErrNotReady = -18;
       
    56 
       
    57     public static final int KErrCorrupt = -20;
       
    58 
       
    59     public static final int KErrAccessDenied = -21;
       
    60 
       
    61     public static final int KErrWrite = -23;
       
    62 
       
    63     public static final int KErrEof = -25;
       
    64 
       
    65     public static final int KErrDiskFull = -26;
       
    66 
       
    67     public static final int KErrBadName = -28;
       
    68 
       
    69     public static final int KErrCommsLineFail = -29;
       
    70 
       
    71     public static final int KErrTimedOut = -33;
       
    72 
       
    73     public static final int KErrDisconnected = -36;
       
    74 
       
    75     public static final int KErrTooBig = -40;
       
    76 
       
    77     public static final int KErrDivideByZero = -41;
       
    78 
       
    79     public static final int KErrHardwareNotAvailable = -44;
       
    80 
       
    81     // Not intended to be constructed
       
    82     private NativeError()
       
    83     {
       
    84     }
       
    85 
       
    86     /**
       
    87      * Checks for basic native error codes that map to standard Java exceptions
       
    88      * and throws the exception if the error code matches. Otherwise throws an
       
    89      * IOException.
       
    90      *
       
    91      * @param aError
       
    92      *            Possible error code.
       
    93      * @return Value passed in is returned if not an error.
       
    94      */
       
    95     public static int checkIO(int aError,
       
    96                               String aErrorMessage) throws IOException
       
    97     {
       
    98         if (aError < KErrNone)
       
    99         {
       
   100             switch (aError)
       
   101             {
       
   102             case KErrNoMemory:
       
   103                 throw new OutOfMemoryError(aErrorMessage +
       
   104                                            Integer.toString(aError));
       
   105                 // KErrArgument must throw IllegalArgumentException
       
   106                 // otherwise lcdui will break, so don't change this.
       
   107             case KErrArgument:
       
   108                 throw new IllegalArgumentException(aErrorMessage +
       
   109                                                    Integer.toString(aError));
       
   110             case KErrDivideByZero:
       
   111                 throw new ArithmeticException(aErrorMessage +
       
   112                                               Integer.toString(aError));
       
   113             default:
       
   114                 throw new IOException(aErrorMessage + errorMessage(aError));
       
   115             }
       
   116         }
       
   117         return aError;
       
   118     }
       
   119 
       
   120     /**
       
   121      * Checks for basic native error codes that map to standard Java exceptions
       
   122      * and throws the exception if the error code matches. Otherwise throws
       
   123      * basic Error class.
       
   124      *
       
   125      * @param aError
       
   126      *            Possible error code.
       
   127      * @param aThrowAlways
       
   128      *            Determines whether a default exception is thrown if the error
       
   129      *            code is not recognised.
       
   130      * @return Value passed in is returned if not an error.
       
   131      * @throws Error
       
   132      *             If the error code does not match any exception thrown in
       
   133      *             checkExplicitOnly, a default exception is thrown here.
       
   134      */
       
   135     public static int check(int aError, String aErrorMessage)
       
   136     {
       
   137         if (aError < KErrNone)
       
   138         {
       
   139             checkExplicitOnly(aError);
       
   140             throw new Error(aErrorMessage + errorMessage(aError));
       
   141         }
       
   142         return aError;
       
   143     }
       
   144 
       
   145 
       
   146 	/**
       
   147 	 * Checks if the object is a null reference, and throws OutOfMemoryError
       
   148 	 * if this is the case. Useful for checking for OOM when Java objects
       
   149 	 * returned from a native method.
       
   150 	 * 
       
   151 	 * @param aObject Object that may be null.
       
   152 	 * @return Value passed in is returned if not an error.
       
   153 	 */
       
   154 	public static Object checkOOM( Object aObject )
       
   155 		{
       
   156 		if ( aObject == null )
       
   157 			{
       
   158 			throw new OutOfMemoryError();
       
   159 			}
       
   160 		return aObject;
       
   161 		}
       
   162 		
       
   163 		/**
       
   164      * Checks if the error code represents out of memory, and throws Java Error
       
   165      * if true. Does not throw anything otherwise.
       
   166      *
       
   167      * @param aError Possible error code.
       
   168      * @return Value passed in is returned if not an out of memory error.
       
   169      */
       
   170     public static int checkOOMOnly(int aError)
       
   171     {
       
   172         if (aError == KErrNoMemory)
       
   173         {
       
   174             throw new OutOfMemoryError();
       
   175         }
       
   176         return aError;
       
   177     }
       
   178 		
       
   179 		/**
       
   180      * Checks for basic native error codes that map to standard Java
       
   181      * exceptions and throws the exception if the error code matches.
       
   182      * Otherwise throws basic Error class.
       
   183      *
       
   184      * @param aError Possible error code.
       
   185      * @param aThrowAlways Determines whether a default exception is thrown
       
   186      * if the error code is not recognised.
       
   187      * @return Value passed in is returned if not an error.
       
   188      * @throws Error If the error code does not match any exception thrown
       
   189      * in checkExplicitOnly, a default exception is thrown here.
       
   190      */
       
   191     public static int check(int aError)
       
   192     {
       
   193         if (aError < KErrNone)
       
   194         {
       
   195             checkExplicitOnly(aError);
       
   196             throw new Error(errorMessage(aError));
       
   197         }
       
   198         return aError;
       
   199     }
       
   200     		
       
   201 	/**
       
   202 	 * Checks for basic native error codes that map to standard Java
       
   203 	 * exceptions and throws the exception if the error code matches.
       
   204 	 * Otherwise just returns the error.
       
   205 	 * 
       
   206 	 * @param aError Possible error code.
       
   207 	 * @return Value passed in is returned if not an error.
       
   208 	 * @throws OutOfMemoryError If aError equals KErrNoMemory.
       
   209 	 * @throws IllegalArgumentException If aError equals KErrArgument
       
   210 	 * @throws ArithmeticException If aError equals KErrDivideByZero
       
   211 	 */
       
   212 	public static int checkExplicitOnly( int aError )
       
   213 		{
       
   214 		if ( aError < KErrNone )
       
   215 			{
       
   216 			switch ( aError )
       
   217 				{
       
   218 				case KErrNoMemory:
       
   219 					throw new OutOfMemoryError();
       
   220 				// KErrArgument must throw IllegalArgumentException
       
   221 				// otherwise lcdui will break, so don't change this.
       
   222 				case KErrArgument:
       
   223 					throw new IllegalArgumentException();
       
   224 				case KErrDivideByZero:
       
   225 					throw new ArithmeticException();
       
   226 				default:
       
   227 					// Do nothing
       
   228 				}
       
   229 			}
       
   230 		return aError;
       
   231 		}		
       
   232 
       
   233     /**
       
   234      * Returns a string formatted with generic text to indicate where the error
       
   235      * code comes from and the error code given.
       
   236      *
       
   237      * @param A
       
   238      *            native error code.
       
   239      * @return A string containing the error code.
       
   240      */
       
   241     public static String errorMessage(int aError)
       
   242     {
       
   243         String result = NATIVE_ERROR_MESSAGE.concat(Integer.toString(aError));
       
   244         return result;
       
   245     }
       
   246 
       
   247 }