/** * {@inheritDoc} * * @see org.modeshape.graph.property.Binary#getBytes() */ public byte[] getBytes() { try { return IoUtil.readBytes(file); } catch (IOException e) { throw new IoException(e); } }
public InMemoryTestBinary( InputStream is ) throws IOException { this(IoUtil.readBytes(is)); }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.BinaryFactory#create(java.io.File) */ public Binary create( File file ) throws ValueFormatException, IoException { if (file == null) return null; if (!file.canRead()) return null; if (!file.isFile()) return null; try { byte[] value = IoUtil.readBytes(file); return create(value); } catch (IOException err) { throw new IoException(GraphI18n.errorConvertingIo.text(file, Binary.class.getSimpleName()), err); } }
/** * {@inheritDoc} */ public Binary create( InputStream stream, long approximateLength ) throws IoException { if (stream == null) return null; try { byte[] value = IoUtil.readBytes(stream); return create(value); } catch (IOException err) { throw new IoException(GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(), Binary.class.getSimpleName()), err); } }
protected void assertFileContains( Projection projection, String filePath, InputStream expectedContent ) throws IOException { assertFileContains(projection, filePath, IoUtil.readBytes(expectedContent)); }
protected void assertBinaryContains( Binary binaryValue, byte[] expectedContent ) throws IOException, RepositoryException { byte[] actual = IoUtil.readBytes(binaryValue.getStream()); assertThat(actual, is(expectedContent)); }
protected void assertFileContains( Projection projection, String filePath, byte[] expectedContent ) throws IOException { File contentFile = projection.getTestFile(filePath); assertThat(contentFile.exists(), is(true)); byte[] actual = IoUtil.readBytes(contentFile); assertThat(actual, is(expectedContent)); }
@Test public void readBytesShouldReturnEmptyByteArrayForNullInputStream() throws Exception { assertThat(IoUtil.readBytes((InputStream)null), is(new byte[] {})); }
public BinaryContains( BinaryValue expectedContent ) { try { this.expectedContent = IoUtil.readBytes(expectedContent.getStream()); } catch (RepositoryException e) { throw new AssertionFailedError(e.getMessage()); } catch (IOException e) { throw new AssertionFailedError(e.getMessage()); } finally { expectedContent.dispose(); } }
private void storeBinaryAndAssert( byte[] data, String nodeName ) throws RepositoryException, IOException { InputStream stream = storeBinaryProperty(data, nodeName); byte[] storedData = IoUtil.readBytes(stream); assertArrayEquals("Data retrieved does not match data stored", data, storedData); }
@Test public void shouldProvideInputStreamToContent() throws Exception { InputStream stream = binary.getStream(); byte[] actual = IoUtil.readBytes(stream); // closes the stream assertThat(actual.length, is(validByteArrayContent.length)); for (int i = 0, len = actual.length; i != len; ++i) { assertThat(actual[i], is(validByteArrayContent[i])); } }
@Test public void shouldProvideInputStreamToContent() throws IOException { InputStream stream = binary.getStream(); byte[] actual = IoUtil.readBytes(stream); // closes the stream assertThat(actual.length, is(validByteArrayContent.length)); for (int i = 0, len = actual.length; i != len; ++i) { assertThat(actual[i], is(validByteArrayContent[i])); } }
@Test @FixFor("MODE-2413") public void shouldSupportReadingFromSelfClosingInputStream() throws Exception { byte[] buffer = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); URL resource = getClass().getResource("simple.json"); try (Base64.InputStream is = new Base64.InputStream(new SelfClosingInputStream(resource.openStream()), Base64.ENCODE)) { int read; while ((read = is.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, read); } } // until Java 8, use this.... String expectedString = DatatypeConverter.printBase64Binary(IoUtil.readBytes(resource.openStream())); assertEquals("Incorrect Base64 encoding", expectedString, new String(bos.toByteArray())); } }
@Test public void shouldCallGetBinaryUsingColmnIndex() throws SQLException, IOException { int col = getColumnTypeLoc(TestUtil.BINARY); for (int i = 0; i < TestUtil.TUPLES.size(); i++) { assertThat(resultSet.next(), is(true)); Object[] tuple = TestUtil.TUPLES.get(i); // need to increment because ResultSet is 1 based. assertThat(IoUtil.readBytes(resultSet.getBinaryStream(col + 1)), is(tuple[col])); } }
@Test public void shouldCallGetBinaryUsingColumnName() throws SQLException, IOException { int col = getColumnTypeLoc(TestUtil.BINARY); for (int i = 0; i < TestUtil.TUPLES.size(); i++) { assertThat(resultSet.next(), is(true)); Object[] tuple = TestUtil.TUPLES.get(i); assertThat(IoUtil.readBytes(resultSet.getBinaryStream(TestUtil.COLUMN_NAMES[col])), is(tuple[col])); } }
@Test public void shouldCreateFile() throws Exception { String folderUri = resourceUri(testFolder()); sardine.createDirectory(folderUri); InputStream fileStream = getClass().getClassLoader().getResourceAsStream("textfile.txt"); assertNotNull(fileStream); String fileUri = folderUri + "/testFile" + UUID.randomUUID().toString(); sardine.put(fileUri, fileStream); assertTrue(sardine.exists(fileUri)); DavResource file = getResourceAtURI(fileUri); byte[] fileBytes = IoUtil.readBytes(getClass().getClassLoader().getResourceAsStream("textfile.txt")); assertEquals(fileBytes.length, file.getContentLength().longValue()); }
@Test public void shouldPersistContentIntoTheDefaultStore() throws BinaryStoreException, IOException { byte[] content = randomContent(); BinaryValue v = store.storeValue(new ByteArrayInputStream(content), false); InputStream is = store.getInputStream(v.getKey()); byte[] storeContent = IoUtil.readBytes(is); InputStream is1 = defaultStore.getInputStream(v.getKey()); byte[] defaultStoreContent = IoUtil.readBytes(is1); assertArrayEquals(content, storeContent); assertArrayEquals(content, defaultStoreContent); }
@Test public void shouldLookInAllBinaryStoresForAKey() throws BinaryStoreException, IOException { byte[] content = randomContent(); BinaryValue v = alternativeStore.storeValue(new ByteArrayInputStream(content), false); InputStream is = store.getInputStream(v.getKey()); byte[] storedContent = IoUtil.readBytes(is); assertArrayEquals(content, storedContent); assertThat(store.hasBinary(v.getKey()), is(true)); }
@FixFor( "MODE-1308" ) @Test public void shouldSupportAnyBinaryImplementation() throws Exception { final String stringValue = "This is the string stringValue"; Binary customBinary = createCustomBinary(stringValue); JcrValue jcrValue = new JcrValue(factories, PropertyType.BINARY, customBinary); org.modeshape.jcr.value.BinaryValue actualValue = jcrValue.getBinary(); assertNotNull(actualValue); byte[] actualBytes = IoUtil.readBytes(actualValue.getStream()); assertArrayEquals(stringValue.getBytes(), actualBytes); }
private BinaryValue storeAndValidate( BinaryKey key, byte[] data ) throws BinaryStoreException, IOException { BinaryValue res = getBinaryStore().storeValue(new ByteArrayInputStream(data), false); assertNotNull(res); assertEquals(key, res.getKey()); assertEquals(data.length, res.getSize()); InputStream inputStream = getBinaryStore().getInputStream(key); byte[] content = IoUtil.readBytes(inputStream); assertArrayEquals(data, content); BinaryKey currentKey = BinaryKey.keyFor(content); assertEquals(key, currentKey); return res; }