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}.
13 class FileReadChannelImpl
implements FileReadChannel
{
15 private FileServiceImpl fileService
;
16 private AppEngineFile file
;
17 private long position
;
18 private boolean isOpen
;
19 private boolean reachedEOF
;
20 private final Object lock
= new Object();
22 FileReadChannelImpl(AppEngineFile f
, FileServiceImpl fs
) {
24 this.fileService
= fs
;
28 throw new NullPointerException("file is null");
31 throw new NullPointerException("fs is null");
33 if (!f
.isReadable()) {
34 throw new IllegalArgumentException("file is not readable");
38 private void checkOpen() throws ClosedChannelException
{
41 throw new ClosedChannelException();
48 * @throws ClosedChannelException
51 public long position() throws ClosedChannelException
{
62 public FileReadChannel
position(long newPosition
) throws IOException
{
63 if (newPosition
< 0) {
64 throw new IllegalArgumentException("newPosition may not be negative");
68 position
= newPosition
;
78 public int read(ByteBuffer dst
) throws IOException
{
83 int numBytesRead
= fileService
.read(file
, dst
, position
);
84 if (numBytesRead
>= 0) {
85 position
+= numBytesRead
;
97 public boolean isOpen() {
107 public void close() throws IOException
{
108 synchronized (lock
) {
112 fileService
.close(file
, false);