Categories
discuss

regex VS Contains. Best Performance? [closed]

I want to compare an URI String over different patterns in java and I want fastest code possible.

Should I use :

if(uri.contains("/br/fab") || uri.contains("/br/err") || uri.contains("/br/sts")

Or something like :

if(uri.matches(".*/br/(fab|err|sts).*"))

Note that I can have a lot more uri and this method is called very often.

What is the best answer between my choices ?

Answer

They’re both fast enough to be over before you know it. I’d go for the one that you can read more easily.

Categories
discuss

dependency management with maven

I have lately become a big fan of Maven for controlling the build cycle for my application. However I’ve encountered some rough edges with Maven’s dependency management. I’m wondering if these are limitations of the tool and paradigm, necessary evils of dependancy management, or if I”m just using the tool incorrectly.

  1. First is the matter of transitive dependencies. As I understand it, if you provide a dependency, Maven will in turn find any dependencies of that dependency. That is great, but for many of my dependencies, this has not worked. For example, including Hibernate in my project:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>
    

    Results in a missing dependency of slf4j. I need to manually add this dependency which I assumed would be Maven’s job. The same goes for Spring. If I add Spring-MVC as a dependency, shouldn’t all of the basic servlet dependencies be added for me (because Spring-MVC would need this stuff)? I’m referring to the servlet, jsp, jstl libraries.

  2. Second is the management of repositories. Maven comes shipped with a default main repository, but I’ve found that in many cases this repository is not up to date. For example, ifyou want spring3, you have to manually add the springsource repository, and if you want hibernate 3.5+ you have to add the jboss repository. It seems to defeat the point of automatic dependency management when you have to hunt down the correct repositories yourself. This hunting soon gets complicated. For example to add Spring3, you may want the spring release repo, the spring externals repo and the spring milestone repo.

  3. Closely related to number 2 is ensuring you have the correct version of an artifact. I have been burned several times by including the wrong versions of dependent artifacts for a given artifact. For example the wrong version of the servlet/jsp/jstl apis for spring3, or the wrong version of persistence / annotation apis for hibernate. The repositories are filled with many versions, some with confusing names like productx-3.ga, productx-3-rc1, productx-3-SNAPSHOT, productx-3-cr, product-3-beta, etc. Some of these are obvious (rc= release candidate), but it can be confusing trying to determine the order of these versions.

  4. Finally, the issue of the type a dependency. I probably just don’t understand this well enough, but many repo artifacts are of type “pom” not “jar”. Several times i have added a dependency jar to my project only to find out at build time that the repo jar does not actually exist (example is org.hibernate ejb3-persistence in the jboss repo).

With some experimenting, I can usually get a build to work, but is dependency management in general this complicated? I still prefer this approach to manually adding jar files to my project, but I would be interested to learn how to improve my maven dependency management skills.

Answer

Can’t answer all parts of the question, but about some of them:

  • Some transitive dependecies are marked optional, so people who don’t need these features wouldn’t download them, but people, who need, have to set them explicitly in their poms.

  • Maven Central repository contains only releases. Therefore, it doesn’t contain Hibernate 3.5 (which is beta) as well as it hadn’t contained Spring 3 until it was released (by the way, you don’t need to specify special Spring repository for Spring 3 any more – release is already in Maven Central)

  • slf4j is a very special kind of dependency – its runtime behavior depends on which implementation of slf4j you use. Therefore, to control its behavior you have to specify slf4j implementation explicitly

  • About management skills: to get useful information for maintaining your pom.xml you can use mvn dependency:tree (especially with -Dverbose=true) and mvn dependency:analyze. It can be also useful to check pom file of your dependency to look for optional dependencies.

Categories
discuss

Creating RSA keys from known parameters in Java

I’m working on implementing Bing Cashback. In order to verify an incoming request from Bing as valid they provide a signature. The signature is a 160-bit SHA-1 hash of the url encrypted using RSA.

Microsoft provides the RSA “public key”, modulus and exponent, with which I’m supposed to decrypt the hash.

Is there a way to create the Java key objects needed to decrypt the hash as Microsoft says?

Everything I can find creates RSA key pairs automatically since that’s how RSA is supposed to work. I’d really like to use the Java objects if at all possible since that’s obviously more reliable than a hand coded solution.

The example code they’ve provided is in .NET and uses a .NET library function to verify the hash. Specifically RSACryptoServiceProvider.VerifyHash()

Answer

RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey pub = factory.generatePublic(spec);
Signature verifier = Signature.getInstance("SHA1withRSA");
verifier.initVerify(pub);
verifier.update(url.getBytes("UTF-8")); // Or whatever interface specifies.
boolean okay = verifier.verify(signature);
Categories
discuss

Implementing In App purchases in Android?

Edit: Android now supports in-app billing!

Original question:

It looks like Android won’t natively support in-app purchases for a while, and when it does there might be a huge user base with devices that don’t support them.

What’s the best way to implement iPhone-like (additional content or services) in-app purchases in Android using the Android Market if possible?

The solution should consider in particular:

  • For all kinds of in-app purchases: Android Market’s 24-hour cancellation policy
  • For consumables/non-consumables: storage of additional content (ie: use precious application memory to avoid piracy, or use SD card to avoid bloating application memory)

Thanks!

Answer

This has changed as of today! There is now an example on the Android Developer site here: https://developer.android.com/google/play/billing/billing_overview

Categories
discuss

Permission denied: File created in …/files

I’m creating a file in data/data/myPackage/files/ :

file = new File( getFilesDir() + "/file.txt");

I’m absolutely sure that the file is created.
Right after its creation I call:

file.canWrite();

and the result is true.

When I try to use that file
I get: “Permission Denied”.
In Eclipse, in DDMS, this file permissions are like:

-rw-------

Can anyone help here?

Thanks

Answer

Easiest way to open the file (and create it at the same time) would be to use the openFileOutput("file.txt", MODE_PRIVATE) method.

This ensures that the file is only readable by your application (the same as you’ve have at the moment), plus it returns you a FileOutputStream so you can start writing to it.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..