/* * Copyright 2015 Constant Innovations Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.constantinnovationsinc.livemultimedia.utilities; import android.util.Log; import java.net.InetAddress; import java.io.*; import java.net.*; import java.util.*; public class DeviceNetwork { private static final String TAG = DeviceNetwork.class.getCanonicalName(); /** * Convert byte array to hex string * @param bytes the byte to convert * @return HexString */ public static String bytesToHex(byte[] bytes) { StringBuilder sbuf = new StringBuilder(); for(int idx: bytes) { int intVal = bytes[idx] & 0xff; if (intVal < 0x10) sbuf.append("0"); sbuf.append(Integer.toHexString(intVal).toUpperCase()); } return sbuf.toString(); } /** * Get utf8 byte array. * @param str to convert * @return array of NULL if error was found */ public static byte[] getUTF8Bytes(String str) { try { return str.getBytes("UTF-8"); } catch (Exception ex) { return null; } } /** * Load UTF8withBOM or any ansi text file. * @param filename as string * @return String * @throws java.io.IOException */ public static String loadFileAsString(String filename) throws java.io.IOException { final int BUFLEN=1024; BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN); byte[] bytes = new byte[BUFLEN]; boolean isUTF8=false; int read,count=0; while((read=is.read(bytes)) != -1) { if (count==0 && bytes[0]==(byte)0xEF && bytes[1]==(byte)0xBB && bytes[2]==(byte)0xBF ) { isUTF8=true; baos.write(bytes, 3, read-3); // drop UTF8 bom marker } else { baos.write(bytes, 0, read); } count+=read; } return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray()); } finally { try { is.close(); } catch(Exception ex){ Log.e(TAG, ex.getMessage()); } } } /** * Returns MAC address of the given interface name. * @param interfaceName eth0, wlan0 or NULL=use first interface * @return mac address or empty string */ public static String getMACAddress(String interfaceName) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } byte[] mac = intf.getHardwareAddress(); if (mac == null) return ""; StringBuilder buf = new StringBuilder(); for (int idx : mac) { buf.append(String.format("%02X:", mac[idx])); } if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); return buf.toString(); } } catch (SocketException ex) { Log.e(TAG, ex.getMessage()); } return ""; } public static String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } }