Thursday, November 12, 2015

StringTokenizer to split using multiple tokens in java

When trying to split a string over multiple tokens, the best thing available was thought to be the split. That way if we have to split over a string multiple times then we can split and then split over the splits, but what if there is more the be split over the parts, then we will have to split again.

Let me explain with an example. Say we have a URL that needs to be parsed.

URL: http://localhost/system/config/file/action/updateLogo?fileName=largetc.jpg&text=Company%20Logo
W

So if we have to read all the the path parameters and then also read the arguments then we have to do a split over the '/' parameter as below:

String[] urlTokens = urlFullPath.split("/");

Now let us say that we want to read the path arguments that are passed in the URL. Then we will have to do multiple splits over the path string to reach the desired variables. The below is one of the ways that it can be achieved.

String[] urlTokens = urlFullPath.split("/");
for (String urlPath : urlTokens) {
if(urlPath.contains("?")){
String[] argTokens = urlPath.split("\\?");
String[] argsParts = argTokens[1].split("&");
for (String args : argsParts) {
System.out.println("Args: " + args);
}
}
}

The output of the above piece of Java code would look something like:
Args: fileName=largetc.jpg
Args: text=Company%20Logo
That, as you can see, are a a lot of splits. Is there any better way. Probably there are a dozen ones. Here is one with StringTokenizer for easier manipulation.

There are two ways to specify a delimiter for a StringTokenizer object.

  1. In the StringTokenizer constructor pass the delimiter when initializing the object
  2. In nextToken() method pass the delimiter you are looking for next at runtime

Using StringTokenizer constructor

In the constructor one can pass the list of all the characters on which the string has to be split and accessed as:
StringTokenizer stringTokenizer = new StringTokenizer(theStringToParse, "/?&");
And then iterate over the tokens via a while loop as below:
while (stringTokenizer.hasMoreTokens())
System.out.println(stringTokenizer.nextToken());
Using nextToken method

To use by runtime parameter to the nextToken method, below is one of the ways:
while (stringTokenizer.hasMoreTokens())
System.out.println(stringTokenizer.nextToken("/"));
The token above can be changed as needed. One thing that needs to be kept in mind is that once a move to nextToken is done then the previous token is lost and one cannot do a trace back.

Split that URL
Combining all of the above here's a piece of code that can be used to extract the arguments passed in a URL as a key value pair.

StringTokenizer stringTokenizer = new StringTokenizer(theStringToParse);
// iterate through tokens of path parameters
while (stringTokenizer.hasMoreTokens()) {
String partOfToken = stringTokenizer.nextToken("?");
if (partOfToken.contains("=")) {
StringTokenizer tokenizeAgain = new StringTokenizer(partOfToken, "&");
while (tokenizeAgain.hasMoreTokens()) {
String argument = tokenizeAgain.nextToken();
String[] keyValueOfArgument = argument.split("=");
System.out.println("Key: " + keyValueOfArgument[0] + " and Value: " + keyValueOfArgument[1]);
}
}
}
The output of the above piece of code, when integrated with all the fans and flurries needed to execute would be, assuming the URL given at the beginning is the string to be parsed:
Key: fileName and Value: largetc.jpg
Key: text and Value: Company%20Logo 

Saturday, September 26, 2015

Is the net really neutral?


The recent debate on net-neutrality has become a major issue, so much that even Rahul Gandhi spoke on this topic. Further it has made it clear to general public that there are many intermediate players before any of the content from the internet is delivered to the end user. All these players are there with a business of their own that serves their personal interest.

A part of this not-so-simple structure of the internet is the government. Hence the Federal Communications Commission (FCC) was created to regulate interstate communications by radio, television, wire, satellite, and cable, as a watchdog of USA’s telecommunication industry. TRAI is the Indian counterpart working to regulate telecom services and tariffs in India.

There has been several comparisons of the internet to the electricity company for the purpose of debate on net-neutrality. This comparison is not exactly appropriate. Electricity does not have an inherent meaning within the flow of charged particles that inform as to what type of electric instrument is being used. For example, if the consumer is using a TV, or a fridge, or any other such specific electric appliance cannot be known by looking at the rate of flow of the charged particles. Other comparison are to the swing, where the swing movement is being controlled based on the payment made for the use of its functionality and to the buying fruits from a street vendor. These comparisons are rather more simplistic ignoring the more complex nuances associated with the internet.

