GEOS  3.13.0dev
ByteOrderDataInStream.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2005-2006 Refractions Research Inc.
7  * Copyright (C) 2001-2002 Vivid Solutions Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************
15  *
16  * Last port: io/ByteOrderDataInStream.java rev. 1.1 (JTS-1.10)
17  *
18  **********************************************************************/
19 
20 #pragma once
21 
22 #include <geos/export.h>
23 #include <geos/io/ParseException.h>
24 #include <geos/io/ByteOrderValues.h>
25 #include <geos/util/Machine.h> // for getMachineByteOrder
26 
27 #include <cstdint>
28 #include <iosfwd> // ostream, istream (if we remove inlines)
29 
30 namespace geos {
31 namespace io {
32 
40 class GEOS_DLL ByteOrderDataInStream {
41 
42 public:
43 
45  : ByteOrderDataInStream(nullptr, 0) {};
46 
47  ByteOrderDataInStream(const unsigned char* buff, size_t buffsz)
48  : byteOrder(getMachineByteOrder())
49  , buf(buff)
50  , end(buff + buffsz)
51  {};
52 
54 
55  void setOrder(int order)
56  {
57  byteOrder = order;
58  };
59 
60  unsigned char readByte() // throws ParseException
61  {
62  if(size() < 1) {
63  throw ParseException("Unexpected EOF parsing WKB");
64  }
65  auto ret = buf[0];
66  buf++;
67  return ret;
68  };
69 
70  int32_t readInt()
71  {
72  if(size() < 4) {
73  throw ParseException("Unexpected EOF parsing WKB");
74  }
75  auto ret = ByteOrderValues::getInt(buf , byteOrder);
76  buf += 4;
77  return ret;
78  };
79 
80  uint32_t readUnsigned()
81  {
82  if(size() < 4) {
83  throw ParseException("Unexpected EOF parsing WKB");
84  }
85  auto ret = ByteOrderValues::getUnsigned(buf , byteOrder);
86  buf += 4;
87  return ret;
88  };
89 
90  int64_t readLong()
91  {
92  if(size() < 8) {
93  throw ParseException("Unexpected EOF parsing WKB");
94  }
95 
96  auto ret = ByteOrderValues::getLong(buf, byteOrder);
97  buf += 8;
98  return ret;
99  };
100 
101  double readDouble()
102  {
103  if(size() < 8) {
104  throw ParseException("Unexpected EOF parsing WKB");
105  }
106  auto ret = ByteOrderValues::getDouble(buf, byteOrder);
107  buf += 8;
108  return ret;
109  };
110 
111  size_t size() const
112  {
113  return static_cast<size_t>(end - buf);
114  };
115 
116 
117 private:
118  int byteOrder;
119  const unsigned char* buf;
120  const unsigned char* end;
121 
122 };
123 
124 } // namespace io
125 } // namespace geos
Allows reading an stream of primitive datatypes from an underlying istream, with the representation b...
Definition: ByteOrderDataInStream.h:40
Notifies a parsing error.
Definition: ParseException.h:33
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25