281 |
throw new ClosedChannelException (); |
throw new ClosedChannelException (); |
282 |
} |
} |
283 |
|
|
284 |
public long transferTo (long position, long count, WritableByteChannel target) |
// like transferTo, but with a count of less than 2Gbytes |
285 |
|
private int smallTransferTo (long position, int count, |
286 |
|
WritableByteChannel target) |
287 |
|
throws IOException |
288 |
|
{ |
289 |
|
ByteBuffer buffer; |
290 |
|
try |
291 |
|
{ |
292 |
|
// Try to use a mapped buffer if we can. If this fails for |
293 |
|
// any reason we'll fall back to using a ByteBuffer. |
294 |
|
buffer = map (MapMode.READ_ONLY, position, count); |
295 |
|
} |
296 |
|
catch (IOException e) |
297 |
|
{ |
298 |
|
buffer = ByteBuffer.allocate (count); |
299 |
|
read (buffer, position); |
300 |
|
buffer.flip(); |
301 |
|
} |
302 |
|
|
303 |
|
return target.write (buffer); |
304 |
|
} |
305 |
|
|
306 |
|
public long transferTo (long position, long count, |
307 |
|
WritableByteChannel target) |
308 |
throws IOException |
throws IOException |
309 |
{ |
{ |
310 |
if (position < 0 |
if (position < 0 |
317 |
if ((mode & READ) == 0) |
if ((mode & READ) == 0) |
318 |
throw new NonReadableChannelException (); |
throw new NonReadableChannelException (); |
319 |
|
|
320 |
// XXX: count needs to be casted from long to int. Dataloss ? |
final int pageSize = 65536; |
321 |
ByteBuffer buffer = ByteBuffer.allocate ((int) count); |
long total = 0; |
322 |
read (buffer, position); |
|
323 |
buffer.flip(); |
while (count > 0) |
324 |
return target.write (buffer); |
{ |
325 |
|
int transferred |
326 |
|
= smallTransferTo (position, (int)Math.min (count, pageSize), |
327 |
|
target); |
328 |
|
if (transferred < 0) |
329 |
|
break; |
330 |
|
total += transferred; |
331 |
|
position += transferred; |
332 |
|
count -= transferred; |
333 |
|
} |
334 |
|
|
335 |
|
return total; |
336 |
} |
} |
337 |
|
|
338 |
public long transferFrom (ReadableByteChannel src, long position, long count) |
// like transferFrom, but with a count of less than 2Gbytes |
339 |
|
private int smallTransferFrom (ReadableByteChannel src, long position, |
340 |
|
int count) |
341 |
|
throws IOException |
342 |
|
{ |
343 |
|
ByteBuffer buffer = null; |
344 |
|
|
345 |
|
if (src instanceof FileChannel) |
346 |
|
{ |
347 |
|
try |
348 |
|
{ |
349 |
|
// Try to use a mapped buffer if we can. If this fails |
350 |
|
// for any reason we'll fall back to using a ByteBuffer. |
351 |
|
buffer = ((FileChannel)src).map (MapMode.READ_ONLY, position, |
352 |
|
count); |
353 |
|
} |
354 |
|
catch (IOException e) |
355 |
|
{ |
356 |
|
} |
357 |
|
} |
358 |
|
|
359 |
|
if (buffer == null) |
360 |
|
{ |
361 |
|
buffer = ByteBuffer.allocate ((int) count); |
362 |
|
src.read (buffer); |
363 |
|
buffer.flip(); |
364 |
|
} |
365 |
|
|
366 |
|
return write (buffer, position); |
367 |
|
} |
368 |
|
|
369 |
|
public long transferFrom (ReadableByteChannel src, long position, |
370 |
|
long count) |
371 |
throws IOException |
throws IOException |
372 |
{ |
{ |
373 |
if (position < 0 |
if (position < 0 |
380 |
if ((mode & WRITE) == 0) |
if ((mode & WRITE) == 0) |
381 |
throw new NonWritableChannelException (); |
throw new NonWritableChannelException (); |
382 |
|
|
383 |
// XXX: count needs to be casted from long to int. Dataloss ? |
final int pageSize = 65536; |
384 |
ByteBuffer buffer = ByteBuffer.allocate ((int) count); |
long total = 0; |
385 |
src.read (buffer); |
|
386 |
buffer.flip(); |
while (count > 0) |
387 |
return write (buffer, position); |
{ |
388 |
|
int transferred = smallTransferFrom (src, position, |
389 |
|
(int)Math.min (count, pageSize)); |
390 |
|
if (transferred < 0) |
391 |
|
break; |
392 |
|
total += transferred; |
393 |
|
position += transferred; |
394 |
|
count -= transferred; |
395 |
|
} |
396 |
|
|
397 |
|
return total; |
398 |
} |
} |
399 |
|
|
400 |
public FileLock tryLock (long position, long size, boolean shared) |
public FileLock tryLock (long position, long size, boolean shared) |