A more appropriate comparison on this would be that of a postman. This is more appropriate because of two reasons. Currently the service providers work using multiplexing of data from different users over a single connecting. This is because the speed of underlying connections that are laid using optical communication systems are at rates of Giga-Bytes that are much higher than what an individual can use. For example the network speeds can go up to 100 GB/sec while the consumer will not be able to consume data at such a speed. Hence instead of giving the whole underlying network for one user at any specific time, the network providers put together data from different users in a single package(data over a network is sent in packets of data, one at a time) for transmission. Hence it is like a postman who is carrying letters in a bag and instead of carrying one person’s letters at a time the letters from multiple users are put in a bag and carried along at the same time.

The second reason that this analogy is closer is because the independent letters have the inherent information in it informing of the source and the destination and so allowing for a discrimination based on the meta-data, considering the content of the mail as the actual data. This model tries to model the complexity on a smaller scale.

Another argument being put forth is that of Internet Fast Lanes. This would allow the telecom operators to give preferential speed to companies with deep pockets while throttling speeds of others. This was shown by the comparisons with swing given previously in this article. But the internet already has a “fast lanes” because of CDNs.  These are distributed system of servers that provide content to the end-users with high performance and availability. In simple language when someone logs in to say facebook or gmail it is not guaranteed that all the data of their wall is coming from facebook server. Most of it may be coming from a CDN that is lying geographically very close to the user. That way the speed of delivery can be increased for quick response to the users. Akamai Technologies is the most popular CDN that delivers content to several privileged companies. As soon as a user opens or logs in to many websites one can see content being pulled from Akamai.com that will be displayed in a small pop-up at the bottom of the browser screen. Another technique being used are the peering connections where the content providers have direct connections to ISPs and run dedicated servers deep inside these ISPs to deliver content faster. Clearly these “fast lanes” are available to only those with deep pockets giving them an edge over the others with lighter purses.
In one of our previous article we pointed out that the internet is controlled by the gateway of the internet, i.e. the search engines. We had argued that if something is hidden in the 100th page of the search result, even if it is the most relevant accessible information, it is as good as non-existent and inaccessible. So the top positions for specific keywords if paid and occupied by companies, however irrelevant to it, then we can clearly see how deep pockets can tilt the internet to be not so neutral. So let’s face it.

The debate over net neutrality is not a recent one. It started in 2003 when the Columbia University media law professor Tim Wu coined the term. What we are still missing is a means of keeping the ISPs in check, else these debates will resurface in a newer form and at different levels. These debates also raise serious concerns that internet service providers are growing too powerful to influence a policy change. One way of exercising control is through common carrier law. These laws are necessary to define the framework in which the internet service providing companies have to function. There will definitely be opposition if it clamps down the current freedom being enjoyed by these companies hence it has to be done impartially by a third party including public opinion in their decisions.

The internet.org by facebook is being touted for being against net neutrality while Mark Zuckerberg defends it as being a plan to bring the internet to everyone. After the uproar, majorly in India, Zuckerberg expanded internet.org so as to allow developers to provide an app through Internet.org. Their argument is that the debate was over consumer choice and developer choice and they seem to have addressed them via their improved platform. Currently they are offering several projects that can work via their platform and an option to build more. Hence believingly the argument has moved further from the debate of providing lopsided access to the internet. Now the discussion has to be around as to which services and websites are or can be provided access via internet.org and who is to decide this.

Let us face it. The internet is not as much neutral after all and people with deep pockets will keep working to further their interests in further making work more in their favour. Let us get back to our analogy of the postman. The postal service is everyone’s necessary. There will soon be a day when the internet will also be such a necessary service, just like railroad, bus services or airlines. Hence what is being proposed is to regulate the telecommunications as common carriers.

Of course there will be arguments against the government gaining control on the network of networks arguing that it is the freedom that has provided incentive to the network providers to build the whole infrastructure that currently delivers internet. Another argument is that if the government holds the control then the whole process will be slowed down while these type of services need a faster response. The need of the hour is to further the debate on the common carrier and take a stand for the common good of the masses at large along with appropriate consideration to the involved parties.



Published Article Reference: http://thecompanion.in/is-net-really-neutral/