Why being a programmer ist great or How to get the current windows wallpaper in c#

Being able to make software is great. Not only because you can make a living from it, but also because it helps you in everyday life. Like when you have to manage your 10k+ wallpaper library. I for once always get baited by these “575 awesome wallpapers you absolutely need” posts and download them right away into my library. Every now and then a black sheep sneaks in but i only see it after it pops up on my Desktop (as if i would review all of these 575 wallpapers).

And here it comes together: as a software developer i have the means to write a small piece of code that gets rid of these ugly wallpapers.

So i hacked this snippet that moves the current wallpaper out of its current folder and into a “reject” folder.
The current wallpaper path can be found in a registry entry “TranscodedImageCache” in “HKCU\Control Panel\Desktop” (at least in Windows 8.1). It’s encoded with unicode though and has to be cleaned a little bit.

Continue reading


String replace with callback in Java (like in JavaScript)

When you use JavaScript a lot you are more or less used to the callback-hell, but i hope you love the benefits of passing functions around as much as i do.

For example when replacing a portion of a string:

"test6test12test".replace(/\d+/g, function(str){
  return parseInt(str) * 2;
});

When doing such things in Java it can be a bit of a hassle, but Lambda-Expressions to the rescue!


Convert a ByteArrayOutputStream into a ByteArrayInputStream

Often while programming you find yourself glueing pre-existing code fragments together. This works out sometimes but even more often it is rather annoying. These days i had to encrypt a pdf and do some other stuff with it in java and therefore put together some libraries i found. One of these libraries offered a method to encrypt the pdf and write the result into a ByteArrayOutputStream. Later then i had to push the encrypted pdf into another method that needed it as a ByteArrayInputStream. First i simply copied the content of the ByteArrayOutputStream as byte array into the ByteArrayInputStream, but this didn’t satisfy me since i knew that the ByteArrayInputStream just needed to be initialized with the internal buffer of the ByteArrayOutputStream. Since this internal buffer is hidden from outside i made up the ByteArrayInOutStream which is a ByteArrayOutputStream that has an additional method that returns a ByteArrayInputStream which is initialized with the internal buffer of the ByteArrayOutputStream. This allows for a instant ‘conversion’ that does not use up more memory than required.

Continue reading


A broad look into the mobile network future

When i went through my files i stumbled over a paper i made at my university and thought maybe someone could find interest in it.

The paper features a summary of the history of mobile networks and tries to make a forecast about which technologies are most likely to play a role in next generation mobile networks.

I am going to quote the abstract here:

The ubiquitous access to information through mobile networks
changed our lives. These mobile networks are in a
state of permanent evolution. In this paper we identify the
most probable drivers for future mobile networks. We provide
an overview over the most successful commercial mobile
networks and point out the technologies used in these mobile
networks that will most likely be reused in future networks.
We discuss the diculties and requirements for future mobile
networks that come with the deployment of billions of
autonomous Machine-to-Machine devices. While some solutions
have been proposed to deal with these Machine-to-
Machine requirements many problems remain unsolved.

Continue reading


@Asynchronous does not work from inside the same EJB

I recently needed to start a method asynchronously on the server start up.

I came up with this code (pseudocode):

@Singleton
@Startup
public StartupEJB {
   @PostConstruct
   private void postConstruct() {
      worker();
   }
 
   @Asynchronous
   public void worker() {
      // Do something heavy
   }
}

This approach failed, the worker() method was not executed asynchronously.

I then found out that the Application Server (in my case JBoss) calls the method directly when it is located inside the same EJB: source.

The solution was to use a seperate EJB that launched the worker Thread:

@Stateless
public class ExecutorBean {
 
   @Asynchronous
   public void execute(Runnable command) {
      command.run();
   }
}

and

@Singleton
@Startup
public StartupEJB {
   @EJB
   ExecutorBean executorBean;
 
   @PostConstruct
   private void postConstruct() {
      worker();
   }
 
   public void worker() {
      executorBean.execute(new Runnable() {
         @Override
         public void run() {
            // Do something heavy
         }        
      });      
   }
}

Beautiful CSS3 loading animation overlay with custom message

I recently stumbled over a beautiful loading animation that is completely done in CSS3 (link):

Loader Animation GIF

All i had to do was fixing a small bug (link), adding some JavaScript and adding an semi-transparent overlay, so all credits go to Hugo Giraudel.

I centered the loading animation div and added a field to display a text message.

To allow for easy display and hiding i cooked up some JavaScript that shows the overlay and puts the text message (or a default message) in the right place.

Demo:
link
Continue reading


Fetch all Languages of the World (ISO 639) as Java Array

Recently i needed a Java Array of all Languages, containing some unique id.

I found that ISO 639 is exactly what i needed. The list is offered by the Library of Congress in a CSV like Format (link).

So i parsed this data and thought that maybe someone can use this, either the Java Array or the Code as Example for parsing external data with JavaScript.

 Demo:

To fetch the list i used Ben Almans JSONP Proxy and Ben Nadels CSVToArray Function.
Continue reading


Remove certain item or clear whole OmniFaces cache

The Omnifaces o:cache Component is a useful Tool when trying to speed up you jsf powered website.

But in some situations you need to remove a certain item from the cache or you want to clear the whole cache, while there seems to be a way (Example 4) to remove single items by Key we couldn’t find a ‘official’ way to clear the whole cache.

 

Here is our Solution to the Problem:

public static boolean removeOmniCacheItem(String key) {    	
	Map<string , Object> applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
 
	if (applicationMap.containsKey(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME)) {
		synchronized (DefaultCacheProvider.class) {
			if (applicationMap.containsKey(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME)) {
				((Cache)applicationMap.get(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME)).remove(key);
				return true;
			}				
		}
	}
 
	return false;
}
 
public static boolean clearOmniCache() {    	
	Map</string><string , Object> applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
 
	if (applicationMap.containsKey(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME)) {
		synchronized (DefaultCacheProvider.class) {
			if (applicationMap.containsKey(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME)) {
				applicationMap.remove(DefaultCacheProvider.DEFAULT_CACHE_PARAM_NAME);
				return true;
			}				
		}
	}
 
	return false;
}
</string>

Continue reading