Skip to content

Commit

Permalink
Merge pull request google#836 from cco3/colfindbugs
Browse files Browse the repository at this point in the history
Fix findbugs errors in collections library
  • Loading branch information
cco3 authored Sep 20, 2016
2 parents 3430aac + 8259eb0 commit d51cbde
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
Expand Down Expand Up @@ -75,7 +76,8 @@ private EddystoneBeacon(byte flags, byte txPower, String url) {
*/
public static String getFatBeaconTitle(byte[] serviceData) {
if (serviceData.length > 2) {
String title = new String(Arrays.copyOfRange(serviceData, 3, serviceData.length)).trim();
byte[] bytes = Arrays.copyOfRange(serviceData, 3, serviceData.length);
String title = new String(bytes, Charset.forName("UTF-8")).trim();
return title.indexOf('\uFFFD') == -1 ? title : "";
}
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

import org.junit.Test;

import java.io.UnsupportedEncodingException;

/**
* Tests for the EddystoneBeacon class.
*/
public class EddystoneBeaconTest {

@Test
public void getFatBeaconTitleTest() {
public void getFatBeaconTitleTest() throws UnsupportedEncodingException {
// Array length failure
assertEquals("", EddystoneBeacon.getFatBeaconTitle(new byte[]{}));
assertEquals("", EddystoneBeacon.getFatBeaconTitle(new byte[]{0x01}));
Expand All @@ -36,9 +38,10 @@ public void getFatBeaconTitleTest() {

// Valid title
String title = "title";
int length = title.getBytes().length;
byte[] titleBytes = title.getBytes("UTF-8");
int length = titleBytes.length;
byte[] serviceData = new byte[length + 3];
System.arraycopy(title.getBytes(), 0, serviceData, 3, length);
System.arraycopy(titleBytes, 0, serviceData, 3, length);
serviceData[0] = 0x10;
serviceData[1] = 0x00;
serviceData[2] = 0x0e;
Expand Down

0 comments on commit d51cbde

Please sign in to comment.