The Generated Codes

Once you save your ASN.1 file, the ASN.1 compiler will automatically compile your ASN.1 specification to the target programing languange codes. Each Java class contains data type definition, metadata type (constant TYPE) and converter (constant CONVERTER), and possibly encoding/decoding methods, clone methods, equals methods, and print methods.

Below are code snippets of the generated codes from The MyHTTP Example.

GetRequest.java

/*
 * Generated by ASN.1 Java Compiler (https://www.asnlab.org/)
 * From ASN.1 module "MyHTTP"
 */
package MyHTTP;

import org.asnlab.asndt.runtime.conv.*;
import org.asnlab.asndt.runtime.type.*;
import java.io.*;
import java.util.Date;

public class GetRequest {

	public Boolean header_only;
	public Boolean lock;
	public AcceptTypes accept_types;
	public String url;
	public Date timestamp;

	public void ber_encode(OutputStream out) throws IOException {
		TYPE.encode(this, EncodingRules.BASIC_ENCODING_RULES, CONVERTER, out);
	}

	public static GetRequest ber_decode(InputStream in) throws IOException {
		return (GetRequest)TYPE.decode(in, EncodingRules.BASIC_ENCODING_RULES, CONVERTER);
	}


	public final static AsnType TYPE = (AsnType) MyHTTP.types.get(4194496);

	public final static GetRequestConverter CONVERTER;

	static {
		CONVERTER = new GetRequestConverter();
		AsnConverter header_onlyConverter = BooleanConverter.INSTANCE;
		AsnConverter lockConverter = BooleanConverter.INSTANCE;
		AsnConverter accept_typesConverter = AcceptTypes.CONVERTER;
		AsnConverter urlConverter = StringConverter.INSTANCE;
		AsnConverter timestampConverter = DateConverter.INSTANCE;
		CONVERTER.setComponentConverters(new AsnConverter[] { header_onlyConverter, lockConverter, accept_typesConverter, urlConverter, timestampConverter });
	}

	private static class GetRequestConverter extends CompositeConverter {

		private static final int header_onlyIndex = 0;
		private static final int lockIndex = 1;
		private static final int accept_typesIndex = 2;
		private static final int urlIndex = 3;
		private static final int timestampIndex = 4;

		public GetRequestConverter() {
			super();
		}

		public Object createObject() {
			return new GetRequest();
		}

		public Object getComponentObject(Object object, int index) {
			GetRequest model = (GetRequest) object;
			switch(index){
				case header_onlyIndex:
					return model.header_only;
				case lockIndex:
					return model.lock;
				case accept_typesIndex:
					return model.accept_types;
				case urlIndex:
					return model.url;
				case timestampIndex:
					return model.timestamp;
			}
			return null;
		}

		public void setComponentObject(Object object, int index, Object componentObject) {
			GetRequest model = (GetRequest) object;
			switch (index) {
				case header_onlyIndex:
					model.header_only = (Boolean) componentObject;
					break;
				case lockIndex:
					model.lock = (Boolean) componentObject;
					break;
				case accept_typesIndex:
					model.accept_types = (AcceptTypes) componentObject;
					break;
				case urlIndex:
					model.url = (String) componentObject;
					break;
				case timestampIndex:
					model.timestamp = (Date) componentObject;
					break;
			}
		}
	}
}

AcceptTypes.java

/*
 * Generated by ASN.1 Java Compiler (https://www.asnlab.org/)
 * From ASN.1 module "MyHTTP"
 */
package MyHTTP;

import org.asnlab.asndt.runtime.conv.*;
import org.asnlab.asndt.runtime.type.*;
import java.io.*;
import java.util.Vector;

public class AcceptTypes {

	public Standards standards;	/* OPTIONAL */
	public Vector others;	/* OPTIONAL */

	public void ber_encode(OutputStream out) throws IOException {
		TYPE.encode(this, EncodingRules.BASIC_ENCODING_RULES, CONVERTER, out);
	}

	public static AcceptTypes ber_decode(InputStream in) throws IOException {
		return (AcceptTypes)TYPE.decode(in, EncodingRules.BASIC_ENCODING_RULES, CONVERTER);
	}


	public final static AsnType TYPE = (AsnType) MyHTTP.types.get(4194368);

	public final static AcceptTypesConverter CONVERTER;

	static {
		CONVERTER = new AcceptTypesConverter();
		AsnConverter standardsConverter = Standards.CONVERTER;
		AsnConverter othersConverter = new ListConverter(StringConverter.INSTANCE);
		CONVERTER.setComponentConverters(new AsnConverter[] { standardsConverter, othersConverter });
	}

	private static class AcceptTypesConverter extends CompositeConverter {

		private static final int standardsIndex = 0;
		private static final int othersIndex = 1;

		public AcceptTypesConverter() {
			super();
		}

		public Object createObject() {
			return new AcceptTypes();
		}

		public Object getComponentObject(Object object, int index) {
			AcceptTypes model = (AcceptTypes) object;
			switch(index){
				case standardsIndex:
					return model.standards;
				case othersIndex:
					return model.others;
			}
			return null;
		}

		public void setComponentObject(Object object, int index, Object componentObject) {
			AcceptTypes model = (AcceptTypes) object;
			switch (index) {
				case standardsIndex:
					model.standards = (Standards) componentObject;
					break;
				case othersIndex:
					model.others = (Vector) componentObject;
					break;
			}
		}
	}
}

The generations of ***_encode()/***_decode() methods, like ber_encode(), are just for the developer's convenience. In fact, those methods can be removed and the generic encode()/decode() methods from the ASN.1 Java Runtime Library can be used(as demonstrated by the generated methods).

To remove the ***_encode()/***_decode() methods, goto the ASN.1 to Java Compiler preference page at Windows > Preferences > ASN.1 > ASN.1 to Java Compiler, in the Encoding Rules section, uncheck all encoding rules, and then resave or manually invoke the building of the ASN.1 project.