1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePartSource.java,v 1.10 2004/04/18 23:51:37 jsdever Exp $
  3. * $Revision: 1.10 $
  4. * $Date: 2004/04/18 23:51:37 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.methods.multipart;
  30. import java.io.ByteArrayInputStream;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.io.FileNotFoundException;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. /**
  37. * A PartSource that reads from a File.
  38. *
  39. * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
  40. * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
  41. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  42. *
  43. * @since 2.0
  44. */
  45. public class FilePartSource implements PartSource {
  46. /** File part file. */
  47. private File file = null;
  48. /** File part file name. */
  49. private String fileName = null;
  50. /**
  51. * Constructor for FilePartSource.
  52. *
  53. * @param file the FilePart source File.
  54. *
  55. * @throws FileNotFoundException if the file does not exist or
  56. * cannot be read
  57. */
  58. public FilePartSource(File file) throws FileNotFoundException {
  59. this.file = file;
  60. if (file != null) {
  61. if (!file.isFile()) {
  62. throw new FileNotFoundException("File is not a normal file.");
  63. }
  64. if (!file.canRead()) {
  65. throw new FileNotFoundException("File is not readable.");
  66. }
  67. this.fileName = file.getName();
  68. }
  69. }
  70. /**
  71. * Constructor for FilePartSource.
  72. *
  73. * @param fileName the file name of the FilePart
  74. * @param file the source File for the FilePart
  75. *
  76. * @throws FileNotFoundException if the file does not exist or
  77. * cannot be read
  78. */
  79. public FilePartSource(String fileName, File file)
  80. throws FileNotFoundException {
  81. this(file);
  82. if (fileName != null) {
  83. this.fileName = fileName;
  84. }
  85. }
  86. /**
  87. * Return the length of the file
  88. * @return the length of the file.
  89. * @see PartSource#getLength()
  90. */
  91. public long getLength() {
  92. if (this.file != null) {
  93. return this.file.length();
  94. } else {
  95. return 0;
  96. }
  97. }
  98. /**
  99. * Return the current filename
  100. * @return the filename.
  101. * @see PartSource#getFileName()
  102. */
  103. public String getFileName() {
  104. return (fileName == null) ? "noname" : fileName;
  105. }
  106. /**
  107. * Return a new {@link FileInputStream} for the current filename.
  108. * @return the new input stream.
  109. * @throws IOException If an IO problem occurs.
  110. * @see PartSource#createInputStream()
  111. */
  112. public InputStream createInputStream() throws IOException {
  113. if (this.file != null) {
  114. return new FileInputStream(this.file);
  115. } else {
  116. return new ByteArrayInputStream(new byte[] {});
  117. }
  118. }
  119. }