@Test public void testPutStreamOptimizedDisk() throws Exception { NfcClient nfcClient = spy(new NfcClient()); StreamVmdkReader disk = mock(StreamVmdkReader.class); DiskWriter writer = mock(DiskWriter.class); InputStream inputStream = new ByteArrayInputStream("test content".getBytes()); doReturn(disk).when(nfcClient).getStreamVmdkReader(inputStream); when(disk.getAdapterType()).thenReturn("buslogic"); when(disk.getCapacityInSectors()).thenReturn(65536L); Map<String, String> ddb = new LinkedHashMap<>(); when(disk.getDdb()).thenReturn(ddb); doReturn(writer).when(nfcClient).putDisk("image destination path", "buslogic", 65536L); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; when(disk.getNextGrain(grain)).thenReturn(65536, -1); when(disk.getCurrentLba()).thenReturn(0L); long bytes = nfcClient.putStreamOptimizedDisk("image destination path", inputStream); assertThat(bytes, is(33554432L)); InOrder inOrder = inOrder(writer, nfcClient); inOrder.verify(writer).writeDdb(ddb); inOrder.verify(writer, times(128)).writeGrain(anyLong(), any(byte[].class), anyInt()); inOrder.verify(writer).finalizeWrite(); inOrder.verify(nfcClient).close(); inOrder.verify(writer).close(); inOrder.verify(nfcClient).abort(); }
public long putStreamOptimizedDisk( String filePath, InputStream inputStream) throws IOException, VmdkFormatException { StreamVmdkReader disk = getStreamVmdkReader(inputStream); DiskWriter writer = null; try { writer = putDisk(filePath, disk.getAdapterType(), disk.getCapacityInSectors()); writer.writeDdb(disk.getDdb()); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; int grainSize = disk.getNextGrain(grain); int grainCount = 0; while (grainSize >= 0) { grainCount++; if (0 == (grainCount % 1000)) { logger.debug("NfcClient putStreamOptimizedDisk for file {} preparing to write grain count/size {}/{}", filePath, grainCount, grainSize); } if (grainSize > 0) { long lba = disk.getCurrentLba(); for (int offset = 0; offset < grainSize; offset += SparseUtil.DISKLIB_SECTOR_SIZE) { writer.writeGrain(lba++, grain, offset); } } grainSize = disk.getNextGrain(grain); } logger.debug("NfcClient putStreamOptimizedDisk for file {} write complete on grain count/size {}/{}", filePath, grainCount, grainSize); writer.finalizeWrite(); close(); return disk.getCapacityInSectors() * SparseUtil.DISKLIB_SECTOR_SIZE; } catch (SocketException e) { logger.error("Remote host closed connection while uploading to datastore (could be of out of space): {}", e); throw e; } finally { if (writer != null) { writer.close(); } abort(); } }
NfcClient implements AutoCloseable { public long putStreamOptimizedDisk( String filePath, InputStream inputStream) throws IOException, VmdkFormatException { StreamVmdkReader disk = getStreamVmdkReader(inputStream); DiskWriter writer = null; try { writer = putDisk(filePath, disk.getAdapterType(), disk.getCapacityInSectors()); writer.writeDdb(disk.getDdb()); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; int grainSize = disk.getNextGrain(grain); int grainCount = 0; while (grainSize >= 0) { grainCount++; if (0 == (grainCount % 1000)) { logger.debug("NfcClient putStreamOptimizedDisk for file {} preparing to write grain count/size {}/{}", filePath, grainCount, grainSize); } if (grainSize > 0) { long lba = disk.getCurrentLba(); for (int offset = 0; offset < grainSize; offset += SparseUtil.DISKLIB_SECTOR_SIZE) { writer.writeGrain(lba++, grain, offset); } } grainSize = disk.getNextGrain(grain); } logger.debug("NfcClient putStreamOptimizedDisk for file {} write complete on grain count/size {}/{}", filePath, grainCount, grainSize); writer.finalizeWrite(); close(); return disk.getCapacityInSectors() * SparseUtil.DISKLIB_SECTOR_SIZE; } catch (SocketException e) { logger.error("Remote host closed connection while uploading to datastore (could be of out of space): {}", e); throw e; } finally { if (writer != null) { writer.close(); } abort(); } } }
NfcClient implements AutoCloseable { public long putStreamOptimizedDisk( String filePath, InputStream inputStream) throws IOException, VmdkFormatException { StreamVmdkReader disk = getStreamVmdkReader(inputStream); DiskWriter writer = null; try { writer = putDisk(filePath, disk.getAdapterType(), disk.getCapacityInSectors()); writer.writeDdb(disk.getDdb()); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; int grainSize = disk.getNextGrain(grain); int grainCount = 0; while (grainSize >= 0) { grainCount++; if (0 == (grainCount % 1000)) { logger.debug("NfcClient putStreamOptimizedDisk for file {} preparing to write grain count/size {}/{}", filePath, grainCount, grainSize); } if (grainSize > 0) { long lba = disk.getCurrentLba(); for (int offset = 0; offset < grainSize; offset += SparseUtil.DISKLIB_SECTOR_SIZE) { writer.writeGrain(lba++, grain, offset); } } grainSize = disk.getNextGrain(grain); } logger.debug("NfcClient putStreamOptimizedDisk for file {} write complete on grain count/size {}/{}", filePath, grainCount, grainSize); writer.finalizeWrite(); close(); return disk.getCapacityInSectors() * SparseUtil.DISKLIB_SECTOR_SIZE; } catch (SocketException e) { logger.error("Remote host closed connection while uploading to datastore (could be of out of space): {}", e); throw e; } finally { if (writer != null) { writer.close(); } abort(); } } @VisibleForTesting protected  NfcClient();  NfcClient(HostServiceTicket ticket, int timeoutMs); }
NfcClient implements AutoCloseable { public long putStreamOptimizedDisk( String filePath, InputStream inputStream) throws IOException, VmdkFormatException { StreamVmdkReader disk = getStreamVmdkReader(inputStream); DiskWriter writer = null; try { writer = putDisk(filePath, disk.getAdapterType(), disk.getCapacityInSectors()); writer.writeDdb(disk.getDdb()); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; int grainSize = disk.getNextGrain(grain); int grainCount = 0; while (grainSize >= 0) { grainCount++; if (0 == (grainCount % 1000)) { logger.debug("NfcClient putStreamOptimizedDisk for file {} preparing to write grain count/size {}/{}", filePath, grainCount, grainSize); } if (grainSize > 0) { long lba = disk.getCurrentLba(); for (int offset = 0; offset < grainSize; offset += SparseUtil.DISKLIB_SECTOR_SIZE) { writer.writeGrain(lba++, grain, offset); } } grainSize = disk.getNextGrain(grain); } logger.debug("NfcClient putStreamOptimizedDisk for file {} write complete on grain count/size {}/{}", filePath, grainCount, grainSize); writer.finalizeWrite(); close(); return disk.getCapacityInSectors() * SparseUtil.DISKLIB_SECTOR_SIZE; } catch (SocketException e) { logger.error("Remote host closed connection while uploading to datastore (could be of out of space): {}", e); throw e; } finally { if (writer != null) { writer.close(); } abort(); } } @VisibleForTesting protected  NfcClient();  NfcClient(HostServiceTicket ticket, int timeoutMs); NfcFileOutputStream putFileAutoClose(String dsPath, long fileSize); NfcFileOutputStream putFile(String dsPath, long fileSize); DiskWriter putDisk(String dsPath, String adapterType, long capacity); long putStreamOptimizedDisk(
      String filePath,
      InputStream inputStream); NfcFileInputStream getFile(String dsPath); DiskReader getDisk(String dsPath); void mkdir(String dsPath); void delete(String dsPath); void close(); void abort(); void fssrvrOpenDisk(String dsPath); void fssrvrWriteDisk(long lba, byte[] data, int length); void fssrvrCloseDisk(); }
