001package org.apache.commons.ssl.org.bouncycastle.asn1; 002 003import java.io.IOException; 004import java.io.InputStream; 005 006class ConstructedOctetStream 007 extends InputStream 008{ 009 private final ASN1StreamParser _parser; 010 011 private boolean _first = true; 012 private InputStream _currentStream; 013 014 ConstructedOctetStream( 015 ASN1StreamParser parser) 016 { 017 _parser = parser; 018 } 019 020 public int read(byte[] b, int off, int len) throws IOException 021 { 022 if (_currentStream == null) 023 { 024 if (!_first) 025 { 026 return -1; 027 } 028 029 ASN1OctetStringParser s = (ASN1OctetStringParser)_parser.readObject(); 030 031 if (s == null) 032 { 033 return -1; 034 } 035 036 _first = false; 037 _currentStream = s.getOctetStream(); 038 } 039 040 int totalRead = 0; 041 042 for (;;) 043 { 044 int numRead = _currentStream.read(b, off + totalRead, len - totalRead); 045 046 if (numRead >= 0) 047 { 048 totalRead += numRead; 049 050 if (totalRead == len) 051 { 052 return totalRead; 053 } 054 } 055 else 056 { 057 ASN1OctetStringParser aos = (ASN1OctetStringParser)_parser.readObject(); 058 059 if (aos == null) 060 { 061 _currentStream = null; 062 return totalRead < 1 ? -1 : totalRead; 063 } 064 065 _currentStream = aos.getOctetStream(); 066 } 067 } 068 } 069 070 public int read() 071 throws IOException 072 { 073 if (_currentStream == null) 074 { 075 if (!_first) 076 { 077 return -1; 078 } 079 080 ASN1OctetStringParser s = (ASN1OctetStringParser)_parser.readObject(); 081 082 if (s == null) 083 { 084 return -1; 085 } 086 087 _first = false; 088 _currentStream = s.getOctetStream(); 089 } 090 091 for (;;) 092 { 093 int b = _currentStream.read(); 094 095 if (b >= 0) 096 { 097 return b; 098 } 099 100 ASN1OctetStringParser s = (ASN1OctetStringParser)_parser.readObject(); 101 102 if (s == null) 103 { 104 _currentStream = null; 105 return -1; 106 } 107 108 _currentStream = s.getOctetStream(); 109 } 110 } 111}