Source Code Cross Referenced for BufferedIndexOutput.java in  » Net » lucene-connector » org » apache » lucene » store » 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 » Net » lucene connector » org.apache.lucene.store 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.store;
002:
003:        /**
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import java.io.IOException;
021:
022:        /** Base implementation class for buffered {@link IndexOutput}. */
023:        public abstract class BufferedIndexOutput extends IndexOutput {
024:            static final int BUFFER_SIZE = 16384;
025:
026:            private final byte[] buffer = new byte[BUFFER_SIZE];
027:            private long bufferStart = 0; // position in file of buffer
028:            private int bufferPosition = 0; // position in buffer
029:
030:            /** Writes a single byte.
031:             * @see IndexInput#readByte()
032:             */
033:            public void writeByte(byte b) throws IOException {
034:                if (bufferPosition >= BUFFER_SIZE)
035:                    flush();
036:                buffer[bufferPosition++] = b;
037:            }
038:
039:            /** Writes an array of bytes.
040:             * @param b the bytes to write
041:             * @param length the number of bytes to write
042:             * @see IndexInput#readBytes(byte[],int,int)
043:             */
044:            public void writeBytes(byte[] b, int offset, int length)
045:                    throws IOException {
046:                int bytesLeft = BUFFER_SIZE - bufferPosition;
047:                // is there enough space in the buffer?
048:                if (bytesLeft >= length) {
049:                    // we add the data to the end of the buffer
050:                    System.arraycopy(b, offset, buffer, bufferPosition, length);
051:                    bufferPosition += length;
052:                    // if the buffer is full, flush it
053:                    if (BUFFER_SIZE - bufferPosition == 0)
054:                        flush();
055:                } else {
056:                    // is data larger then buffer?
057:                    if (length > BUFFER_SIZE) {
058:                        // we flush the buffer
059:                        if (bufferPosition > 0)
060:                            flush();
061:                        // and write data at once
062:                        flushBuffer(b, offset, length);
063:                        bufferStart += length;
064:                    } else {
065:                        // we fill/flush the buffer (until the input is written)
066:                        int pos = 0; // position in the input data
067:                        int pieceLength;
068:                        while (pos < length) {
069:                            pieceLength = (length - pos < bytesLeft) ? length
070:                                    - pos : bytesLeft;
071:                            System.arraycopy(b, pos + offset, buffer,
072:                                    bufferPosition, pieceLength);
073:                            pos += pieceLength;
074:                            bufferPosition += pieceLength;
075:                            // if the buffer is full, flush it
076:                            bytesLeft = BUFFER_SIZE - bufferPosition;
077:                            if (bytesLeft == 0) {
078:                                flush();
079:                                bytesLeft = BUFFER_SIZE;
080:                            }
081:                        }
082:                    }
083:                }
084:            }
085:
086:            /** Forces any buffered output to be written. */
087:            public void flush() throws IOException {
088:                flushBuffer(buffer, bufferPosition);
089:                bufferStart += bufferPosition;
090:                bufferPosition = 0;
091:            }
092:
093:            /** Expert: implements buffer write.  Writes bytes at the current position in
094:             * the output.
095:             * @param b the bytes to write
096:             * @param len the number of bytes to write
097:             */
098:            private void flushBuffer(byte[] b, int len) throws IOException {
099:                flushBuffer(b, 0, len);
100:            }
101:
102:            /** Expert: implements buffer write.  Writes bytes at the current position in
103:             * the output.
104:             * @param b the bytes to write
105:             * @param offset the offset in the byte array
106:             * @param len the number of bytes to write
107:             */
108:            protected abstract void flushBuffer(byte[] b, int offset, int len)
109:                    throws IOException;
110:
111:            /** Closes this stream to further operations. */
112:            public void close() throws IOException {
113:                flush();
114:            }
115:
116:            /** Returns the current position in this file, where the next write will
117:             * occur.
118:             * @see #seek(long)
119:             */
120:            public long getFilePointer() {
121:                return bufferStart + bufferPosition;
122:            }
123:
124:            /** Sets current position in this file, where the next write will occur.
125:             * @see #getFilePointer()
126:             */
127:            public void seek(long pos) throws IOException {
128:                flush();
129:                bufferStart = pos;
130:            }
131:
132:            /** The number of bytes in the file. */
133:            public abstract long length() throws IOException;
134:
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.