Java Code Examples for java.nio.MappedByteBuffer#hasRemaining()
The following examples show how to use
java.nio.MappedByteBuffer#hasRemaining() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YoloVisionTest.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 2
Source File: Bug1169.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@NoWarning("RCN,NP") @Nonnull public <R> R execute2(@Nonnull final Engine<R> engine, @Nonnull final Path path, @Nonnull final byte[] buffer) throws UnsupportedOperationException, IOException { try (FileChannel c = open(path, StandardOpenOption.READ)) { engine.reset(); final long size = c.size(); long i = 0; while (i < size) { final MappedByteBuffer mb = c.map(MapMode.READ_ONLY, i, Math.min(size - i, Integer.MAX_VALUE)); final int bl = buffer.length; while (mb.hasRemaining()) { final int get = Math.min(mb.remaining(), bl); mb.get(buffer, 0, get); engine.update(buffer, 0, get); i += get; } } return engine.digest(); } }
Example 3
Source File: Bug1169.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@NoWarning("RCN,NP") @Nonnull protected <R> R executeEngine(@Nonnull final Engine<R> engine, @Nonnull final Path path) throws IOException { try (final FileChannel c = open(path, StandardOpenOption.READ)) { engine.reset(); final MappedByteBuffer mb = c.map(MapMode.READ_ONLY, 0L, c.size()); final int bufferLength = 8*1024; final byte[] buffer = new byte[bufferLength]; while (mb.hasRemaining()) { final int get = Math.min(mb.remaining(), bufferLength); mb.get(buffer, 0, get); engine.update(buffer, 0, get); } return engine.digest(); } }
Example 4
Source File: VisionTest.java From SoftwarePilot with MIT License | 6 votes |
byte[] readImg(){ try { //BufferedImage origImage = ImageIO.read(imgPath); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //ImageIO.write(origImage, "jpg", baos); //return baos.toByteArray(); File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); byte[] pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 5
Source File: FileChannelUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); FileChannel channel = reader.getChannel(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); if (buff.hasRemaining()) { byte[] data = new byte[buff.remaining()]; buff.get(data); assertEquals("world", new String(data, StandardCharsets.UTF_8)); } } }
Example 6
Source File: BasicVision.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 7
Source File: RoutineTemplate.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 8
Source File: WaypointMissionTestBackup.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 9
Source File: WaypointMissionTest.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 10
Source File: WaypointMissionTest.java From SoftwarePilot with MIT License | 6 votes |
byte[] readWaypoint(String fname){ //read in the file try{ File f = new File(Environment.getExternalStorageDirectory().getPath()+fname); FileChannel fileChannel = new RandomAccessFile(f,"r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileChannel.size()); data = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = data.length; if (buffer.remaining()<remaining){ remaining = buffer.remaining(); } buffer.get(data,0,remaining); } return data; }catch (IOException e) { e.printStackTrace(); } return new byte[0]; }
Example 11
Source File: AutoSelfie.java From SoftwarePilot with MIT License | 6 votes |
byte[] readImg(){ try { //BufferedImage origImage = ImageIO.read(imgPath); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //ImageIO.write(origImage, "jpg", baos); //return baos.toByteArray(); File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 12
Source File: BasicRoutine.java From SoftwarePilot with MIT License | 6 votes |
byte[] read4k(){ try { File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example 13
Source File: GenericCopyUtil.java From PowerFileExplorer with GNU General Public License v3.0 | 5 votes |
private boolean copyFile(final FileChannel inChannel, final BufferedOutputStream bufferedOutputStream) throws IOException { final MappedByteBuffer inBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, mSourceFile.size); int count = -1; final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; while (inBuffer.hasRemaining() && count != 0) { if (progressHandler.isCancelled) { bufferedOutputStream.close(); return false; } final int tempPosition = inBuffer.position(); try { // try normal way of getting bytes final ByteBuffer tempByteBuffer = inBuffer.get(buffer); count = tempByteBuffer.position() - tempPosition; } catch (BufferUnderflowException exception) { exception.printStackTrace(); // not enough bytes left in the channel to read, iterate over each byte and store // in the buffer // reset the counter bytes count = 0; final int length = buffer.length; for (int i=0; i < length && inBuffer.hasRemaining(); i++) { buffer[i] = inBuffer.get(); count++; } } if (count != -1) { bufferedOutputStream.write(buffer, 0, count); ServiceWatcherUtil.POSITION = inBuffer.position(); } } bufferedOutputStream.flush(); return true; }
Example 14
Source File: MappedPageSourceIT.java From offheap-store with Apache License 2.0 | 5 votes |
@Test public void testCreateLotsOfMappedBuffers() throws IOException { final int size = 1024 * 1024; final int count = 16; MappedPageSource source = new MappedPageSource(dataFile); try { byte[] data = new byte[1024]; Arrays.fill(data, (byte) 0xff); List<MappedPage> pages = new ArrayList<>(); for (int i = 0; i < count; i++) { pages.add(source.allocate(size, false, false, null)); } for (MappedPage p : pages) { MappedByteBuffer b = p.asByteBuffer(); while (b.hasRemaining()) { b.put(data); } b.force(); } } finally { source.close(); } try (RandomAccessFile raf = new RandomAccessFile(source.getFile(), "r")) { Assert.assertEquals(size * count, raf.length()); } }
Example 15
Source File: FileUtil.java From cc-dbp with Apache License 2.0 | 5 votes |
/** * get an input stream for the file that is fast, trading memory for speed * * @param file * @return InputStream */ public static InputStream fastStream(File file) { FileInputStream fis = null; FileChannel ch = null; byte[] byteArray = null; try { fis = new FileInputStream(file); if (fis != null) { ch = fis.getChannel(); if (ch.size() > 1000000000) { return new BufferedInputStream(fis, BUFFER_SIZE); } MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, ch.size()); byteArray = new byte[mb.capacity()]; int got; while (mb.hasRemaining()) { got = Math.min(mb.remaining(), byteArray.length); mb.get(byteArray, 0, got); } } } catch (FileNotFoundException fnfe) { throw new IOError(fnfe); } catch (IOException ioe) { throw new IOError(ioe); } finally { if (ch != null) { try { ch.close(); fis.close(); } catch (IOException ioe2) { } } } return new ByteArrayInputStream(byteArray); }
Example 16
Source File: FileChannelDemo03.java From javacore with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
public static void main(String[] args) throws Exception { File file = new File("d:" + File.separator + "out.txt"); FileInputStream input = new FileInputStream(file); FileChannel fin = input.getChannel(); // 得到输入的通道 MappedByteBuffer mbb = fin.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); byte[] data = new byte[(int) file.length()]; // 开辟空间接收内容 int foot = 0; while (mbb.hasRemaining()) { data[foot++] = mbb.get(); // 读取数据 } System.out.println(new String(data)); // 输出内容 fin.close(); input.close(); }
Example 17
Source File: CharReadingBenchmark.java From SimpleFlatMapper with MIT License | 5 votes |
@Benchmark public void memoryMappedFile(Blackhole blackhole) throws IOException { char[] buffer = new char[bufferSize]; try (FileChannel channel = FileChannel.open(file.toPath())) { MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); CharsetDecoder charsetDecoder = charset.newDecoder(); while(byteBuffer.hasRemaining()) { CoderResult coderResult = charsetDecoder.decode(byteBuffer, CharBuffer.wrap(buffer), true); blackhole.consume(buffer); } } }
Example 18
Source File: FileUtil.java From Inspeckage with Apache License 2.0 | 5 votes |
public static Map<String, String> readMultiFile(SharedPreferences prefs, String folderName) { Map<String, String> files = new HashMap<>(); try { String absolutePath; if (prefs.getBoolean(Config.SP_HAS_W_PERMISSION, false)) { absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath()+Config.P_ROOT+"/"+prefs.getString(Config.SP_PACKAGE,"")+"/"+ folderName; } else { absolutePath = prefs.getString(Config.SP_DATA_DIR, null)+Config.P_ROOT+"/"+folderName; } File folder = new File(absolutePath); if (folder.listFiles() != null && folder.length() > 0) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.exists() && fileEntry.isFile()) { FileInputStream f = new FileInputStream(fileEntry.getAbsolutePath()); FileChannel ch = f.getChannel(); MappedByteBuffer mbb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, ch.size()); String text = ""; while (mbb.hasRemaining()) { String charsetName = "UTF-8"; CharBuffer cb = Charset.forName(charsetName).decode(mbb); text = cb.toString(); } files.put(fileEntry.getName(), text); } } } } catch (Exception e) { e.printStackTrace(); } return files; }
Example 19
Source File: TextFiles.java From Java-Coding-Problems with MIT License | 4 votes |
public static long countOccurrencesV5(Path path, String text) throws IOException { if (path == null || text == null) { throw new IllegalArgumentException("Path/text cannot be null"); } if (text.isBlank()) { return 0; } final byte[] texttofind = text.getBytes(StandardCharsets.UTF_8); long count = 0; try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) { long position = 0; long length = fileChannel.size(); while (position < length) { long remaining = length - position; long bytestomap = (long) Math.min(MAP_SIZE, remaining); MappedByteBuffer mbBuffer = fileChannel.map(MapMode.READ_ONLY, position, bytestomap); long limit = mbBuffer.limit(); long lastSpace = -1; long firstChar = -1; while (mbBuffer.hasRemaining()) { boolean isFirstChar = false; while (firstChar != 0 && mbBuffer.hasRemaining()) { byte currentByte = mbBuffer.get(); if (Character.isWhitespace((char) currentByte)) { lastSpace = mbBuffer.position(); } if (texttofind[0] == currentByte) { isFirstChar = true; break; } } if (isFirstChar) { boolean isRestOfChars = true; int j; for (j = 1; j < texttofind.length; j++) { if (!mbBuffer.hasRemaining() || texttofind[j] != mbBuffer.get()) { isRestOfChars = false; break; } } if (isRestOfChars) { count++; lastSpace = -1; } firstChar = -1; } } if (lastSpace > -1) { position = position - (limit - lastSpace); } position += bytestomap; } } return count; }
Example 20
Source File: Mismatches.java From Java-Coding-Problems with MIT License | 4 votes |
public static boolean haveMismatches(Path p1, Path p2) throws IOException { if (p1 == null || p2 == null) { throw new IllegalArgumentException("The given paths cannot be null"); } try ( FileChannel channel1 = (FileChannel.open(p1, EnumSet.of(StandardOpenOption.READ)))) { try ( FileChannel channel2 = (FileChannel.open(p2, EnumSet.of(StandardOpenOption.READ)))) { long length1 = channel1.size(); long length2 = channel2.size(); if (length1 != length2) { return true; } int position = 0; while (position < length1) { long remaining = length1 - position; int bytestomap = (int) Math.min(MAP_SIZE, remaining); MappedByteBuffer mbBuffer1 = channel1.map( MapMode.READ_ONLY, position, bytestomap); MappedByteBuffer mbBuffer2 = channel2.map( MapMode.READ_ONLY, position, bytestomap); while (mbBuffer1.hasRemaining()) { if (mbBuffer1.get() != mbBuffer2.get()) { return true; } } position += bytestomap; } } } return false; }