magic-numbers

Solution for Magic Number issue…?

家住魔仙堡 提交于 2021-02-08 03:06:34
问题 Consider the following code segment... public static UserStatus getEnum(int code) { switch (code) { case 0: return PENDING; case 1: return ACTIVE; case 2: return SUSPENDED; case 3: return DELETED; case 4: return LOGIN_DISABLED; default: return null; } } Now number 3 and 4 in cases(case 3 and case 4) are detected as magic numbers by SONAR. To avoid that issue I changed my code segment as follows... public static UserStatus getEnum(int code) { final int Pending=0; final int Active=1; final int

Solution for Magic Number issue…?

瘦欲@ 提交于 2021-02-08 02:59:20
问题 Consider the following code segment... public static UserStatus getEnum(int code) { switch (code) { case 0: return PENDING; case 1: return ACTIVE; case 2: return SUSPENDED; case 3: return DELETED; case 4: return LOGIN_DISABLED; default: return null; } } Now number 3 and 4 in cases(case 3 and case 4) are detected as magic numbers by SONAR. To avoid that issue I changed my code segment as follows... public static UserStatus getEnum(int code) { final int Pending=0; final int Active=1; final int

How to get the magic number from File in java

怎甘沉沦 提交于 2021-01-27 11:42:33
问题 I have file from UploadedFile button, and I want to print the extension files by use in magic number, My code: UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue(); byte[] fileByteArray = IOUtils.toByteArray(file.getInputStream()); pay attention: Mime type and content file (from file and from the filename) not same to magic number (magic number comes from the first bytes of the inputStream) How can I do it? 回答1: I know this is an old question, just put my answer here hopefully

How to get the magic number from File in java

≡放荡痞女 提交于 2021-01-27 11:39:47
问题 I have file from UploadedFile button, and I want to print the extension files by use in magic number, My code: UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue(); byte[] fileByteArray = IOUtils.toByteArray(file.getInputStream()); pay attention: Mime type and content file (from file and from the filename) not same to magic number (magic number comes from the first bytes of the inputStream) How can I do it? 回答1: I know this is an old question, just put my answer here hopefully

Original file bytes from StreamReader, magic number detection

限于喜欢 提交于 2020-03-18 12:15:37
问题 I'm trying to differentiate between "text files" and "binary" files, as I would effectively like to ignore files with "unreadable" contents. I have a file that I believe is a GZIP archive. I'm tring to ignore this kind of file by detecting the magic numbers / file signature. If I open the file with the Hex editor plugin in Notepad++ I can see the first three hex codes are 1f 8b 08 . However if I read the file using a StreamReader , I'm not sure how to get to the original bytes.. using (var

Is -1 a magic number? An anti-pattern? A code smell? Quotes and guidelines from authorities [duplicate]

試著忘記壹切 提交于 2020-01-12 04:38:09
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Constant abuse? I've seen -1 used in various APIs, most commonly when searching into a "collection" with zero-based indices, usually to indicate the "not found" index. This "works" because -1 is never a legal index to begin with. It seems that any negative number should work, but I think -1 is almost always used, as some sort of (unwritten?) convention. I would like to limit the scope to Java at least for now.

Why do we need a magic number in the beginning of the .class file?

跟風遠走 提交于 2019-12-30 04:00:26
问题 I read a few posts here about the magic number 0xCAFEBABE in the beginning of each java .class file and wanted to know why it is needed - what is the purpose of this marking? Is it still needed anymore? or is it just for backwards compatibility now? Couldn't find a post that answers this - nor did I see the answer in the java spec 回答1: The magic number is basically an identifier for a file format. A JPEG for example always starts with FFD8. It is not necessary for Java itself, it simply helps

What does '0xDEAD' mean in the following code?

余生长醉 提交于 2019-12-23 11:21:08
问题 There is an enum structure,but I don't understand the meaning of '0xDEAD - 2' in this enum. enum TerminatedTypes { _not_terminated = 0xDEAD - 2, _thread_exiting, _thread_terminated, _vm_exited }; From the code above,What kind of benefit can I get? The code above is in 'hotspot/src/share/vm/runtime/thread.hpp' in openjdk8. I am studying source code of jdk,please help me. 回答1: It's a hex literal, being used as an eyecatcher (useful in debuggers) so that the _thread_terminated value will be

What is 0xbbadbeef used for in Webkit?

China☆狼群 提交于 2019-12-22 10:25:14
问题 While working with Webkit I encountered an error with a pointer set to 0xbbadbeef. What is BadBeef used for in Webkit? 回答1: It is a hexspeak used in WebKit and, it indicates a known, unrecoverable error such as out of memory. As you can see from the link https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/wtf/Assertions.h&l=180 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter. Use CRASH

AddOutParameter - non-magic number way of finding length of DBType.Int32

北城余情 提交于 2019-12-22 05:38:13
问题 I have a magic number in the following code... Microsoft.Practices.EnterpriseLibrary.Data.Database db = /* code omitted */; db.AddOutParameter(command, "@ParamName", DbType.Int32, 8); Is there a clean way to get the length of DbType.Int32 , as required for the last argument to AddOutParameter ? 回答1: Not sure what you mean about length. It's a 32 bit int so it's 4 bytes which can be 10 digits as described in this quote from this MSDN page. An integral type representing signed 32-bit integers