1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.files
;
5 import java
.io
.IOException
;
6 import java
.nio
.ByteBuffer
;
7 import java
.nio
.channels
.ClosedChannelException
;
10 * An implementation of {@code FileReadChannel}.
14 class FileReadChannelImpl
implements FileReadChannel
{
16 private FileServiceImpl fileService
;
17 private AppEngineFile file
;
18 private long position
;
19 private boolean isOpen
;
20 private boolean reachedEOF
;
21 private final Object lock
= new Object();
23 FileReadChannelImpl(AppEngineFile f
, FileServiceImpl fs
) {
25 this.fileService
= fs
;
29 throw new NullPointerException("file is null");
32 throw new NullPointerException("fs is null");
34 if (!f
.isReadable()) {
35 throw new IllegalArgumentException("file is not readable");
39 private void checkOpen() throws ClosedChannelException
{
42 throw new ClosedChannelException();
49 * @throws ClosedChannelException
52 public long position() throws ClosedChannelException
{
63 public FileReadChannel
position(long newPosition
) throws IOException
{
64 if (newPosition
< 0) {
65 throw new IllegalArgumentException("newPosition may not be negative");
69 position
= newPosition
;
79 public int read(ByteBuffer dst
) throws IOException
{
84 int numBytesRead
= fileService
.read(file
, dst
, position
);
85 if (numBytesRead
>= 0) {
86 position
+= numBytesRead
;
98 public boolean isOpen() {
108 public void close() throws IOException
{
109 synchronized (lock
) {
113 fileService
.close(file
, false);
119 public String
toString() {
120 return "FileReadChannel [file=" + file
+ ", position=" + position
+ ", isOpen=" + isOpen
121 + ", reachedEOF=" + reachedEOF
+ "]";