@Test public void serializeIntAsHexAsciiBytesLeftPaddedWithTwoZeroes() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(20); AsciiUtils.writeIntAsHexAsciiBytes(10, out, 2); byte[] expecteds = new byte[] {'0', 'A'}; assertArrayEquals(expecteds, out.toByteArray()); out.reset(); AsciiUtils.writeIntAsHexAsciiBytes(27, out, 2); expecteds = new byte[] {'1', 'B'}; assertArrayEquals(expecteds, out.toByteArray()); }
@Test public void serializeStringAsAsciiBytes() throws Exception { final String data = "abc123"; final Charset asciiCharset = Charset.forName("US-ASCII"); final int loops = 1000; ByteArrayOutputStream out = new ByteArrayOutputStream(10); byte[] expecteds = new byte[] {'a', 'b', 'c', '1', '2', '3'}; long start = 0; start = System.nanoTime(); for (int i = 0; i < loops; i++) { AsciiUtils.writeStringAsAsciiBytes(data, out); out.flush(); assertArrayEquals(expecteds, out.toByteArray()); out.reset(); } System.out.println( "AsciiUtils#writeStringAsAsciiBytes() elapsed time: " + ((System.nanoTime() - start) / 1000000) + " ms"); start = System.nanoTime(); for (int i = 0; i < loops; i++) { out.write(data.getBytes(asciiCharset)); out.flush(); assertArrayEquals(expecteds, out.toByteArray()); out.reset(); } System.out.println( "String#getBytes() elapsed time: " + ((System.nanoTime() - start) / 1000000) + " ms"); }
@Test public void serializeIntAsAsciiBytesLeftPaddedWithThreeZeroes() throws Exception { final int in = 2; ByteArrayOutputStream out = new ByteArrayOutputStream(20); AsciiUtils.writeIntAsAsciiBytes(in, out, 3); final byte[] expecteds = new byte[] {'0', '0', '2'}; assertArrayEquals(expecteds, out.toByteArray()); }
@Test public void serializeByteArrayAsHexAsciiBytes() throws Exception { final byte[] bytes = new byte[] {(byte) 0x00, (byte) 0x01, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF}; ByteArrayOutputStream out = new ByteArrayOutputStream(20); AsciiUtils.writeByteArrayAsHexAsciiBytes(bytes, out); final byte[] expecteds = new byte[] {'0', '0', '0', '1', 'a', 'b', 'c', 'd', 'e', 'f'}; assertArrayEquals(expecteds, out.toByteArray()); }
/** * Use a name of place to query Yahoo weather apis for weather information. Querying will be run * on a separated thread to accessing Yahoo's apis. When it is completed, a callback will be * fired. See {@link YahooWeatherInfoListener} for detail. * * @param context app's context * @param cityAreaOrLocation A city name, like "Shanghai"; an area name, like "Mountain View"; a * pair of city and country, like "Tokyo, Japan"; a location or view spot, like "Eiffel * Tower"; Yahoo's apis will find a closest position for you. * @param result A {@link WeatherInfo} instance. */ public void queryYahooWeatherByPlaceName( final Context context, final String cityAreaOrLocation, final YahooWeatherInfoListener result) { YahooWeatherLog.d("query yahoo weather by name of place"); mContext = context; if (!NetworkUtils.isConnected(context)) { if (mExceptionListener != null) mExceptionListener.onFailConnection(new Exception("Network is not avaiable")); return; } final String convertedlocation = AsciiUtils.convertNonAscii(cityAreaOrLocation); mWeatherInfoResult = result; final WeatherQueryByPlaceTask task = new WeatherQueryByPlaceTask(); task.execute(new String[] {convertedlocation}); }
@Test public void convertByteArrayToHexString() throws Exception { final byte[] bytes = new byte[] {(byte) 0x00, (byte) 0x01, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF}; assertEquals("0001abcdef", AsciiUtils.byteArrayToHexString(bytes)); }