DataSchemeDataSource extends BaseDataSource { @Override public long open(DataSpec dataSpec) throws IOException { transferInitializing(dataSpec); this.dataSpec = dataSpec; readPosition = (int) dataSpec.position; Uri uri = dataSpec.uri; String scheme = uri.getScheme(); if (!SCHEME_DATA.equals(scheme)) { throw new ParserException("Unsupported scheme: " + scheme); } String[] uriParts = Util.split(uri.getSchemeSpecificPart(), ","); if (uriParts.length != 2) { throw new ParserException("Unexpected URI format: " + uri); } String dataString = uriParts[1]; if (uriParts[0].contains(";base64")) { try { data = Base64.decode(dataString, 0); } catch (IllegalArgumentException e) { throw new ParserException("Error while parsing Base64 encoded string: " + dataString, e); } } else { data = Util.getUtf8Bytes(URLDecoder.decode(dataString, C.ASCII_NAME)); } endPosition = dataSpec.length != C.LENGTH_UNSET ? (int) dataSpec.length + readPosition : data.length; if (endPosition > data.length || readPosition > endPosition) { data = null; throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); } transferStarted(dataSpec); return (long) endPosition - readPosition; } @SuppressWarnings("nullness:initialization.fields.uninitialized")  DataSchemeDataSource(); @Override long open(DataSpec dataSpec); @Override int read(byte[] buffer, int offset, int readLength); @Override @Nullable Uri getUri(); @Override void close(); static final String SCHEME_DATA; }
@Test public void testInvalidStartPositionRequest() throws IOException { try { schemeDataDataSource.open( buildDataSpec(DATA_SCHEME_URI, 108, C.LENGTH_UNSET)); fail(); } catch (DataSourceException e) { assertThat(e.reason).isEqualTo(DataSourceException.POSITION_OUT_OF_RANGE); } }
@Test public void testRangeExceedingResourceLengthRequest() throws IOException { try { schemeDataDataSource.open( buildDataSpec(DATA_SCHEME_URI, 97, 11)); fail(); } catch (DataSourceException e) { assertThat(e.reason).isEqualTo(DataSourceException.POSITION_OUT_OF_RANGE); } }
@Test public void testIncorrectScheme() { try { schemeDataDataSource.open(buildDataSpec("http: fail(); } catch (IOException e) { } }
@Test public void testMalformedData() { try { schemeDataDataSource.open(buildDataSpec("data:text/plain;base64,,This%20is%20Content")); fail(); } catch (IOException e) { } try { schemeDataDataSource.open(buildDataSpec("data:text/plain;base64,IncorrectPadding==")); fail(); } catch (IOException e) { } }