nsBinaryStream.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
893N/A/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 3909N/A * ***** BEGIN LICENSE BLOCK ***** 893N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1 893N/A * The contents of this file are subject to the Mozilla Public License Version 893N/A * 1.1 (the "License"); you may not use this file except in compliance with 2362N/A * the License. You may obtain a copy of the License at 893N/A * Software distributed under the License is distributed on an "AS IS" basis, 893N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 893N/A * for the specific language governing rights and limitations under the 893N/A * The Original Code is Mozilla Communicator client code, released 893N/A * The Initial Developer of the Original Code is 893N/A * Netscape Communications Corporation. 893N/A * Portions created by the Initial Developer are Copyright (C) 1998-1999 2362N/A * the Initial Developer. All Rights Reserved. 893N/A * Alternatively, the contents of this file may be used under the terms of 893N/A * either of the GNU General Public License Version 2 or later (the "GPL"), 893N/A * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 893N/A * in which case the provisions of the GPL or the LGPL are applicable instead 893N/A * of those above. If you wish to allow use of your version of this file only 893N/A * under the terms of either the GPL or the LGPL, and not to allow others to 893N/A * use your version of this file under the terms of the MPL, indicate your 893N/A * decision by deleting the provisions above and replace them with the notice 893N/A * and other provisions required by the GPL or the LGPL. If you do not delete 893N/A * the provisions above, a recipient may use your version of this file under 893N/A * the terms of any one of the MPL, the GPL or the LGPL. 893N/A * ***** END LICENSE BLOCK ***** */ 893N/A * This file contains implementations of the nsIBinaryInputStream and 893N/A * nsIBinaryOutputStream interfaces. Together, these interfaces allows reading 893N/A * and writing of primitive data types (integers, floating-point values, 893N/A * booleans, etc.) to a stream in a binary, untagged, fixed-endianness format. 893N/A * This might be used, for example, to implement network protocols or to 893N/A * produce architecture-neutral binary disk files, i.e. ones that can be read 893N/A * and written by both big-endian and little-endian platforms. Output is 893N/A * written in big-endian order (high-order byte first), as this is traditional 893N/A * @See nsIBinaryInputStream 893N/A * @See nsIBinaryOutputStream 893N/A "False assumption about sizeof(float)");
893N/A "False assumption about sizeof(double)");
893N/A // XXX use WriteSegments here to avoid copy! // when forwarding ReadSegments to mInputStream, we need to make sure // 'this' is being passed to the writer each time. To do this, we need // a thunking function which keeps the real input stream around. "False assumption about sizeof(float)");
"False assumption about sizeof(double)");
// sometimes, WriteSegmentToString will be handed an odd-number of // bytes, which means we only have half of the last PRUnichar // there are a few cases we have to account for here: // * even length buffer, no carryover - easy, just append // * odd length buffer, no carryover - the last byte needs to be saved // * odd length buffer, with carryover - first byte needs to be used // with the carryover byte, and // the rest of the even length // buffer is appended as normal // * even length buffer, with carryover - the first byte needs to be // used with the previous carryover byte. // this gives you an odd length buffer, // so you have to save the last byte for // same version of the above, but with correct casting and endian swapping // we're always going to consume the whole buffer no matter what // happens, so take care of that right now.. that allows us to // tweak aCount later. Do NOT move this! // if the last Write had an odd-number of bytes read, then // re-create the two-byte sequence we want to work with // Now the little endianness dance // now skip past the first byte of the buffer.. code from here // can assume normal operations, but should not assume aCount // is relative to the ORIGINAL buffer // this array is possibly unaligned... be careful how we access it! // calculate number of full characters in segment (aCount could be odd!) // copy all data into our aligned buffer. byte swap if necessary. // remember this is the modifed aCount and aFromSegment, // so that will take into account the fact that we might have // skipped the first byte in the buffer // we must have had a carryover byte, that we'll need the next // pre-allocate output buffer, and get direct access to buffer...