home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / Request.java < prev    next >
Encoding:
Java Source  |  1997-09-04  |  6.3 KB  |  216 lines

  1. /*    ********************************************************************************
  2.     Request.java *******************************************************************
  3.     ********************************************************************************  */
  4.  
  5. package aspcomp;
  6.  
  7. import java.io.*;
  8. import com.ms.com.*;
  9. import com.ms.asp.*;
  10.  
  11. /**
  12.  * Cover class for the ASP Request intrinsic.
  13.  */
  14.  
  15. public class Request 
  16.     extends InputStream 
  17. {
  18.     // Consider: add a global Map that searches through all Dictionaries
  19.  
  20. /*    ********************************************************************************
  21.     Constructor ********************************************************************
  22.     ********************************************************************************  */
  23.  
  24.     public Request(IRequest request, IResponse response) 
  25.     {
  26.         m_IRequest            = request;
  27.         m_IResponse            = response;
  28.         m_arrInput            = null;
  29.         m_iInputLength        = 0;
  30.         m_iInputPosition    = 0;
  31.         m_iMarkPosition        = 0;
  32.         m_iMarkReadLimit    = 0;
  33.         m_fMarked            = false;
  34.     }
  35.  
  36.  
  37. /*    ********************************************************************************
  38.     Internal (Private) Variables ***************************************************
  39.     ********************************************************************************  */
  40.  
  41.     private IRequest  m_IRequest;
  42.     private IResponse m_IResponse;
  43.     private byte []   m_arrInput;
  44.     private int       m_iInputLength;
  45.     private int       m_iInputPosition;
  46.     private int       m_iMarkPosition;
  47.     private int       m_iMarkReadLimit;
  48.     private boolean   m_fMarked;
  49.  
  50.  
  51. /*    ********************************************************************************
  52.     External (Public) Methods ******************************************************
  53.     ********************************************************************************  */
  54.  
  55.     public RequestDictionary getServerVariables() 
  56.     {
  57.         return new RequestDictionary(m_IRequest.getServerVariables());
  58.     }
  59.  
  60.     public RequestDictionary getClientCertificate() 
  61.     {
  62.         return new RequestDictionary(m_IRequest.getClientCertificate());
  63.     }
  64.  
  65.     public RequestDictionary getForm()  
  66.     {
  67.         return new RequestDictionary(m_IRequest.getForm());
  68.     }
  69.  
  70.     public CookieDictionary getCookies()  
  71.     {
  72.         // Consider: we pass the IResponse cookie collection here, to allow read/write
  73.         // cookie access. This is different that ASP. change this? If so, we need to
  74.         // fix Cookie, etc. to deal with read vs. read/write cookies.
  75.         return new CookieDictionary(m_IResponse.getCookies());
  76.     }
  77.  
  78.     public RequestDictionary getQueryString() 
  79.     {
  80.         return new RequestDictionary(m_IRequest.getQueryString());
  81.     }
  82.  
  83.     public int getTotalBytes() 
  84.     {
  85.         return m_IRequest.getTotalBytes();
  86.     }
  87.  
  88.  
  89. /*    ********************************************************************************
  90.     External (Public) java.io.InputStream Interface Methods ************************
  91.     ********************************************************************************  */
  92.  
  93.     public int available()                
  94.         throws IOException 
  95.     {
  96.         return getTotalBytes();
  97.     }
  98.  
  99.     public void close()                    
  100.         throws IOException 
  101.     {
  102.         // There is nothing to do here -- ASP streams do not need to be closed
  103.     }
  104.  
  105.     public synchronized void mark(int iReadLimit) 
  106.     {
  107.         m_fMarked        = true;
  108.         m_iMarkPosition     = m_iInputPosition;
  109.         m_iMarkReadLimit = iReadLimit;
  110.     }
  111.  
  112.     public boolean markSupported() 
  113.     {
  114.         return true;
  115.     }
  116.  
  117.     public synchronized void reset()    
  118.         throws IOException 
  119.     {
  120.         if (!m_fMarked) {
  121.             throw new AspIOException(AspLocalizedStrings.ASP_E_UNMARKED_STREAM);
  122.         }
  123.         else {
  124.             m_fMarked = false;
  125.             m_iInputPosition = m_iMarkPosition;
  126.         }
  127.     }
  128.  
  129.     public long skip(long iNumToSkip)    
  130.         throws IOException
  131.     {
  132.         long iProperNumberToSkip = Math.min(iNumToSkip, m_iInputLength - m_iInputPosition);
  133.  
  134.         // Conversion to int should be okay -- everything else
  135.         // takes ints, so shouldn't ever be a problem.
  136.         m_iInputPosition += (int) iProperNumberToSkip;
  137.         
  138.         if (m_fMarked) {
  139.             m_iMarkReadLimit -= iProperNumberToSkip;
  140.             if (m_iMarkReadLimit <= 0) {
  141.                 m_fMarked = false;
  142.             }
  143.         }
  144.  
  145.         return iProperNumberToSkip;
  146.     }
  147.  
  148.     public int read()                    
  149.         throws IOException,
  150.                NullPointerException,
  151.                IndexOutOfBoundsException
  152.     {
  153.         byte [] b = new byte[1];
  154.         int iNumRead = read(b, 0, 1);
  155.         if (iNumRead == -1) return -1;
  156.         else return b[0];
  157.     }
  158.  
  159.     public int read(byte[] b)            
  160.         throws IOException,
  161.                NullPointerException,
  162.                IndexOutOfBoundsException
  163.     {
  164.         return read(b, 0, b.length);
  165.     }
  166.  
  167.     public synchronized int read(byte[] b, int off, int len)    
  168.         throws IOException,
  169.                NullPointerException,
  170.                IndexOutOfBoundsException
  171.     {
  172.         //
  173.         // We want to read in all the data first, and then deal with it later.  We set
  174.         // a reading position for the stream to deal with subsequent calls to the 
  175.         // read() functions.
  176.         //
  177.  
  178.         if (m_arrInput == null) {
  179.             Variant varInput    = m_IRequest.BinaryRead(new Variant(getTotalBytes()));
  180.             SafeArray sa        = varInput.toSafeArray();
  181.             m_iInputLength        = sa.getUBound() - sa.getLBound() + 1;
  182.             m_arrInput            = new byte[m_iInputLength];
  183.             sa.getBytes(0, m_iInputLength, m_arrInput, 0);
  184.             m_iInputPosition    = 0;
  185.         }
  186.  
  187.         // If there isn't any data left, return -1.
  188.         if (m_iInputPosition >= m_iInputLength) return -1;
  189.  
  190.         // Transfer the smallest number of bytes of (a) the number of bytes requested, 
  191.         // or (b) the number of bytes left to be read.
  192.         int iNumToTransfer = Math.min(len, m_iInputLength - m_iInputPosition);
  193.  
  194.         // If there isn't enough space in the passed in array for the transfer, throw!
  195.         if ((b.length - off) < iNumToTransfer) {
  196.             // There won't be enough space in the passed in array.
  197.             throw new ArrayIndexOutOfBoundsException();        
  198.         }
  199.  
  200.         // Do the transfer of bytes, and update the current input position.
  201.         System.arraycopy(m_arrInput, m_iInputPosition, b, off, iNumToTransfer);
  202.         m_iInputPosition += iNumToTransfer;
  203.  
  204.         // Update the marking status, if necessary.
  205.         if (m_fMarked) {
  206.             m_iMarkReadLimit -= iNumToTransfer;
  207.             if (m_iMarkReadLimit <= 0) {
  208.                 m_fMarked = false;
  209.             }
  210.         }
  211.  
  212.         // And return the number of bytes transferred.
  213.         return iNumToTransfer;
  214.     }
  215. }
  216.