Wednesday 29 May 2019

BCD Length From Long


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public static byte[] intToBCDBytes(long num) {

  return intToBCDBytes(num, 0);
 }

 public static byte[] intToBCDBytes(long num, int minBytes) {
  int digits = 0;

  long temp = num;
  while (temp != 0) {
   digits++;
   temp /= 10;
  }

  int byteLen = digits % 2 == 0 ? digits / 2 : (digits + 1) / 2;
  boolean isOdd = digits % 2 != 0;

  if (byteLen < minBytes)
   byteLen = minBytes;

  byte bcd[] = new byte[byteLen];

  for (int i = 0; i < digits; i++) {
   byte tmp = (byte) (num % 10);

   if (i == digits - 1 && isOdd)
    bcd[i / 2] = tmp;
   else if (i % 2 == 0)
    bcd[i / 2] = tmp;
   else {
    byte foo = (byte) (tmp << 4);

    bcd[i / 2] |= foo;
   }

   num /= 10;
  }

  for (int i = 0; i < byteLen / 2; i++) {
   byte tmp = bcd[i];
   bcd[i] = bcd[byteLen - i - 1];
   bcd[byteLen - i - 1] = tmp;
  }

  return bcd;
 }

 public static String bcdToString(byte bcd) {
  StringBuffer sb = new StringBuffer();

  byte high = (byte) (bcd & 0xf0);
  high >>>= (byte) 4;
  high = (byte) (high & 0x0f);
  byte low = (byte) (bcd & 0x0f);

  sb.append(high);
  sb.append(low);

  return sb.toString();
 }

 public static Integer bcdToInt(byte[] bcd) {

  StringBuffer sb = new StringBuffer();

  for (int i = 0; i < bcd.length; i++) {
   sb.append(bcdToString(bcd[i]));
  }

  return new Integer(sb.toString());
 }

Friday 26 April 2019

Java ClassPath Issues

When you specify -jar then the -cp parameter will be ignored.
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp
    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

How to add S3 based maven repo into gradle repositories


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
buildscript {
 repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    jcenter()
    mavenCentral()
  }
  dependencies {
    classpath 'com.amazonaws:aws-java-sdk:1.11.214' 
    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6-rc1'
  }
}



def awsProfile = "prfilename" //default/saml/east1/anything your configured in aws credentials file
def mavenRepo   = "s3://repo.your.com"

ext {
    samlCredentials = new com.amazonaws.auth.profile.ProfileCredentialsProvider(awsProfile).credentials
}




repositories {
    maven {
        //url "s3://myCompanyBucket/maven2"
        url "${mavenRepo}/releases"
        credentials(AwsCredentials) {
          //  accessKey "someKey"
          //  secretKey "someSecret"
            // optional
           // sessionToken "someSTSToken"
            accessKey samlCredentials.getAWSAccessKeyId()
         secretKey samlCredentials.getAWSSecretKey()
         if (System.env.BUILD_NUMBER==null) {
            if (samlCredentials.getSessionToken()!=null) {
              sessionToken samlCredentials.getSessionToken()
            }
         }
        }
    }
  
  mavenCentral()
  jcenter()
    
 }