1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store.legacy.doc.internal

File FilesystemAttachmentContent.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

8
23
6
1
125
69
12
0.52
3.83
6
2

Classes

Class Line # Actions
FilesystemAttachmentContent 41 23 0% 12 24
0.3513513535.1%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.store.legacy.doc.internal;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.IOException;
25    import java.io.InputStream;
26   
27    import org.apache.commons.io.IOUtils;
28    import org.apache.commons.io.input.AutoCloseInputStream;
29    import org.xwiki.store.UnexpectedException;
30   
31    import com.xpn.xwiki.doc.XWikiAttachment;
32    import com.xpn.xwiki.doc.XWikiAttachmentContent;
33   
34    /**
35    * The content of an attachment. This implementation is based on a file on the filesystem. This implementation is
36    * mutable but the underlying file is left alone.
37    *
38    * @version $Id: 49b1918cbc62cc74059ee3637e66676d7c5ab176 $
39    * @since 3.0M2
40    */
 
41    public class FilesystemAttachmentContent extends XWikiAttachmentContent
42    {
43    /**
44    * The underlying storage mechanism.
45    */
46    private final File storageFile;
47   
48    /**
49    * The Constructor.
50    *
51    * @param storage the file where the data is stored.
52    * @param attachment the attachment to associate this content with.
53    */
 
54  9 toggle public FilesystemAttachmentContent(final File storage, final XWikiAttachment attachment)
55    {
56  9 super(attachment, null);
57  9 this.storageFile = storage;
58    }
59   
60    /**
61    * Constructor that doesn't set the attachment. This is necessary for loading attachment content without touching
62    * the dirty bit of the owner document.
63    *
64    * @param storage the file where the data is stored.
65    * @since 4.4RC1
66    */
 
67  1 toggle public FilesystemAttachmentContent(final File storage)
68    {
69  1 super(null, null);
70  1 this.storageFile = storage;
71    }
72   
 
73  3 toggle @Override
74    public FilesystemAttachmentContent clone()
75    {
76  3 return new FilesystemAttachmentContent(this.storageFile, this.getAttachment());
77    }
78   
 
79  0 toggle @Override
80    @Deprecated
81    public byte[] getContent()
82    {
83  0 if (this.getFileItem() != null) {
84  0 return super.getContent();
85    }
86   
87  0 final InputStream is = this.getContentInputStream();
88  0 try {
89  0 return IOUtils.toByteArray(is);
90    } catch (IOException e) {
91  0 throw new RuntimeException("Failed to load attachment content", e);
92    } finally {
93  0 IOUtils.closeQuietly(is);
94    }
95    }
96   
 
97  4 toggle @Override
98    public InputStream getContentInputStream()
99    {
100  4 if (this.getFileItem() != null) {
101  0 return super.getContentInputStream();
102    }
103   
104  4 try {
105  4 return new AutoCloseInputStream(new FileInputStream(this.storageFile));
106    } catch (IOException e) {
107  0 throw new UnexpectedException("Failed to get InputStream", e);
108    }
109    }
110   
 
111  0 toggle @Override
112    public int getSize()
113    {
114  0 if (this.getFileItem() != null) {
115  0 return super.getSize();
116    }
117   
118  0 long size = this.storageFile.length();
119    // The most important thing is that it doesn't roll over into the negative space.
120  0 if (size > ((long) Integer.MAX_VALUE)) {
121  0 return Integer.MAX_VALUE;
122    }
123  0 return (int) size;
124    }
125    }