Coding

Ever since I advanced beyond the “hello worlds” of Java, I had never stopped coding. This section is for documenting code usages that I had employed while solving the technological problems that I had encountered.

How to loop through lines of text in a text file from a shell script dynamically

There are times when we need to dynamically run shell commands based on the items listed in a text file.

The shell script that I ran in my bash shell does the following:
for each line separated by newline and not preceded by a ‘#’ character, print the line to console.

In order to build the shell script, I would need the following code pieces:

  • A way to remove/delete empty lines or leading and trailing whitespace in a text file
  • A way to loop through the text file, line by line
  • A way to check if the first character of the string

This post documents what I had ran in my bash shell (in Ubuntu 14.0.4) to loop through items separated by newlines in a text file.

How to save and load environment objects in R

There was a need for me to build a prediction model in R and a Shiny app to allow users to get predictions out of my model. As the building of a prediction model take quite a while, it is not feasible for my shiny app to build the prediction model on demand.

There must be a way for my Shiny app to load my prediction model only once for fulfilling prediction requests from the users. As with many other programming languages, there are mechanisms in R that allow me to save my environment objects in one session and load them back in another session. This post documents how I can save and load environment objects in R.

How to upload a file via a HTTP multipart request in Java without using any external libraries

There was this situation when there was a need for my applet to send some log files (generated by some desktop application) on the remote clients.

To keep my applet lean, I chose to implement this file upload function by sending a HTTP multipart request when my applet loads on the remote client’s browser. Policies were in place to ensure that my applet was able to read the log files and send them back to a web server which will collect the log files.

This post documents how I can upload a file by sending a HTTP multipart request in Java without using any external libraries. For the sake of brevity, I used the server endpoint that I had discussed earlier to accept the file from the codes that will be mentioned in this post.

How to raise and consume custom events in C#

When something happens asynchronously, the events mechanisms in C# is a good way for different components of our C# programs to notify one another about it.

For example, when the report generation component had the finance report ready, it can raise an event to components that had registered interest in that finance report.

This post documents how we can raise and consume custom events in C#.

How to create a thumbnail of an image in Java without using external libraries

I had a situation when I wanted to display thumbnails of the images that my user had uploaded to my web application. A straightforward way could be to return the images in its entirety and use css techniques to scale down the image into a thumbnail. However, this is costly for my users who mainly access my web application from mobile devices.

One strategy for optimizing the web experiences of my users would be to generate the thumbnails at the server end so as to reduce the sizes of the HTTP responses that my server sends back to the mobile devices. And to reduce bloat from external libraries, I decided to use the facilities provided from Java standard libraries.

This post documents how I create a thumbnail from an image without using external Java libraries. For the sake of brevity, let’s assume that we had downloaded an image via HTTP GET from a web server and saved that image to the path /images/sample.jpg.

How to use the aggregate function in R to perform computation on measures that are categorized by some variables in a data frame

In today’s fast-paced world, there are tremendous amount of data being recorded periodically. These data may come from sensors which record some measurement along with some categorization such as time and sensor type.

To make sense of such data, most data analysts use the R programming language as a tool. Apart from being free, there are many nice features of R which can help make my data analysis work easier.

This post records the use of the aggregate function in R which I often use to create meaning out of the humongous data which I lay my hands on.

How to send HTTP GET request with Java without using any external libraries

There was this time when I need to build a program to check whether my web server is running fine. To determine that my web server is running fine, I wrote a Java applet that sends a HTTP GET to one of my web resources when the applet runs for the first time. And to keep the applet light, I look into Java in-built features for sending HTTP GETs to my server. This post documents a proof of concept that I did to communicate with my server endpoint via HTTP GET using Java in-built features.

How to send HTTP POST request with Java without using any external libraries

I need to create a Java solution that will be able to send some bulk processing information back to a HTTP server endpoint. The Java solution should be as lean as possible, using as many facilities that Java provides out of the box. Luckily for me, Java do provides the java.net.HttpURLConnection class for me to build my solution. This post details a proof of concept which I did to get to know more about the java.net.HttpURLConnection class.

In this proof of concept, I create a Java console program that will hit the php endpoint which I had created earlier to proof that I can use jQuery to push a dynamically generated file to the web browser based on the user’s input.

How to get information about all the printers that are installed in my windows machine from a C# program via WMI

I need to create a windows service that is able to send some print jobs to one of the installed printers. As this is a windows service, it has to be able to find the printer with a preset printer name. This post details some prototyping codes that I had created to determine whether I am able to locate information of all the printers that are installed in the windows host where my windows service will run.