Source Code Cross Referenced for DataFile.java in  » Library » mime-pull » org » jvnet » mimepull » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » mime pull » org.jvnet.mimepull 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jvnet.mimepull;
002:
003:        import java.io.*;
004:
005:        /**
006:         * Use {@link RandomAccessFile} for concurrent access of read
007:         * and write partial part's content.
008:         *
009:         * @author Kohsuke Kawaguchi
010:         * @author Jitendra Kotamraju
011:         */
012:        final class DataFile {
013:            private File file;
014:            private RandomAccessFile raf;
015:            private long writePointer;
016:
017:            DataFile(File file) {
018:                this .file = file;
019:                try {
020:                    raf = new RandomAccessFile(file, "rw");
021:                } catch (IOException ioe) {
022:                    throw new MIMEParsingException(ioe);
023:                }
024:                writePointer = 0;
025:            }
026:
027:            /**
028:             * there is no point in calling read(), write(). Directly can use
029:             * {#getInputStream()}
030:             */
031:            void close() {
032:                try {
033:                    raf.close();
034:                } catch (IOException ioe) {
035:                    throw new MIMEParsingException(ioe);
036:                }
037:            }
038:
039:            /**
040:             *
041:             * @return
042:             */
043:            InputStream getInputStream() {
044:                try {
045:                    close();
046:                    return new DirectInputStream(this );
047:                } catch (FileNotFoundException fe) {
048:                    throw new MIMEParsingException(fe);
049:                }
050:            }
051:
052:            /**
053:             * Read data from the given file pointer position.
054:             *
055:             * @param pointer read position
056:             * @param buf that needs to be filled
057:             * @param offset the start offset of the data.
058:             * @param length of data that needs to be read
059:             */
060:            synchronized void read(long pointer, byte[] buf, int offset,
061:                    int length) {
062:                try {
063:                    raf.seek(pointer);
064:                    raf.readFully(buf, offset, length);
065:                } catch (IOException ioe) {
066:                    throw new MIMEParsingException(ioe);
067:                }
068:            }
069:
070:            void renameTo(File f) {
071:                try {
072:                    raf.close();
073:                    file.renameTo(f);
074:                } catch (IOException ioe) {
075:                    throw new MIMEParsingException(ioe);
076:                }
077:            }
078:
079:            /**
080:             * Write data to the file
081:             *
082:             * @param data that needs to written to a file
083:             * @param offset start offset in the data
084:             * @param length no bytes to write
085:             * @return file pointer before the write operation(or at which the
086:             *         data is written)
087:             */
088:            synchronized long writeTo(byte[] data, int offset, int length) {
089:                try {
090:                    long temp = writePointer;
091:                    raf.seek(writePointer);
092:                    raf.write(data, offset, length);
093:                    writePointer = raf.getFilePointer(); // Update pointer for next write
094:                    return temp;
095:                } catch (IOException ioe) {
096:                    throw new MIMEParsingException(ioe);
097:                }
098:            }
099:
100:            /**
101:             * Keeps {@link DataFile} from garbage collected when
102:             * someone is reading it.
103:             */
104:            private static final class DirectInputStream extends
105:                    FilterInputStream {
106:                private DataFile parent;
107:
108:                public DirectInputStream(DataFile parent)
109:                        throws FileNotFoundException {
110:                    super (new FileInputStream(parent.file));
111:                    this .parent = parent;
112:                }
113:
114:                public void close() throws IOException {
115:                    super .close();
116:                    parent = null;
117:                }
118:            }
119:
120:            @Override
121:            protected void finalize() throws Throwable {
122:                try {
123:                    raf.close();
124:                    file.delete();
125:                } catch (Exception e) {
126:                    // Ignore all exceptions
127:                } finally {
128:                    super.finalize();
129:                }
130:            }
131:
132:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.