/* Consolidation of a number of operations related to network programming. All functions here are static. To use, put Netutils.java in your directory, javac Netutils.java, then call functions within your program in the form Netutils.encodeint(...); */ import java.io.*; public class Netutils { /////////////////////// byte-level translation, from binaryops.java // encode signed 32 bit int into byte[] buffer at offset public static void encodeint(int x, byte[] buf, int offset) { if (offset<0 || buf.length-4 < offset) throw new RuntimeException("invalid offset for encodeint: "+offset); buf[offset] = (byte) ((x & 0xff000000)>>>24); buf[offset+1] = (byte) ((x & 0x00ff0000)>>>16); buf[offset+2] = (byte) ((x & 0x0000ff00)>>>8); buf[offset+3] = (byte) (x & 0x000000ff); }//encodeint // extract signed 32 bit int from byte[] buffer at offset public static int extractint(byte[] buf, int offset) { if (offset<0 || buf.length-4 < offset) throw new RuntimeException("invalid offset for extractint: "+offset); int ax = buf[offset]<<24; // shift left, also sets signed bit ax = ax | (0x00ff0000 & (buf[offset+1]<<16)); ax = ax | (0x0000ff00 & (buf[offset+2]<<8)); ax = ax | (0x000000ff & buf[offset+3]); return ax; } // 64 bit versions public static void encodelong(long x, byte[] buf, int offset) { if (offset<0 || buf.length-8 < offset) throw new RuntimeException("invalid offset for encodeint: "+offset); long mask = 0xff<<56; //0xff00 0000 0000 0000; for(int i=0;i<8;i++) { buf[offset+i] = (byte) ((x & mask)>>>(56-8*i)); mask = mask>>>8; } }//encodelong public static long extractlong(byte[] buf, int offset) { if (offset<0 || buf.length-8 < offset) throw new RuntimeException("invalid offset for extractint: "+offset); long mask = 0xff<<56; //0xff00000000000000; long ax = buf[offset]<<56; for(int i=1;i<8;i++) { mask = mask>>>8; ax = ax | (mask & (buf[offset+i]<<(56-8*i))); } return ax; }//extractlong // Inverts the byte ordering of int: although java uses network byte ordering, // you may still need this when communicating with a program that's not // written correctly, such as a C program that didn't call htonl. public static int invertByteOrder(int x) { byte[] buf = new byte[4]; byte[] revbuf = new byte[4]; encodeint(x,buf,0); for(int i=0;i<4;i++) revbuf[i] = buf[3-i]; // invert the buffer return extractint(revbuf,0); } // you should be able to write similar procedures based on this example // encode 32 bit int containing unsigned short as 2 bytes at buf[offset]: public static void encodeu16(int x, byte[] buf, int offset) { if (x<0 || x>65535 || offset<0 || buf.length-2>8); } // extract 2 bytes from buf[offset] as an unsigned int public static int extractu16(byte[] buf, int offset) { if (offset<0 || buf.length-2 < offset) throw new RuntimeException("invalid offset for extractu16: "+offset); int ax = 0x000000ff & buf[offset+1]; // buf[offset] first convert to int ax = ax | (0x0000ff00 & (buf[offset]<<8)); return ax; } public static void testbinops(String[] av) // used to be a main { byte[] buf = new byte[6]; int n = Integer.parseInt(av[0]); encodeint(n,buf,0); System.out.println(extractint(buf,0)); encodeu16(n,buf,4); System.out.println(extractu16(buf,4)); } // extracts bytes from buffer, starting at index i and of max length l, // or until zero encountered, or until end of buffer. public static String bufToString(byte[] buffer, int i, int l) { String s = ""; while (i>=0 && (i0) { s = s+(char)buffer[i++]; l--; } return s; } // The next function will take a string and write to a buffer starting // at the given index i, and terminating the buffer with a zero. // The function will return index of terminating zero. public static int StringToBuf(byte[] buffer, String s, int i) { int j = 0; while (i>=0 && i0) { dst[di++] = src[si++]; l--; } } // checks if two byte[]'s are equal public static boolean bufeq(byte[] A, byte[] B) // buffer equality { if (A.length != B.length) return false; boolean ax = true; for (int i=0;i>>0)); byte[] buf = new byte[8]; encodelong(x,buf,0); long y = extractlong(buf,0); System.out.println(y); } */ }// Netutils