View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.gateway.kernel.commonfile;
21  
22  import io.netty.buffer.ByteBuf;
23  import io.netty.buffer.ByteBufAllocator;
24  import io.netty.buffer.Unpooled;
25  import io.netty.channel.ChannelHandlerContext;
26  import io.netty.handler.stream.ChunkedInput;
27  import org.waarp.common.command.exception.CommandAbstractException;
28  import org.waarp.common.exception.FileEndOfTransferException;
29  import org.waarp.common.exception.FileTransferException;
30  import org.waarp.common.file.DataBlock;
31  import org.waarp.common.file.FileInterface;
32  import org.waarp.gateway.kernel.exception.HttpIncorrectRetrieveException;
33  
34  /**
35   *
36   */
37  public class CommonFileChunkedInput implements ChunkedInput<ByteBuf> {
38  
39    private final FileInterface document;
40  
41    private boolean lastChunkAlready;
42  
43    private long offset;
44  
45    /**
46     * @param document
47     *
48     * @throws HttpIncorrectRetrieveException
49     */
50    public CommonFileChunkedInput(final FileInterface document)
51        throws HttpIncorrectRetrieveException {
52      this.document = document;
53      try {
54        this.document.retrieve();
55      } catch (final CommandAbstractException e) {
56        throw new HttpIncorrectRetrieveException(e);
57      }
58    }
59  
60    @Override
61    public final ByteBuf readChunk(final ChannelHandlerContext ctx)
62        throws Exception {
63      return readChunk(ByteBufAllocator.DEFAULT);
64    }
65  
66    @Override
67    public final long length() {
68      try {
69        return document.length();
70      } catch (final CommandAbstractException e) {
71        return -1;
72      }
73    }
74  
75    @Override
76    public final long progress() {
77      return offset;
78    }
79  
80    @Override
81    public final boolean isEndOfInput() {
82      return lastChunkAlready;
83    }
84  
85    @Override
86    public final void close() throws HttpIncorrectRetrieveException {
87      try {
88        if (document.isInReading()) {
89          document.abortFile();
90        }
91      } catch (final CommandAbstractException e) {
92        throw new HttpIncorrectRetrieveException(e);
93      }
94      lastChunkAlready = true;
95    }
96  
97    @Override
98    public final ByteBuf readChunk(final ByteBufAllocator byteBufAllocator)
99        throws Exception {
100     // Document
101     final DataBlock block;
102     try {
103       block = document.readDataBlock();
104     } catch (final FileEndOfTransferException e) {
105       lastChunkAlready = true;
106       return Unpooled.EMPTY_BUFFER;
107     } catch (final FileTransferException e) {
108       throw new HttpIncorrectRetrieveException(e);
109     }
110     lastChunkAlready = block.isEOF();
111     offset += block.getByteCount();
112     final byte[] bytes = block.getByteBlock();
113     final ByteBuf buffer = byteBufAllocator.buffer(bytes.length, bytes.length);
114     buffer.writeBytes(bytes);
115     return buffer;
116   }
117 }