Source Code Cross Referenced for CheckedInputStream.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » zip » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
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
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.zip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util.zip;
027
028        import java.io.FilterInputStream;
029        import java.io.InputStream;
030        import java.io.IOException;
031
032        /**
033         * An input stream that also maintains a checksum of the data being read.
034         * The checksum can then be used to verify the integrity of the input data.
035         *
036         * @see		Checksum
037         * @version 	1.28, 05/05/07
038         * @author 	David Connelly
039         */
040        public class CheckedInputStream extends FilterInputStream {
041            private Checksum cksum;
042
043            /**
044             * Creates an input stream using the specified Checksum.
045             * @param in the input stream
046             * @param cksum the Checksum
047             */
048            public CheckedInputStream(InputStream in, Checksum cksum) {
049                super (in);
050                this .cksum = cksum;
051            }
052
053            /**
054             * Reads a byte. Will block if no input is available.
055             * @return the byte read, or -1 if the end of the stream is reached.
056             * @exception IOException if an I/O error has occurred
057             */
058            public int read() throws IOException {
059                int b = in.read();
060                if (b != -1) {
061                    cksum.update(b);
062                }
063                return b;
064            }
065
066            /**
067             * Reads into an array of bytes. If <code>len</code> is not zero, the method
068             * blocks until some input is available; otherwise, no
069             * bytes are read and <code>0</code> is returned.
070             * @param buf the buffer into which the data is read
071             * @param off the start offset in the destination array <code>b</code>
072             * @param len the maximum number of bytes read
073             * @return    the actual number of bytes read, or -1 if the end
074             *		  of the stream is reached.
075             * @exception  NullPointerException If <code>buf</code> is <code>null</code>.
076             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
077             * <code>len</code> is negative, or <code>len</code> is greater than 
078             * <code>buf.length - off</code>
079             * @exception IOException if an I/O error has occurred
080             */
081            public int read(byte[] buf, int off, int len) throws IOException {
082                len = in.read(buf, off, len);
083                if (len != -1) {
084                    cksum.update(buf, off, len);
085                }
086                return len;
087            }
088
089            /**
090             * Skips specified number of bytes of input.
091             * @param n the number of bytes to skip
092             * @return the actual number of bytes skipped
093             * @exception IOException if an I/O error has occurred
094             */
095            public long skip(long n) throws IOException {
096                byte[] buf = new byte[512];
097                long total = 0;
098                while (total < n) {
099                    long len = n - total;
100                    len = read(buf, 0, len < buf.length ? (int) len
101                            : buf.length);
102                    if (len == -1) {
103                        return total;
104                    }
105                    total += len;
106                }
107                return total;
108            }
109
110            /**
111             * Returns the Checksum for this input stream.
112             * @return the Checksum value
113             */
114            public Checksum getChecksum() {
115                return cksum;
116            }
117        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.