home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / Request.java < prev    next >
Text File  |  1997-10-25  |  7KB  |  218 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.  
  19. /*  ********************************************************************************
  20.     Constructor ********************************************************************
  21.     ********************************************************************************  */
  22.  
  23.     public Request(IRequest request, IResponse response) 
  24.     {
  25.         m_IRequest = request;
  26.         m_IResponse = response;
  27.         m_arrInput = null;
  28.         m_iInputLength = 0;
  29.         m_iInputPosition = 0;
  30.         m_iMarkPosition = 0;
  31.         m_iMarkReadLimit = 0;
  32.         m_fMarked = false;
  33.     }
  34.  
  35.  
  36. /*  ********************************************************************************
  37.     Internal (Private) Variables ***************************************************
  38.     ********************************************************************************  */
  39.  
  40.     private IRequest  m_IRequest;
  41.     private IResponse m_IResponse;
  42.     private byte []   m_arrInput;
  43.     private int       m_iInputLength;
  44.     private int       m_iInputPosition;
  45.     private int       m_iMarkPosition;
  46.     private int       m_iMarkReadLimit;
  47.     private boolean   m_fMarked;
  48.  
  49.  
  50. /*  ********************************************************************************
  51.     External (Public) Methods ******************************************************
  52.     ********************************************************************************  */
  53.  
  54.     public RequestDictionary getServerVariables() 
  55.     {
  56.         return new RequestDictionary(m_IRequest.getServerVariables());
  57.     }
  58.  
  59.     public RequestDictionary getClientCertificate() 
  60.     {
  61.         return new RequestDictionary(m_IRequest.getClientCertificate());
  62.     }
  63.  
  64.     public RequestDictionary getForm()  
  65.     {
  66.         return new RequestDictionary(m_IRequest.getForm());
  67.     }
  68.  
  69.     public CookieDictionary getCookies()  
  70.     {
  71.         // Note: we pass the IResponse (not IRequest) cookie collection here, to 
  72.         // allow read/write cookie access. 
  73.         return new CookieDictionary(m_IResponse.getCookies());
  74.     }
  75.  
  76.     public RequestDictionary getQueryString() 
  77.     {
  78.         return new RequestDictionary(m_IRequest.getQueryString());
  79.     }
  80.  
  81.     public int getTotalBytes() 
  82.     {
  83.         return m_IRequest.getTotalBytes();
  84.     }
  85.  
  86.  
  87. /*  ********************************************************************************
  88.     External (Public) java.io.InputStream Interface Methods ************************
  89.     ********************************************************************************  */
  90.  
  91.     public int available()                
  92.         throws IOException 
  93.     {
  94.         return getTotalBytes();
  95.     }
  96.  
  97.     public void close()                    
  98.         throws IOException 
  99.     {
  100.         // There is nothing to do here -- ASP streams do not need to be closed
  101.     }
  102.  
  103.     public synchronized void mark(int iReadLimit) 
  104.     {
  105.         m_fMarked        = true;
  106.         m_iMarkPosition  = m_iInputPosition;
  107.         m_iMarkReadLimit = iReadLimit;
  108.     }
  109.  
  110.     public boolean markSupported() 
  111.     {
  112.         return true;
  113.     }
  114.  
  115.     public synchronized void reset()    
  116.         throws IOException 
  117.     {
  118.         if (!m_fMarked) {
  119.             throw new AspIOException(AspLocalizedStrings.ASP_E_UNMARKED_STREAM);
  120.         }
  121.         else {
  122.             m_fMarked = false;
  123.             m_iInputPosition = m_iMarkPosition;
  124.         }
  125.     }
  126.  
  127.     public long skip(long iNumToSkip)    
  128.         throws IOException
  129.     {
  130.         long iProperNumberToSkip = Math.min(iNumToSkip, m_iInputLength - m_iInputPosition);
  131.  
  132.         // Conversion to int should be okay -- everything else
  133.         // takes ints, so shouldn't ever be a problem.
  134.         m_iInputPosition += (int) iProperNumberToSkip;
  135.         
  136.         if (m_fMarked) {
  137.             m_iMarkReadLimit -= iProperNumberToSkip;
  138.             if (m_iMarkReadLimit <= 0) {
  139.                 m_fMarked = false;
  140.             }
  141.         }
  142.  
  143.         return iProperNumberToSkip;
  144.     }
  145.  
  146.     public int read()                    
  147.         throws IOException,
  148.                NullPointerException,
  149.                IndexOutOfBoundsException
  150.     {
  151.         byte [] b = new byte[1];
  152.         int iNumRead = read(b, 0, 1);
  153.         if (iNumRead == -1) return -1;
  154.         else return b[0];
  155.     }
  156.  
  157.     public int read(byte[] b)            
  158.         throws IOException,
  159.                NullPointerException,
  160.                IndexOutOfBoundsException
  161.     {
  162.         return read(b, 0, b.length);
  163.     }
  164.  
  165.     public synchronized int read(byte[] b, int off, int len)    
  166.         throws IOException,
  167.                NullPointerException,
  168.                IndexOutOfBoundsException
  169.     {
  170.         //
  171.         // We want to read in all the data first, and then deal with it later.  We set
  172.         // a reading position for the stream to deal with subsequent calls to the 
  173.         // read() functions.
  174.         //
  175.  
  176.         if (m_arrInput == null) 
  177.         {
  178.             Variant varInput = m_IRequest.BinaryRead(new Variant(getTotalBytes()));
  179.             SafeArray sa = varInput.toSafeArray();
  180.             m_iInputLength = sa.getUBound() - sa.getLBound() + 1;
  181.             m_arrInput = new byte[m_iInputLength];
  182.             sa.getBytes(0, m_iInputLength, m_arrInput, 0);
  183.             m_iInputPosition = 0;
  184.         }
  185.  
  186.         // If there isn't any data left, return -1.
  187.         if (m_iInputPosition >= m_iInputLength) return -1;
  188.  
  189.         // Transfer the smallest number of bytes of (a) the number of bytes requested, 
  190.         // or (b) the number of bytes left to be read.
  191.         int iNumToTransfer = Math.min(len, m_iInputLength - m_iInputPosition);
  192.  
  193.         // If there isn't enough space in the passed in array for the transfer, throw!
  194.         if ((b.length - off) < iNumToTransfer) 
  195.         {
  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.         {
  207.             m_iMarkReadLimit -= iNumToTransfer;
  208.             if (m_iMarkReadLimit <= 0) 
  209.             {
  210.                 m_fMarked = false;
  211.             }
  212.         }
  213.  
  214.         // And return the number of bytes transferred.
  215.         return iNumToTransfer;
  216.     }
  217. }
  218.