Source Code Cross Referenced for Buffer.java in  » Web-Server » simple » simple » util » 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 » Web Server » simple » simple.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Buffer.java February 2001
003:         *
004:         * Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
013:         * GNU Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General 
016:         * Public License along with this library; if not, write to the 
017:         * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
018:         * Boston, MA  02111-1307  USA
019:         */
020:
021:        package simple.util;
022:
023:        import java.io.ByteArrayInputStream;
024:        import java.io.OutputStream;
025:        import java.io.InputStream;
026:        import java.io.IOException;
027:
028:        /**
029:         * The <code>Buffer</code> is intended to be a general purpose byte 
030:         * <code>Buffer</code>. The intended use of the <code>Buffer</code> 
031:         * is to allow the constructiuon of buffering for streams to be 
032:         * seamless and easy.
033:         * <p>
034:         * This provides several convinence methods which make the use of the 
035:         * <code>Buffer</code> easy and useful. The <code>Buffer</code> object 
036:         * allows an initial capacity to be specified however if there is a 
037:         * need for extra space to be added to the <code>Buffer</code> then the 
038:         * append methods will expand the capacity of the <code>Buffer</code> 
039:         * as needed. 
040:         * <p>
041:         * This is also capable of appending other <code>Buffer</code> objects,
042:         * which is quicker that extracting the bytes and then appending those 
043:         * bytes to the second <code>Buffer</code>. The append methods have 
044:         * much the same semantics as the <code>java.lang.StringBuffer</code>.
045:         *
046:         * @author Niall Gallagher
047:         *
048:         * @see simple.util.ByteStore
049:         */
050:        public final class Buffer implements  ByteStore {
051:
052:            /**
053:             * Used to store the bytes passed to the <code>Buffer</code>.
054:             */
055:            private byte[] buf = null;
056:
057:            /**
058:             * This is the count of the number of bytes buffered.
059:             */
060:            private int count = 0;
061:
062:            /**
063:             * This creates a default <code>Buffer</code> object. The initial
064:             * capacity of the default <code>Buffer</code> object is set to 16,
065:             * the capacity will be expanded when the append methods are used 
066:             * and  there is not enough space in the <code>Buffer</code> to
067:             * accomodate the extra bytes.
068:             */
069:            public Buffer() {
070:                this (16);
071:            }
072:
073:            /**
074:             * This creates a <code>Buffer</code>. The initial capacity of
075:             * this <code>Buffer</code> can be specified by the size parameter, 
076:             * the capacity will be expanded when the append methods are used 
077:             * and there is not enough space in the <code>Buffer</code> to 
078:             * accomodate the extra bytes.
079:             *
080:             * @param size the initial capacity of the <code>Buffer</code>
081:             */
082:            public Buffer(int size) {
083:                this .buf = new byte[size];
084:            }
085:
086:            /**
087:             * This creates a <code>Buffer</code> with the data given. The
088:             * capacity will be expanded when the append methods are used 
089:             * and there is not enough space in the <code>Buffer</code> to
090:             * accomodate the extra bytes.
091:             *
092:             * @param b this is the initial data this will have
093:             */
094:            public Buffer(byte[] b) {
095:                this (b, 0, b.length);
096:            }
097:
098:            /**
099:             * This creates a <code>Buffer</code> with the data given. The
100:             * capacity will be expanded when the append methods are used 
101:             * and there is not enough space in the <code>Buffer</code> to
102:             * accomodate the extra bytes.
103:             *
104:             * @param buf this is the initial data this will have
105:             */
106:            public Buffer(Buffer buf) {
107:                this (buf.buf, 0, buf.count);
108:            }
109:
110:            /**
111:             * This creates a <code>Buffer</code> with the data given. The
112:             * capacity will be expanded when the append methods are used 
113:             * and there is not enough space in the <code>Buffer</code> to
114:             * accomodate the extra bytes.
115:             *
116:             * @param b this is the initial data this will have
117:             * @param len this is the number of bytes to be taken
118:             * @param off this is the offset to take the bytes from
119:             */
120:            public Buffer(byte[] b, int off, int len) {
121:                this .buf = new byte[len];
122:                this .count = 0;
123:                this .append(b, off, len); /* checks bounds */
124:            }
125:
126:            /**
127:             * This creates a <code>Buffer</code> with the data given. The
128:             * capacity will be expanded when the append methods are used 
129:             * and there is not enough space in the <code>Buffer</code> to
130:             * accomodate the extra bytes.
131:             *
132:             * @param buf this is the initial data this will have
133:             * @param len this is the number of bytes to be taken
134:             * @param off this is the offset to take the bytes from
135:             */
136:            public Buffer(Buffer buf, int off, int len) {
137:                this .buf = new byte[len];
138:                this .count = 0;
139:                this .append(buf, off, len); /* checks bounds */
140:            }
141:
142:            /**
143:             * This method is used to append bytes to the end of the
144:             * <code>Buffer</code>. This will expand the capacity of 
145:             * the <code>Buffer</code> if there is not enough space 
146:             * to accomodate the extra bytes. 
147:             *
148:             * @param b this is the data that is to be append to this
149:             * <code>Buffer</code>
150:             */
151:            public void append(int b) {
152:                if (count >= buf.length) {
153:                    resize(buf.length + 1);
154:                }
155:                buf[count++] = (byte) b;
156:            }
157:
158:            /**
159:             * This method is used to append bytes to the end of the
160:             * <code>Buffer</code>. This will expand the capacity of 
161:             * the <code>Buffer</code> if there is not enough space 
162:             * to accomodate the extra bytes. 
163:             *
164:             * @param b this is the data that is to be append to this
165:             * <code>Buffer</code>
166:             */
167:            public void append(byte[] b) {
168:                if (b.length + count > buf.length) {
169:                    resize(b.length + count);
170:                }
171:                System.arraycopy(b, 0, buf, count, b.length);
172:                count += b.length;
173:            }
174:
175:            /**
176:             * This method is used to append bytes to the end of the
177:             * <code>Buffer</code>. This will expand the capacity of 
178:             * the <code>Buffer</code> if there is not enough space 
179:             * to accomodate the extra bytes. 
180:             *
181:             * @param b this is the data that is to be append to this
182:             * <code>Buffer</code>
183:             * @param len the number of bytes that are to be taken
184:             * @param off the offset to take the bytes from the array
185:             */
186:            public void append(byte[] b, int off, int len) {
187:                if (off < 0 || len < 0 || off + len > b.length) {
188:                    throw new IndexOutOfBoundsException();
189:                } else if (off > b.length) {
190:                    throw new IndexOutOfBoundsException();
191:                }
192:                if (len + count > buf.length) {
193:                    resize(count + len);
194:                }
195:                System.arraycopy(b, off, buf, count, len);
196:                count += len;
197:            }
198:
199:            /**
200:             * This method is used to append bytes to the end of the
201:             * <code>Buffer</code>. This will expand the capacity of 
202:             * the <code>Buffer</code> if there is not enough space 
203:             * to accomodate the extra bytes. 
204:             *
205:             * @param buf the data that is to be append to this
206:             * <code>Buffer</code>
207:             */
208:            public void append(Buffer buf) {
209:                append(buf.buf, 0, buf.count);
210:            }
211:
212:            /**
213:             * This method is used to append bytes to the end of the 
214:             * <code>Buffer</code>. This will expand the capacity of 
215:             * the <code>Buffer</code> if there is not enough space 
216:             * to accomodate the extra bytes. 
217:             *
218:             * @param buf this is the data that is to be append to this
219:             * <code>Buffer</code>
220:             * @param len the number of bytes that are to be taken
221:             * @param off the offset to take bytes from the array
222:             */
223:            public void append(Buffer buf, int off, int len) {
224:                if (off + len > buf.count) { /* append(byte[]) does the rest */
225:                    throw new IndexOutOfBoundsException();
226:                }
227:                append(buf.buf, off, len);
228:            }
229:
230:            /**
231:             * This method is used to insert bytes into the <code>Buffer</code>
232:             * at the specified position. This will insert the bytes into the
233:             * <code>Buffer</code> without over writing any bytes in the
234:             * <code>Buffer</code>. This will simply move the bytes that come
235:             * before the specified position up the ways and insert the data.
236:             * This will throw an IndexOutOfBoundsException if the insert is
237:             * done at a position where there is no bytes, i.e. must be in the
238:             * bounds.
239:             *
240:             * @param pos this is the position within the <code>Buffer</code>
241:             * to insert data
242:             * @param b this is the data that is to be inserted into the
243:             * <code>Buffer</code>
244:             */
245:            public void insert(int pos, int b) {
246:                if (pos < 0 || pos >= count) {
247:                    throw new IndexOutOfBoundsException();
248:                }
249:                if (count + 1 > buf.length) {
250:                    resize(count + 1);
251:                }
252:                System.arraycopy(buf, pos, buf, pos + 1, count - pos);
253:                buf[pos] = (byte) b;
254:            }
255:
256:            /**
257:             * This method is used to insert bytes into the <code>Buffer</code>
258:             * at the specified position. This will insert the bytes into the
259:             * <code>Buffer</code> without over writing any bytes in the
260:             * <code>Buffer</code>. This will simply move the bytes that come
261:             * before the specified position up the ways and insert the data.
262:             * This will throw an IndexOutOfBoundsException if the insert is
263:             * done at a position where there is no bytes, i.e. must be in the
264:             * bounds.
265:             *
266:             * @param pos this is the position within the <code>Buffer</code>
267:             * to insert data
268:             * @param b this is the data that is to be inserted into the
269:             * <code>Buffer</code>
270:             */
271:            public void insert(int pos, byte[] b) {
272:                if (pos < 0 || pos >= count) {
273:                    throw new IndexOutOfBoundsException();
274:                }
275:                if (count + b.length > buf.length) {
276:                    resize(count + b.length);
277:                }
278:                System.arraycopy(buf, pos, buf, pos + b.length, b.length);
279:                System.arraycopy(buf, pos, b, 0, b.length);
280:            }
281:
282:            /**
283:             * This method is used to insert bytes into the <code>Buffer</code>
284:             * at the specified position. This will insert the bytes into the
285:             * <code>Buffer</code> without over writing any bytes in the
286:             * <code>Buffer</code>. This will simply move the bytes that come
287:             * before the specified position up the ways and insert the data.
288:             * This will throw an IndexOutOfBoundsException if the insert is
289:             * done at a position where there is no bytes, i.e. must be in the
290:             * bounds.
291:             *
292:             * @param pos this is the position within the <code>Buffer</code>
293:             * to insert data
294:             * @param b this is the data that is to be inserted into the
295:             * <code>Buffer</code>
296:             * @param len this is the number of bytes that are to be taken
297:             * @param off this is the offset to take the bytes from the array
298:             */
299:            public void insert(int pos, byte[] b, int off, int len) {
300:                if (off < 0 || len < 0 || off + len > b.length) {
301:                    throw new IndexOutOfBoundsException();
302:                } else if (pos >= count || pos < 0) {
303:                    throw new IndexOutOfBoundsException();
304:                }
305:                if (count + len > buf.length) {
306:                    resize(count + len);
307:                }
308:                System.arraycopy(buf, pos, buf, pos + len, len);
309:                System.arraycopy(buf, pos, b, off, len);
310:            }
311:
312:            /**
313:             * This method is used to insert bytes into the <code>Buffer</code>
314:             * at the specified position. This will insert the bytes into the
315:             * <code>Buffer</code> without over writing any bytes in the
316:             * <code>Buffer</code>. This will simply move the bytes that come
317:             * before the specified position up the ways and insert the data.
318:             * This will throw an IndexOutOfBoundsException if the insert is
319:             * done at a position where there is no bytes, i.e. must be in the
320:             * bounds.
321:             *
322:             * @param pos this is the position within the <code>Buffer</code>
323:             * to insert data
324:             * @param buf this is the data that is to be inserted into the
325:             * <code>Buffer</code>
326:             */
327:            public void insert(int pos, Buffer buf) {
328:                insert(pos, buf.buf, 0, buf.count);
329:            }
330:
331:            /**
332:             * This method is used to insert bytes into the <code>Buffer</code>
333:             * at the specified position. This will insert the bytes into the
334:             * <code>Buffer</code> without over writing any bytes in the
335:             * <code>Buffer</code>. This will simply move the bytes that come
336:             * before the specified position up the ways and insert the data.
337:             * This will throw an IndexOutOfBoundsException if the insert is
338:             * done at a position where there is no bytes, i.e. must be in the
339:             * bounds.
340:             *
341:             * @param pos this is the position within the <code>Buffer</code>
342:             * to insert data
343:             * @param buf this is the data that is to be inserted into the
344:             * <code>Buffer</code>
345:             * @param len this is the number of bytes that are to be taken
346:             * @param off this is the offset to take the bytes from the array
347:             */
348:            public void insert(int pos, Buffer buf, int off, int len) {
349:                if (off + len > buf.count) { /* insert(byte[] b) does the rest */
350:                    throw new IndexOutOfBoundsException();
351:                }
352:                insert(pos, buf.buf, off, len);
353:            }
354:
355:            /**
356:             * This is used to delete a region of data from the <code>Buffer</code>. 
357:             * This will ensure that all the bytes that follow the position of the 
358:             * delete will be copied down the so that the information is removed 
359:             * from the <code>Buffer</code>.
360:             *
361:             * @param pos this is the position that the delete is to be performed
362:             * on
363:             */
364:            public void delete(int pos) {
365:                if (pos >= count || pos < 0) {
366:                    throw new IndexOutOfBoundsException();
367:                }
368:                System.arraycopy(buf, pos + 1, buf, pos, count - pos + 1);
369:                count--;
370:            }
371:
372:            /**
373:             * This is used to delete a region of data from the <code>Buffer</code>. 
374:             * This will ensure that all the bytes that follow the position of the 
375:             * delete will be copied down the so that the information is removed from 
376:             * the <code>Buffer</code>.
377:             *
378:             * @param pos this is the position that the delete is to be performed
379:             * on
380:             * @param len this is the number of bytes after pos that are to be
381:             * removed
382:             */
383:            public void delete(int pos, int len) {
384:                if (pos > count || pos + len > count) {
385:                    throw new IndexOutOfBoundsException();
386:                } else if (pos < 0 || len < 0) {
387:                    throw new IndexOutOfBoundsException();
388:                }
389:                System.arraycopy(buf, pos + len, buf, pos, count - pos + len);
390:                count -= len;
391:            }
392:
393:            /**
394:             * This is used to examine the bytes that are stored in the
395:             * <code>Buffer</code>. This does a byte comparison of the data
396:             * that is stored in the <code>Buffer</code>. The comparison is
397:             * done from an arbitrary offset within the <code>Buffer</code>
398:             * data.
399:             *
400:             * @param pos this is the position within the <code>Buffer</code>
401:             * to compare data
402:             * @param b this is the data that is to be compared to the
403:             * <code>Buffer</code>
404:             * @param len this is the number of bytes that are to be compares
405:             * @param off this is the offset to compare the bytes from the data
406:             *
407:             * @return this returns true if the region given matches the
408:             * <code>Buffer</code>s
409:             */
410:            public boolean regionMatches(int pos, byte[] b, int off, int len) {
411:                if (off < 0 || len < 0 || off + len > b.length) {
412:                    throw new IndexOutOfBoundsException();
413:                } else if (pos + len > count || pos < 0) {
414:                    throw new IndexOutOfBoundsException();
415:                }
416:                for (int i = len + off; off < i; off++) {
417:                    if (buf[pos++] != b[off])
418:                        return false;
419:                }
420:                return true;
421:            }
422:
423:            /**
424:             * This is used to examine the bytes that are stored in the
425:             * <code>Buffer</code>. This does a byte comparison of the data
426:             * that is stored in the <code>Buffer</code>. The comparison is
427:             * done from an arbitrary offset within the <code>Buffer</code>
428:             * data.
429:             *
430:             * @param pos this is the position within the <code>Buffer</code>
431:             * to compare data
432:             * @param buf this is the data that is to be compared to the
433:             * <code>Buffer</code>
434:             * @param len this is the number of bytes that are to be compares
435:             * @param off this is the offset to compare the bytes from the data
436:             *
437:             * @return this returns true if the region given matches the
438:             * <code>Buffer</code>s.
439:             */
440:            public boolean regionMatches(int pos, Buffer buf, int off, int len) {
441:                if (off + len > buf.count) {
442:                    throw new IndexOutOfBoundsException();
443:                }
444:                return regionMatches(pos, buf.buf, off, len);
445:            }
446:
447:            /**
448:             * This is used to compare the contents of one <code>Buffer</code>
449:             * with the contents of another. If the bytes are that same this is
450:             * true.
451:             *
452:             * @param buf this is the data that is to be compared with this
453:             *
454:             * @return this returns true if the <code>Buffer</code>s are
455:             * equal
456:             */
457:            public boolean equals(Buffer buf) {
458:                if (buf.count != count) {
459:                    return false;
460:                }
461:                return regionMatches(0, buf.buf, 0, count);
462:            }
463:
464:            /**
465:             * This is used to write bytes into the <code>Buffer</code> at 
466:             * an arbitrary position. This unlike the insert methods will
467:             * overwrite the data found at that position. This will throw an
468:             * IndexOutOfBoundsException if the position that the data is
469:             * attempting to write to is greater than the <code>Buffer</code>
470:             * length.
471:             *
472:             * @param pos this is the position within the <code>Buffer</code>
473:             * to write the data
474:             * @param b this is the data that is to be written into the
475:             * <code>Buffer</code>
476:             */
477:            public void setByte(int pos, int b) {
478:                if (pos >= count || pos < 0) {
479:                    throw new IndexOutOfBoundsException();
480:                }
481:                buf[pos] = (byte) b;
482:            }
483:
484:            /**
485:             * This is used to write bytes into the <code>Buffer</code> at 
486:             * an arbitrary position. This unlike the insert methods will
487:             * overwrite the data found at that position. This will throw an
488:             * IndexOutOfBoundsException if the position that the data is
489:             * attempting to write to is greater than the <code>Buffer</code>
490:             * length.
491:             *
492:             * @param pos this is the position within the <code>Buffer</code>
493:             * to write the data
494:             * @param b this is the data that is to be written into the
495:             * <code>Buffer</code>
496:             * @param len this is the number of bytes that are to be taken
497:             * @param off this is the offset to take the bytes from the array
498:             */
499:            public void setBytes(int pos, byte[] b, int off, int len) {
500:                if (off < 0 || len < 0 || off + len > b.length) {
501:                    throw new IndexOutOfBoundsException();
502:                } else if (pos >= count || pos < 0) {
503:                    throw new IndexOutOfBoundsException();
504:                }
505:                System.arraycopy(b, off, buf, pos, len);
506:            }
507:
508:            /**
509:             * This is used to write bytes into the <code>Buffer</code> at 
510:             * an arbitrary position. This unlike the insert methods will
511:             * overwrite the data found at that position. This will throw an
512:             * IndexOutOfBoundsException if the position that the data is
513:             * attempting to write to is greater than the <code>Buffer</code>
514:             * length.
515:             *
516:             * @param pos this is the position within the <code>Buffer</code>
517:             * to write the data
518:             * @param buf this is the data that is to be written into the
519:             * <code>Buffer</code>
520:             */
521:            public void setBytes(int pos, Buffer buf) {
522:                setBytes(pos, buf.buf, 0, buf.count);
523:            }
524:
525:            /**
526:             * This is used to write bytes into the <code>Buffer</code> at 
527:             * an arbitrary position. This unlike the insert methods will
528:             * overwrite the data found at that position. This will throw an
529:             * IndexOutOfBoundsException if the position that the data is
530:             * attempting to write to is greater than the <code>Buffer</code>
531:             * length.
532:             *
533:             * @param pos this is the position within the <code>Buffer</code>
534:             * to write the data
535:             * @param buf this is the data that is to be written into the
536:             * <code>Buffer</code>
537:             * @param len this is the number of bytes that are to be taken
538:             * @param off this is the offset to take the bytes from the array
539:             */
540:            public void setBytes(int pos, Buffer buf, int off, int len) {
541:                if (pos + off > buf.count) {
542:                    throw new IndexOutOfBoundsException();
543:                }
544:                setBytes(pos, buf.buf, off, len);
545:            }
546:
547:            /**
548:             * This is used to read data from the <code>Buffer</code>. This 
549:             * will read data from the <code>Buffer</code> at an arbitrary
550:             * position. An IndexOutOfBoundsException will be thrown if there
551:             * is an attempt to read data outside the <code>Buffer</code>s
552:             * bounds.
553:             *
554:             * @param pos this is the position within the <code>Buffer</code>
555:             * to read the data
556:             * @return this will return the byte that can be found at position
557:             * pos
558:             */
559:            public byte getByte(int pos) {
560:                if (pos >= count || pos < 0) {
561:                    throw new IndexOutOfBoundsException();
562:                }
563:                return buf[pos];
564:            }
565:
566:            /**
567:             * This is used to read data from the <code>Buffer</code>. This 
568:             * will read data from the <code>Buffer</code> at an arbitrary
569:             * position. An IndexOutOfBoundsException will be thrown if there
570:             * is an attempt to read data outside the <code>Buffer</code>'s
571:             * bounds.
572:             *
573:             * @param pos this is the position within the <code>Buffer</code>
574:             * to read the data
575:             * @param b this is the <code>Buffer</code> that the bytes are 
576:             * to be read into
577:             */
578:            public void getBytes(int pos, byte[] b) {
579:                getBytes(pos, b, 0, b.length);
580:            }
581:
582:            /**
583:             * This is used to read data from the <code>Buffer</code>. This 
584:             * will read data from the <code>Buffer</code> at an arbitrary
585:             * position. An IndexOutOfBoundsException will be thrown if there
586:             * is an attempt to read data outside the <code>Buffer</code>'s
587:             * bounds.
588:             *
589:             * @param pos this is the position within the <code>Buffer</code>
590:             * to read the data
591:             * @param b this is the <code>Buffer</code> that the bytes are 
592:             * to be read into
593:             * @param len this is the number of bytes to be read from the
594:             * <code>Buffer</code>
595:             * @param off this is the offset to take the bytes from the
596:             * <code>Buffer</code>
597:             */
598:            public void getBytes(int pos, byte[] b, int off, int len) {
599:                if (off < 0 || len < 0 || off + len > b.length) {
600:                    throw new IndexOutOfBoundsException();
601:                } else if (pos + len > count || pos < 0) {
602:                    throw new IndexOutOfBoundsException();
603:                }
604:                System.arraycopy(buf, pos, b, off, len);
605:            }
606:
607:            /**
608:             * This is used to emit data from the <code>Buffer</code> out to 
609:             * an <code>OutputStream</code>. This will invoke a direct call 
610:             * to write in the <code>OutputStream</code>. This method will
611:             * not flush the stream and exceptions will propagate.
612:             *
613:             * @param out this is the <code>OutputStream</code> that the byte
614:             * are to write to
615:             *
616:             * @exception IOException this will be thrown if there is an I/O
617:             * error
618:             */
619:            public void writeTo(OutputStream out) throws IOException {
620:                out.write(buf, 0, count);
621:            }
622:
623:            /**
624:             * This is used to emit data from the <code>Buffer</code> out to 
625:             * an <code>OutputStream</code>. This will invoke a direct call 
626:             * to write in the <code>OutputStream</code>. This method will
627:             * not flush the stream and exceptions will propagate.
628:             *
629:             * @param out this is the <code>OutputStream</code> that the byte
630:             * are to write to
631:             * @param len this is the number of bytes to be write from the
632:             * <code>Buffer</code>
633:             * @param off this is the offset to take the bytes from the
634:             * <code>Buffer</code>
635:             *
636:             * @exception IOException this will be thrown if there is an I/O
637:             * error
638:             */
639:            public void writeTo(OutputStream out, int off, int len)
640:                    throws IOException {
641:                if (off < 0 || len < 0 || off + len > count) {
642:                    throw new IndexOutOfBoundsException();
643:                }
644:                out.write(buf, off, len);
645:            }
646:
647:            /**
648:             * This ensure that there is enough space in the <code>Buffer</code> 
649:             * to allow for more chars to be added. If the <code>Buffer</code> 
650:             * is already larger than min then the <code>Buffer</code> will not 
651:             * be expanded at all.
652:             *
653:             * @param min the minimum size needed for the <code>Buffer</code>
654:             */
655:            public void resize(int min) { /* just get it from vector */
656:                if (buf.length < min) {
657:                    int size = buf.length * 2;
658:                    int max = min > size ? min : size;
659:                    byte[] temp = new byte[max];
660:                    System.arraycopy(buf, 0, temp, 0, count);
661:                    buf = temp;
662:                }
663:            }
664:
665:            /**
666:             * This method is used so that the <code>Buffer</code> can be
667:             * represented as an input stream. Note however that this does not
668:             * make a copy of the data but rather provides direct access to the
669:             * data that is being used. This means that this should not be used
670:             * concurrently with <code>Buffer</code>s methods.
671:             *
672:             * @return this returns an <code>InputStream</code> of the data
673:             * within this <code>Buffer</code>
674:             */
675:            public InputStream getInputStream() {
676:                return new ByteArrayInputStream(buf, 0, count);
677:            }
678:
679:            /**
680:             * This is used to create a copy of the bytes that are stored in the 
681:             * <code>Buffer</code> object. This does not return the internal
682:             * bytes of the <code>Buffer</code> but rather creates a byte array
683:             * <code>Buffer</code>.length in length and fills the byte array
684:             * with the data in the <code>Buffer</code> before its returned.
685:             *
686:             * @return this returns a byte array that contains the
687:             * <code>Buffer</code>s contents
688:             */
689:            public byte[] toArray() {
690:                byte[] copy = new byte[count];
691:                System.arraycopy(buf, 0, copy, 0, count);
692:                return copy;
693:            }
694:
695:            /**
696:             * This will clear all data from the <code>Buffer</code>. This 
697:             * simply sets the count to be zero, it will not clear the memory
698:             * occupied by the instance as the internal buffer will remain.
699:             */
700:            public void clear() {
701:                count = 0;
702:            }
703:
704:            /**
705:             * This provides the number of bytes that can fit inside this
706:             * buffer without requiring an expensive <code>resize</code>. To
707:             * ensure that memory is not wasted this method should be used.
708:             *
709:             * @return the capacity of this buffer before resizing it
710:             */
711:            public int capacity() {
712:                return buf.length;
713:            }
714:
715:            /**
716:             * This returns the number of bytes that have been appended to this
717:             * <code>Buffer</code> object. It indicates the number of bytes
718:             * that have been stored rather than the capacity of the buffer.
719:             *
720:             * @return the number bytes in the <code>Buffer</code> object
721:             */
722:            public int length() {
723:                return count;
724:            }
725:
726:            /**
727:             * This method is used to acquire the buffered bytes as a string.
728:             * This is useful if the contents need to be manipulated as a
729:             * string or transferred into another encoding. If the UTF-8
730:             * content encoding is not supported the platform default is 
731:             * used, however this is unlikely as UTF-8 should be supported.
732:             *
733:             * @return this returns a UTF-8 encoding of the buffer contents
734:             */
735:            public String toString() {
736:                try {
737:                    return new String(buf, 0, count, "UTF-8");
738:                } catch (IOException e) {
739:                }
740:                return new String(buf, 0, count);
741:            }
742:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.