Coverage Summary for Class: LittleEndianDataOutputStream (com.google.common.io)

Class Class, % Method, % Line, %
LittleEndianDataOutputStream 0% (0/1) 0% (0/14) 0% (0/20)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.io; 16  17 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.GwtIncompatible; 19 import com.google.common.base.Preconditions; 20 import com.google.common.primitives.Longs; 21 import java.io.DataOutput; 22 import java.io.DataOutputStream; 23 import java.io.FilterOutputStream; 24 import java.io.IOException; 25 import java.io.OutputStream; 26  27 /** 28  * An implementation of {@link DataOutput} that uses little-endian byte ordering for writing {@code 29  * char}, {@code short}, {@code int}, {@code float}, {@code double}, and {@code long} values. 30  * 31  * <p><b>Note:</b> This class intentionally violates the specification of its supertype {@code 32  * DataOutput}, which explicitly requires big-endian byte order. 33  * 34  * @author Chris Nokleberg 35  * @author Keith Bottner 36  * @since 8.0 37  */ 38 @Beta 39 @GwtIncompatible 40 @ElementTypesAreNonnullByDefault 41 public final class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput { 42  43  /** 44  * Creates a {@code LittleEndianDataOutputStream} that wraps the given stream. 45  * 46  * @param out the stream to delegate to 47  */ 48  public LittleEndianDataOutputStream(OutputStream out) { 49  super(new DataOutputStream(Preconditions.checkNotNull(out))); 50  } 51  52  @Override 53  public void write(byte[] b, int off, int len) throws IOException { 54  // Override slow FilterOutputStream impl 55  out.write(b, off, len); 56  } 57  58  @Override 59  public void writeBoolean(boolean v) throws IOException { 60  ((DataOutputStream) out).writeBoolean(v); 61  } 62  63  @Override 64  public void writeByte(int v) throws IOException { 65  ((DataOutputStream) out).writeByte(v); 66  } 67  68  /** 69  * @deprecated The semantics of {@code writeBytes(String s)} are considered dangerous. Please use 70  * {@link #writeUTF(String s)}, {@link #writeChars(String s)} or another write method instead. 71  */ 72  @Deprecated 73  @Override 74  public void writeBytes(String s) throws IOException { 75  ((DataOutputStream) out).writeBytes(s); 76  } 77  78  /** 79  * Writes a char as specified by {@link DataOutputStream#writeChar(int)}, except using 80  * little-endian byte order. 81  * 82  * @throws IOException if an I/O error occurs 83  */ 84  @Override 85  public void writeChar(int v) throws IOException { 86  writeShort(v); 87  } 88  89  /** 90  * Writes a {@code String} as specified by {@link DataOutputStream#writeChars(String)}, except 91  * each character is written using little-endian byte order. 92  * 93  * @throws IOException if an I/O error occurs 94  */ 95  @Override 96  public void writeChars(String s) throws IOException { 97  for (int i = 0; i < s.length(); i++) { 98  writeChar(s.charAt(i)); 99  } 100  } 101  102  /** 103  * Writes a {@code double} as specified by {@link DataOutputStream#writeDouble(double)}, except 104  * using little-endian byte order. 105  * 106  * @throws IOException if an I/O error occurs 107  */ 108  @Override 109  public void writeDouble(double v) throws IOException { 110  writeLong(Double.doubleToLongBits(v)); 111  } 112  113  /** 114  * Writes a {@code float} as specified by {@link DataOutputStream#writeFloat(float)}, except using 115  * little-endian byte order. 116  * 117  * @throws IOException if an I/O error occurs 118  */ 119  @Override 120  public void writeFloat(float v) throws IOException { 121  writeInt(Float.floatToIntBits(v)); 122  } 123  124  /** 125  * Writes an {@code int} as specified by {@link DataOutputStream#writeInt(int)}, except using 126  * little-endian byte order. 127  * 128  * @throws IOException if an I/O error occurs 129  */ 130  @Override 131  public void writeInt(int v) throws IOException { 132  out.write(0xFF & v); 133  out.write(0xFF & (v >> 8)); 134  out.write(0xFF & (v >> 16)); 135  out.write(0xFF & (v >> 24)); 136  } 137  138  /** 139  * Writes a {@code long} as specified by {@link DataOutputStream#writeLong(long)}, except using 140  * little-endian byte order. 141  * 142  * @throws IOException if an I/O error occurs 143  */ 144  @Override 145  public void writeLong(long v) throws IOException { 146  byte[] bytes = Longs.toByteArray(Long.reverseBytes(v)); 147  write(bytes, 0, bytes.length); 148  } 149  150  /** 151  * Writes a {@code short} as specified by {@link DataOutputStream#writeShort(int)}, except using 152  * little-endian byte order. 153  * 154  * @throws IOException if an I/O error occurs 155  */ 156  @Override 157  public void writeShort(int v) throws IOException { 158  out.write(0xFF & v); 159  out.write(0xFF & (v >> 8)); 160  } 161  162  @Override 163  public void writeUTF(String str) throws IOException { 164  ((DataOutputStream) out).writeUTF(str); 165  } 166  167  // Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior: 168  // it silently ignores any exception thrown by flush(). Instead, just close the delegate stream. 169  // It should flush itself if necessary. 170  @Override 171  public void close() throws IOException { 172  out.close(); 173  } 174 }