IcyDecoder implements MetadataDecoder { @Override @SuppressWarnings("ByteBufferBackingArray") public Metadata decode(MetadataInputBuffer inputBuffer) { ByteBuffer buffer = Assertions.checkNotNull(inputBuffer.data); @Nullable String icyString = decodeToString(buffer); byte[] icyBytes = new byte[buffer.limit()]; buffer.get(icyBytes); if (icyString == null) { return new Metadata(new IcyInfo(icyBytes, null, null)); } @Nullable String name = null; @Nullable String url = null; int index = 0; Matcher matcher = METADATA_ELEMENT.matcher(icyString); while (matcher.find(index)) { @Nullable String key = Util.toLowerInvariant(matcher.group(1)); @Nullable String value = matcher.group(2); switch (key) { case STREAM_KEY_NAME: name = value; break; case STREAM_KEY_URL: url = value; break; } index = matcher.end(); } return new Metadata(new IcyInfo(icyBytes, name, url)); } IcyDecoder(); @Override @SuppressWarnings("ByteBufferBackingArray") Metadata decode(MetadataInputBuffer inputBuffer);  }
@Test public void decode() { byte[] icyContent = "StreamTitle='test title';StreamURL='test_url';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test title"); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_respectsLimit() { byte[] icyTitle = "StreamTitle='test title';".getBytes(UTF_8); byte[] icyUrl = "StreamURL='test_url';".getBytes(UTF_8); byte[] paddedRawBytes = TestUtil.joinByteArrays(icyTitle, icyUrl); MetadataInputBuffer metadataBuffer = createMetadataInputBuffer(paddedRawBytes); metadataBuffer.data.limit(icyTitle.length); Metadata metadata = decoder.decode(metadataBuffer); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyTitle); assertThat(streamInfo.title).isEqualTo("test title"); assertThat(streamInfo.url).isNull(); }
@Test public void decode_titleOnly() { byte[] icyContent = "StreamTitle='test title';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test title"); assertThat(streamInfo.url).isNull(); }
@Test public void decode_extraTags() { byte[] icyContent = "StreamTitle='test title';StreamURL='test_url';CustomTag|withWeirdSeparator" .getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test title"); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_emptyTitle() { byte[] icyContent = "StreamTitle='';StreamURL='test_url';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEmpty(); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_semiColonInTitle() { byte[] icyContent = "StreamTitle='test; title';StreamURL='test_url';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test; title"); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_quoteInTitle() { byte[] icyContent = "StreamTitle='test' title';StreamURL='test_url';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test' title"); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_lineTerminatorInTitle() { byte[] icyContent = "StreamTitle='test\r\ntitle';StreamURL='test_url';".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("test\r\ntitle"); assertThat(streamInfo.url).isEqualTo("test_url"); }
@Test public void decode_iso885911() { byte[] icyContent = "StreamTitle='tést';StreamURL='tést_url';".getBytes(ISO_8859_1); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isEqualTo("tést"); assertThat(streamInfo.url).isEqualTo("tést_url"); }
@Test public void decode_unrecognisedEncoding() { byte[] icyContent = "StreamTitle='tést';StreamURL='tést_url';".getBytes(UTF_16); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isNull(); assertThat(streamInfo.url).isNull(); }
@Test public void decode_noRecognisedHeaders() { byte[] icyContent = "NotIcyData".getBytes(UTF_8); Metadata metadata = decoder.decode(createMetadataInputBuffer(icyContent)); assertThat(metadata.length()).isEqualTo(1); IcyInfo streamInfo = (IcyInfo) metadata.get(0); assertThat(streamInfo.rawMetadata).isEqualTo(icyContent); assertThat(streamInfo.title).isNull(); assertThat(streamInfo.url).isNull(); }