Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayInputStream; public class Main { /** * read length value from EMV tag bytestream * * source: https://code.google.com/p/javaemvreader/ * * @param stream * @return */ private static int readTagLength(ByteArrayInputStream stream) { // Find LENGTH bytes int length; int tmpLength = stream.read(); if (tmpLength <= 127) { // 0111 1111 // short length form length = tmpLength; } else if (tmpLength == 128) { // 1000 0000 // length identifies indefinite form, will be set later length = tmpLength; } else { // long length form int numberOfLengthOctets = tmpLength & 127; // turn off 8th bit tmpLength = 0; for (int i = 0; i < numberOfLengthOctets; i++) { int nextLengthOctet = stream.read(); tmpLength <<= 8; tmpLength |= nextLengthOctet; } length = tmpLength; } return length; } }