View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.file;
21  
22  import java.io.IOException;
23  import java.nio.channels.FileChannel;
24  
25  /**
26   * Manage a File to be sent to a remote host. We keep a track on the current
27   * position, and the number of already written bytes.
28   * 
29   * 
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class DefaultFileRegion implements FileRegion {
33      /** The channel used to manage the file */
34      private final FileChannel channel;
35  
36      /** The original position in the file */
37      private final long originalPosition;
38  
39      /** The position in teh file */
40      private long position;
41  
42      /** The number of bytes remaining to write */
43      private long remainingBytes;
44  
45      /**
46       * Creates a new DefaultFileRegion instance
47       * 
48       * @param channel The channel mapped over the file
49       * @throws IOException If we had an IO error
50       */
51      public DefaultFileRegion(FileChannel channel) throws IOException {
52          this(channel, 0, channel.size());
53      }
54  
55      /**
56       * Creates a new DefaultFileRegion instance
57       * 
58       * @param channel The channel mapped over the file
59       * @param position The position in teh file
60       * @param remainingBytes The remaining bytes
61       */
62      public DefaultFileRegion(FileChannel channel, long position, long remainingBytes) {
63          if (channel == null) {
64              throw new IllegalArgumentException("channel can not be null");
65          }
66          
67          if (position < 0) {
68              throw new IllegalArgumentException("position may not be less than 0");
69          }
70          
71          if (remainingBytes < 0) {
72              throw new IllegalArgumentException("remainingBytes may not be less than 0");
73          }
74          
75          this.channel = channel;
76          this.originalPosition = position;
77          this.position = position;
78          this.remainingBytes = remainingBytes;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public long getWrittenBytes() {
86          return position - originalPosition;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public long getRemainingBytes() {
94          return remainingBytes;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public FileChannel getFileChannel() {
102         return channel;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public long getPosition() {
110         return position;
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public void update(long value) {
118         position += value;
119         remainingBytes -= value;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public String getFilename() {
127         return null;
128     }
129 }