1 18 19 package jcifs.smb; 20 21 import java.io.IOException ; 22 import java.io.StringWriter ; 23 import java.io.PrintWriter ; 24 import jcifs.util.Hexdump; 25 26 39 40 public class SmbException extends IOException implements NtStatus, DosError, WinError { 41 42 static String getMessageByCode( int errcode ) { 43 if(( errcode & 0xC0000000 ) == 0xC0000000 ) { 44 int min = 0; 45 int max = NT_STATUS_CODES.length; 46 47 while( max >= min ) { 48 int mid = (min + max) / 2; 49 50 if( errcode > NT_STATUS_CODES[mid] ) { 51 min = mid + 1; 52 } else if( errcode < NT_STATUS_CODES[mid] ) { 53 max = mid - 1; 54 } else { 55 return NT_STATUS_MESSAGES[mid]; 56 } 57 } 58 } else { 59 int min = 0; 60 int max = DOS_ERROR_CODES.length; 61 62 while( max >= min ) { 63 int mid = (min + max) / 2; 64 65 if( errcode > DOS_ERROR_CODES[mid][0] ) { 66 min = mid + 1; 67 } else if( errcode < DOS_ERROR_CODES[mid][0] ) { 68 max = mid - 1; 69 } else { 70 return DOS_ERROR_MESSAGES[mid]; 71 } 72 } 73 } 74 75 return "0x" + Hexdump.toHexString( errcode, 8 ); 76 } 77 static int getStatusByCode( int errcode ) { 78 if(( errcode & 0xC0000000 ) != 0 ) { 79 return errcode; 80 } else { 81 int min = 0; 82 int max = DOS_ERROR_CODES.length; 83 84 while( max >= min ) { 85 int mid = (min + max) / 2; 86 87 if( errcode > DOS_ERROR_CODES[mid][0] ) { 88 min = mid + 1; 89 } else if( errcode < DOS_ERROR_CODES[mid][0] ) { 90 max = mid - 1; 91 } else { 92 return DOS_ERROR_CODES[mid][1]; 93 } 94 } 95 } 96 97 return NT_STATUS_UNSUCCESSFUL; 98 } 99 static String getMessageByWinerrCode( int errcode ) { 100 int min = 0; 101 int max = WINERR_CODES.length; 102 103 while( max >= min ) { 104 int mid = (min + max) / 2; 105 106 if( errcode > WINERR_CODES[mid] ) { 107 min = mid + 1; 108 } else if( errcode < WINERR_CODES[mid] ) { 109 max = mid - 1; 110 } else { 111 return WINERR_MESSAGES[mid]; 112 } 113 } 114 115 return errcode + ""; 116 } 117 118 119 private int status; 120 private Throwable rootCause; 121 122 SmbException() { 123 } 124 SmbException( int errcode, Throwable rootCause ) { 125 super( getMessageByCode( errcode )); 126 status = getStatusByCode( errcode ); 127 this.rootCause = rootCause; 128 } 129 SmbException( String msg ) { 130 super( msg ); 131 status = NT_STATUS_UNSUCCESSFUL; 132 } 133 SmbException( String msg, Throwable rootCause ) { 134 super( msg ); 135 this.rootCause = rootCause; 136 status = NT_STATUS_UNSUCCESSFUL; 137 } 138 SmbException( int errcode, boolean winerr ) { 139 super( winerr ? getMessageByWinerrCode( errcode ) : getMessageByCode( errcode )); 140 status = winerr ? errcode : getStatusByCode( errcode ); 141 } 142 143 public int getNtStatus() { 144 return status; 145 } 146 public Throwable getRootCause() { 147 return rootCause; 148 } 149 public String toString() { 150 if( rootCause != null ) { 151 StringWriter sw = new StringWriter (); 152 PrintWriter pw = new PrintWriter ( sw ); 153 rootCause.printStackTrace( pw ); 154 return super.toString() + "\n" + sw; 155 } else { 156 return super.toString(); 157 } 158 } 159 } 160 161 | Popular Tags |