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 FileWriteChannel}.
13 class FileWriteChannelImpl
implements FileWriteChannel
{
15 private FileServiceImpl fileService
;
16 private AppEngineFile file
;
17 private boolean lockHeld
;
18 private boolean isOpen
;
19 private final Object lock
= new Object();
21 FileWriteChannelImpl(AppEngineFile f
, boolean lock
, FileServiceImpl fs
) {
24 this.fileService
= fs
;
27 throw new NullPointerException("file is null");
29 if (!f
.isWritable()) {
30 throw new IllegalArgumentException("file is not writable");
34 private void checkOpen() throws ClosedChannelException
{
36 throw new ClosedChannelException();
44 public int write(ByteBuffer src
) throws IOException
{
46 return write(src
, null);
54 public int write(ByteBuffer buffer
, String sequenceKey
) throws IOException
{
57 return fileService
.append(file
, buffer
, sequenceKey
);
65 public boolean isOpen() {
75 public void close() throws IOException
{
80 fileService
.close(file
, false);
89 public void closeFinally() throws IllegalStateException
, IOException
{
92 throw new IllegalStateException(
93 "The lock for this file is not held by the current request");
96 fileService
.close(file
, true);
99 fileService
.openForAppend(file
, true);
100 fileService
.close(file
, true);
101 } catch (FinalizationException e
) {