NfcClient implements AutoCloseable { public long putStreamOptimizedDisk( String filePath, InputStream inputStream) throws IOException, VmdkFormatException { StreamVmdkReader disk = getStreamVmdkReader(inputStream); DiskWriter writer = null; try { writer = putDisk(filePath, disk.getAdapterType(), disk.getCapacityInSectors()); writer.writeDdb(disk.getDdb()); byte[] grain = new byte[disk.getGrainSize() * SparseUtil.DISKLIB_SECTOR_SIZE]; int grainSize = disk.getNextGrain(grain); int grainCount = 0; while (grainSize >= 0) { grainCount++; if (0 == (grainCount % 1000)) { logger.debug("NfcClient putStreamOptimizedDisk for file {} preparing to write grain count/size {}/{}", filePath, grainCount, grainSize); } if (grainSize > 0) { long lba = disk.getCurrentLba(); for (int offset = 0; offset < grainSize; offset += SparseUtil.DISKLIB_SECTOR_SIZE) { writer.writeGrain(lba++, grain, offset); } } grainSize = disk.getNextGrain(grain); } logger.debug("NfcClient putStreamOptimizedDisk for file {} write complete on grain count/size {}/{}", filePath, grainCount, grainSize); writer.finalizeWrite(); close(); return disk.getCapacityInSectors() * SparseUtil.DISKLIB_SECTOR_SIZE; } catch (SocketException e) { logger.error("Remote host closed connection while uploading to datastore (could be of out of space): {}", e); throw e; } finally { if (writer != null) { writer.close(); } abort(); } } @VisibleForTesting protected  NfcClient();  NfcClient(HostServiceTicket ticket, int timeoutMs); NfcFileOutputStream putFileAutoClose(String dsPath, long fileSize); NfcFileOutputStream putFile(String dsPath, long fileSize); DiskWriter putDisk(String dsPath, String adapterType, long capacity); long putStreamOptimizedDisk(
      String filePath,
      InputStream inputStream); NfcFileInputStream getFile(String dsPath); DiskReader getDisk(String dsPath); void mkdir(String dsPath); void delete(String dsPath); void close(); void abort(); void fssrvrOpenDisk(String dsPath); void fssrvrWriteDisk(long lba, byte[] data, int length); void fssrvrCloseDisk(); static final int MAX_XFER_SIZE; }